在雪花中,如何从键值列表中获取某个键的所有值的列表
in snowflake, how to get a list of all values of a certain key out a list of key values
有一个大型半结构化对象的列,其中一个部分本身就是一个键值(实际上是一个键值列表)我可以这样得到它:
t.payload:questions_and_answers
给出:
[{"answer":"yes","position":0,"question":"would you"},
{"answer":"because","position":1,"question":"what"}]
我想从中得到:
yes, because
有什么想法吗?
使用展平:
CREATE OR REPLACE TABLE t
AS
SELECT PARSE_JSON('{questions_and_answers:[{"answer":"yes","position":0,"question":"would you"},
{"answer":"because","position":1,"question":"what"}]}') AS payload;
查询:
SELECT s.value:answer::STRING
FROM t
,TABLE(FLATTEN (input => t.payload, PATH =>'questions_and_answers')) s;
或者如果需要单输出:
SELECT LISTAGG(s.value:answer::STRING, ', ') AS result
FROM t
,TABLE(FLATTEN (input => t.payload, PATH =>'questions_and_answers')) s;
输出:
有一个大型半结构化对象的列,其中一个部分本身就是一个键值(实际上是一个键值列表)我可以这样得到它:
t.payload:questions_and_answers
给出:
[{"answer":"yes","position":0,"question":"would you"},
{"answer":"because","position":1,"question":"what"}]
我想从中得到:
yes, because
有什么想法吗?
使用展平:
CREATE OR REPLACE TABLE t
AS
SELECT PARSE_JSON('{questions_and_answers:[{"answer":"yes","position":0,"question":"would you"},
{"answer":"because","position":1,"question":"what"}]}') AS payload;
查询:
SELECT s.value:answer::STRING
FROM t
,TABLE(FLATTEN (input => t.payload, PATH =>'questions_and_answers')) s;
或者如果需要单输出:
SELECT LISTAGG(s.value:answer::STRING, ', ') AS result
FROM t
,TABLE(FLATTEN (input => t.payload, PATH =>'questions_and_answers')) s;
输出: