【问题标题】:Concatenate Repeated Record in Google Big Query连接 Google Big Query 中的重复记录
【发布时间】:2020-03-27 10:49:20
【问题描述】:

我的数据存在于以下 JSON 结构中

{"person": "John", "children": [{"name":"Tim", "age":9},{"name":"Suszan", "age":12},{"name":"Karl", "age":14}]}

在 Big Query 中,它具有 schmea

children        RECORD  REPEATED    
children. age   INTEGER NULLABLE    
children. name  STRING  NULLABLE    
person          STRING  NULLABLE    

当我们查看实际的表格时,我们有这个

但我真的很想拥有以下格式的数据

我被卡住了,因为我知道我可以使用unnest(children) as children 来访问记录,但它会创建一个新行,但我不能使用ARRAY_TO_STRING(children.name),因为它不是纯粹的数组。我有点夹在两者之间。

谢谢。

【问题讨论】:

    标签: sql google-bigquery


    【解决方案1】:

    您应该能够访问元素并聚合:

    select t.person, string_agg(child, ',')
    from t cross join
         unnest(children) child
    group by t.person;
    

    【讨论】:

    • 完美! string_agg(child.name) 是我必须做的唯一改变,但这正是我想要的!我很感激!一旦“提问的冷却时间”过去,我会接受这个作为答案
    【解决方案2】:

    我宁愿建议低于版本

    #standardSQL
    SELECT person, 
      (SELECT STRING_AGG(name) FROM t.children) AS children
    FROM `project.dataset.table` t 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多