如何将列表中对象的字段值聚合到Snowflake中的一行中?
我有一个包含 id 的表和一个包含名称的类似 JSON 的对象列表。我需要将此对象列表转换为单个字符串,名称以逗号分隔。
原始表
1 [{'name':'John', age: 23}, {'name':'Mary', age: 50}]
2 [{'name':'Joseph'}]
3 []
4 [{'name':'Alexander', age:12}, {'name':'Martha', age:42}, {'name':'Eugene'}]
预期结果
1 John, Mary
2 Joseph
3 NULL (or empty string)
4 Alexander, Martha, Eugene
我已经尝试使用 LATERAL JOIN 和 FLATTEN 函数,但这似乎不合适,因为它会生成更多行。我应该使用它然后使用 listagg 进行分组吗?感觉有一种更简单的方法可以做到这一点。
这是一个包含两个表的 db-fiddle 。
I have a table with id and a list of JSON-like objects containing names. I need to convert this list of objects into a single string with the names separated by commas.
Original table
1 [{'name':'John', age: 23}, {'name':'Mary', age: 50}]
2 [{'name':'Joseph'}]
3 []
4 [{'name':'Alexander', age:12}, {'name':'Martha', age:42}, {'name':'Eugene'}]
Expected result
1 John, Mary
2 Joseph
3 NULL (or empty string)
4 Alexander, Martha, Eugene
I've experimented with a LATERAL JOIN together with a FLATTEN function but that doesn't seem fit as it generates more rows. Should I use that and then group by with listagg? It feels like there's an easier way of doing it.
Here's a db-fiddle with both tables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用的数据集如下 -
注意:我加载的文件实际上具有从 tab1 到 tab4 排序的“tab*”;然而,
JSON 中不保留顺序。
查询来获取。
Used dataset as below -
Note: The file I loaded actually had 'tab*' ordered from tab1 to tab4; however,
order is not preserved in JSON.
Query to fetch.