简体   繁体   English

雪花 SQL:如何使用 JSON 对象循环遍历数组,以找到符合条件的项目

[英]Snowflake SQL: How to loop through array with JSON objects, to find item that meets condition

Breaking my head on this.打破我的头。 In Snowflake my field city_info looks like (for 3 sample records)在雪花中,我的字段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}]

I try to extract a column city from this我尝试从中提取列city

Chicago
Boston
New York

I tried to flatten this我试图把它弄平

select *
from lateral flatten(input =>
  select city_info::VARIANT as event
  from data
)

And from there I can derive the value, but this only allows me to do this for 1 row (so I have to add limit 1 which doesn't makes sense, as I need this for all my rows).从那里我可以得出该值,但这仅允许我为 1 行执行此操作(因此我必须添加没有意义的limit 1 ,因为我的所有行都需要此)。

If I try to do it for the 3 rows it tells me subquery returns more than one row.如果我尝试对 3 行执行此操作,它会告诉我subquery returns more than one row.

Any help is appreciated!任何帮助表示赞赏! Chris克里斯

You could write it as:你可以把它写成:

SELECT value:content::string AS city_name
FROM tab,
LATERAL FLATTEN(input => tab.city_info)
WHERE value:name::string = 'city'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM