簡體   English   中英

對 BQ 中的重復字段進行分組

[英]Grouping repeated fields in BQ

我有下表,其中“產品”是一個重復字段。

在此處輸入圖像描述

我如何按“id”分組並合並重復的字段以對數量求和,以便 output 看起來像這樣。

在此處輸入圖像描述

試圖找到一個不會取消嵌套的優雅解決方案。

考慮以下

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          

如果應用於您問題中的樣本數據 - output 是

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM