I have the following table where "product" is a repeated field.
How can I group by "id" and merge the repeated field to sum the quantities so the output looks like this.
Trying to find an elegant solution that does not unnest.
1 answers
solution1
1 ACCPTED 2021-09-28 14:56:22
Consider below
select id, array(
select as struct sku, sum(quantity) quantity
from t.product
group by sku
) product
from (
select id, array_concat_agg(product) product
from your_table
group by id
) t
if applied to sample data in your question - output is
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.