【问题标题】:Aggregate over repeated fields: BigQuery聚合重复字段:BigQuery
【发布时间】:2021-08-26 14:34:53
【问题描述】:

我是 bigquery 的新手,并试图了解如何在嵌套数组上进行聚合。

数据字段看起来像这样

clientid region  m.metric  m.metric.key  m.metric.value
     1      A.     large       0            100
                              10            200
                              20            300 

如何获取按 clientid 分组的 metric.values 的聚合

谢谢!

【问题讨论】:

    标签: google-cloud-platform google-bigquery


    【解决方案1】:

    尝试以下解决方案:

    with sample as (
        select 1 as clientid, "A." as region, STRUCT("large" as metric, struct([0,10,20] as key, [100,200,300] as value) as x) as m
    )
    select clientid
    , sum(metric_value)
    from sample
    cross join unnest(m.x.value) as metric_value
    group by clientid 
    

    【讨论】:

    • 如果 m.metric.key 和 m.metric.value 记录很多,我该如何采用这个方案。而不是 struct[10,20,30] 有没有办法概括这一点?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多