无法以雪花中的预期格式转换数据
Not able to transform data in expected format in snowflake
我得到了这样一列的行数据
[
{
"value": "A",
"path": "nth-child(1)"
},
{
"value": "K",
"path": "nth-child(2)"
},
{
"value": "C",
"path": "nth-child(3)"
}
]
需要帮助.....
想要从该列的行中获取这种格式的数据
{
"A",
"K",
"C",
},
像这样尝试过:但它结合了 table
的所有行
SELECT LISTAGG(f.value:value::STRING, ',') AS col
FROM tablename
,LATERAL FLATTEN(input => parse_json(column_name)) f
我使用 CTE 只是为了为示例提供假数据:
WITH data(json) as (
select parse_json(column1) from values
('[{"value":"A","path":"nth-child(1)"},{"value":"K","path":"nth-child(2)"},{"value":"C","path":"nth-child(3)"}]'),
('[{"value":"B","path":"nth-child(1)"},{"value":"L","path":"nth-child(2)"},{"value":"D","path":"nth-child(3)"}]'),
('[{"value":"C","path":"nth-child(1)"},{"value":"M","path":"nth-child(2)"},{"value":"E","path":"nth-child(3)"}]')
)
SELECT LISTAGG(f.value:value::text,',') as l1
from data as d
,table(flatten(input=>d.json)) f
group by f.seq
order by f.seq;
给出:
L1
A,K,C
B,L,D
C,M,E
因此通过 ||
进行一些字符串连接
SELECT '{' || LISTAGG('"' ||f.value:value::text|| '"' , ',') || '}' as l1
from data as d
,table(flatten(input=>d.json)) f
group by f.seq
order by f.seq;
给出:
L1
{"A","K","C"}
{"B","L","D"}
{"C","M","E"}
我得到了这样一列的行数据
[
{
"value": "A",
"path": "nth-child(1)"
},
{
"value": "K",
"path": "nth-child(2)"
},
{
"value": "C",
"path": "nth-child(3)"
}
]
需要帮助..... 想要从该列的行中获取这种格式的数据
{
"A",
"K",
"C",
},
像这样尝试过:但它结合了 table
的所有行SELECT LISTAGG(f.value:value::STRING, ',') AS col
FROM tablename
,LATERAL FLATTEN(input => parse_json(column_name)) f
我使用 CTE 只是为了为示例提供假数据:
WITH data(json) as (
select parse_json(column1) from values
('[{"value":"A","path":"nth-child(1)"},{"value":"K","path":"nth-child(2)"},{"value":"C","path":"nth-child(3)"}]'),
('[{"value":"B","path":"nth-child(1)"},{"value":"L","path":"nth-child(2)"},{"value":"D","path":"nth-child(3)"}]'),
('[{"value":"C","path":"nth-child(1)"},{"value":"M","path":"nth-child(2)"},{"value":"E","path":"nth-child(3)"}]')
)
SELECT LISTAGG(f.value:value::text,',') as l1
from data as d
,table(flatten(input=>d.json)) f
group by f.seq
order by f.seq;
给出:
L1 |
---|
A,K,C |
B,L,D |
C,M,E |
因此通过 ||
SELECT '{' || LISTAGG('"' ||f.value:value::text|| '"' , ',') || '}' as l1
from data as d
,table(flatten(input=>d.json)) f
group by f.seq
order by f.seq;
给出:
L1 |
---|
{"A","K","C"} |
{"B","L","D"} |
{"C","M","E"} |