首页 > 解决方案 > 连接 Google Big Query 中的重复记录

问题描述

我有以下 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),因为它不是纯粹的数组。我有点夹在两者之间。

谢谢你。

标签: sqlgoogle-bigquery

解决方案


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

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

推荐阅读