【问题标题】:BigQuery SQL to add metadata to a columnBigQuery SQL 将元数据添加到列
【发布时间】:2021-11-17 07:26:19
【问题描述】:

目前,我有一个具有以下架构的 BigQuery 表:

region node_ids
1 [1, 2, 3, 4]
2 [5, 6, 7, 8]

其中 node_ids 是 BigQuery 中的 array<int64> 列。

我有另一个表,其中包含从每个 node_id 到该节点中错误数的映射,它看起来像这样。

node_id errors
1 2
2 3
3 7
4 5
5 6
6 9
7 10
8 6

我想使用这个辅助表来增加第一个表的错误值。具体来说,我想将 node_ids 转换为 array<struct<int64, int64>>,其中每个 node_id 还带有错误数。

我不确定如何在 BigQuery SQL 中执行此操作。非常感谢您提前提供的帮助。

【问题讨论】:

    标签: sql database google-bigquery


    【解决方案1】:

    考虑以下方法

    select region, array_agg(struct(node_id, errors)) node_ids
    from regions r,
    r.node_ids node_id
    join mapping 
    using(node_id) 
    group by region       
    

    如果应用于您问题中的样本数据 - 输出是

    结果架构是

    另一种选择是避免取消嵌套然后分组 - 仅在行级别进行映射

    select region, array(
        select as struct node_id, errors
        from r.node_ids node_id
        join mapping 
        using(node_id) 
      ) node_ids
    from regions r
    

    显然输出完全相同

    【讨论】:

      猜你喜欢
      • 2018-07-05
      • 2020-01-17
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 2010-11-06
      • 2021-12-22
      • 2020-06-27
      • 1970-01-01
      相关资源
      最近更新 更多