如何将列表中对象的字段值聚合到 Snowflake 中的一行中?
How to aggregate the field values of objects in a list into a row in Snowflake?
我有一个带有 id 的 table 和一个包含名称的类似 JSON 的对象列表。我需要将这个对象列表转换为单个字符串,名称以逗号分隔。
原版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'}]
预期结果
1 John, Mary
2 Joseph
3 NULL (or empty string)
4 Alexander, Martha, Eugene
我尝试过将 LATERAL JOIN 与 FLATTEN 函数结合使用,但这似乎不合适,因为它会生成更多行。我应该使用它然后用 listagg 分组吗?感觉有更简单的方法。
这是一个 db-fiddle,其中包含两个 table。
使用的数据集如下 -
select * from tab_names_json;
+---------------------------+
| NAMES |
|---------------------------|
| { |
| "tab2": [ |
| { |
| "name": "Joseph" |
| } |
| ], |
| "tab3": [], |
| "tab4": [ |
| { |
| "age": 12, |
| "name": "Alexander" |
| }, |
| { |
| "age": 42, |
| "name": "Martha" |
| }, |
| { |
| "name": "Eugene" |
| } |
| ], |
| "tabl": [ |
| { |
| "age": 23, |
| "name": "John" |
| }, |
| { |
| "age": 50, |
| "name": "Mary" |
| } |
| ] |
| } |
+---------------------------+
注意:我加载的文件实际上是'tab*'从tab1到tab4排序的;然而,
JSON.
中未保留顺序
要提取的查询。
select distinct listagg(ifnull(tab1.name,'null'),',')
within group(order by tab1.name)
over (partition by tab1.seq) as agg
from
(select f1.seq as seq,f1.value:name as name from TAB_NAMES_JSON t,
lateral flatten(input=>t.names) as f,
lateral flatten (input => f.value,mode=>'ARRAY',outer=>TRUE) f1) tab1;
+-------------------------+
| AGG |
|-------------------------|
| Joseph |
| null |
| Alexander,Eugene,Martha |
| John,Mary |
+-------------------------+
我有一个带有 id 的 table 和一个包含名称的类似 JSON 的对象列表。我需要将这个对象列表转换为单个字符串,名称以逗号分隔。
原版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'}]
预期结果
1 John, Mary
2 Joseph
3 NULL (or empty string)
4 Alexander, Martha, Eugene
我尝试过将 LATERAL JOIN 与 FLATTEN 函数结合使用,但这似乎不合适,因为它会生成更多行。我应该使用它然后用 listagg 分组吗?感觉有更简单的方法。
这是一个 db-fiddle,其中包含两个 table。
使用的数据集如下 -
select * from tab_names_json;
+---------------------------+
| NAMES |
|---------------------------|
| { |
| "tab2": [ |
| { |
| "name": "Joseph" |
| } |
| ], |
| "tab3": [], |
| "tab4": [ |
| { |
| "age": 12, |
| "name": "Alexander" |
| }, |
| { |
| "age": 42, |
| "name": "Martha" |
| }, |
| { |
| "name": "Eugene" |
| } |
| ], |
| "tabl": [ |
| { |
| "age": 23, |
| "name": "John" |
| }, |
| { |
| "age": 50, |
| "name": "Mary" |
| } |
| ] |
| } |
+---------------------------+
注意:我加载的文件实际上是'tab*'从tab1到tab4排序的;然而, JSON.
中未保留顺序要提取的查询。
select distinct listagg(ifnull(tab1.name,'null'),',')
within group(order by tab1.name)
over (partition by tab1.seq) as agg
from
(select f1.seq as seq,f1.value:name as name from TAB_NAMES_JSON t,
lateral flatten(input=>t.names) as f,
lateral flatten (input => f.value,mode=>'ARRAY',outer=>TRUE) f1) tab1;
+-------------------------+
| AGG |
|-------------------------|
| Joseph |
| null |
| Alexander,Eugene,Martha |
| John,Mary |
+-------------------------+