如何将列表中对象的字段值聚合到Snowflake中的一行中?

发布于 2025-01-17 01:47:12 字数 603 浏览 1 评论 0原文

我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

草莓味的萝莉 2025-01-24 01:47:12

使用的数据集如下 -

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"      |
|     }                     |
|   ]                       |
| }                         |
+---------------------------+

注意:我加载的文件实际上具有从 tab1 到 tab4 排序的“tab*”;然而,
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               |
+-------------------------+

Used dataset as below -

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"      |
|     }                     |
|   ]                       |
| }                         |
+---------------------------+

Note: The file I loaded actually had 'tab*' ordered from tab1 to tab4; however,
order is not preserved in JSON.

Query to fetch.

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               |
+-------------------------+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文