Snowflake SQL:如何循环遍历包含 JSON 个对象的数组,以找到满足条件的项目
Snowflake SQL: How to loop through array with JSON objects, to find item that meets condition
让我头疼。在 Snowflake 中,我的字段 city_info
看起来像(对于 3 个示例记录)
[{"name": "age", "content": 35}, {"name": "city", "content": "Chicago"}]
[{"name": "age", "content": 20}, {"name": "city", "content": "Boston"}]
[{"name": "city", "content": "New York"}, {"name": "age", "content": 42}]
我尝试从中提取一列city
Chicago
Boston
New York
我试着把它弄平
select *
from lateral flatten(input =>
select city_info::VARIANT as event
from data
)
从那里我可以得出值,但这只允许我为 1 行执行此操作(所以我必须添加 limit 1
这没有意义,因为我所有的都需要这个行)。
如果我尝试对 3 行执行此操作,它会告诉我 subquery returns more than one row.
感谢任何帮助!克里斯
你可以这样写:
SELECT value:content::string AS city_name
FROM tab,
LATERAL FLATTEN(input => tab.city_info)
WHERE value:name::string = 'city'
让我头疼。在 Snowflake 中,我的字段 city_info
看起来像(对于 3 个示例记录)
[{"name": "age", "content": 35}, {"name": "city", "content": "Chicago"}]
[{"name": "age", "content": 20}, {"name": "city", "content": "Boston"}]
[{"name": "city", "content": "New York"}, {"name": "age", "content": 42}]
我尝试从中提取一列city
Chicago
Boston
New York
我试着把它弄平
select *
from lateral flatten(input =>
select city_info::VARIANT as event
from data
)
从那里我可以得出值,但这只允许我为 1 行执行此操作(所以我必须添加 limit 1
这没有意义,因为我所有的都需要这个行)。
如果我尝试对 3 行执行此操作,它会告诉我 subquery returns more than one row.
感谢任何帮助!克里斯
你可以这样写:
SELECT value:content::string AS city_name
FROM tab,
LATERAL FLATTEN(input => tab.city_info)
WHERE value:name::string = 'city'