[英]Break JSON list of values into rows in a SNOWFLAKE database table
這是一種方法。 首先去掉 [" 和 ]" 因為 city 列中的雙引號不包含單個數組元素而是包含所有元素,然后將字符串標記化並使用 strtok_to_array 將其作為真實數組返回,然后將數組元素展平以分隔行並將行(城市)橫向連接回記錄的其余部分。
with data as
(select 'A' as name, 'M' as gender, '["completed"]' as orders, '["Cochi,Hyderabad"]' as city
union all
select 'B' as name, 'M' as gender, '["completed"]' as orders, '["Cochi,Hyderabad,Delhi"]' as city
union all
select 'C' as name, 'F' as gender, '["cancelled"]' as orders, '["Mumbai,Pune"]' as city
union all
select 'D' as name, 'M' as gender, '["pending"]' as orders, '["cochi"]' as city
)
, data2 as
( select d.name
, d.gender
, replace(replace(d.orders,'["',''),'"]','') as orders
, strtok_to_array(replace(replace(city,'["',''),'"]',''),',') as city
from data d
)
select d2.name
, d2.gender
, d2.orders
, replace(c.value,'"','') as city
from data2 d2
, lateral flatten(input => d2.city) c;
如果city
字段中的數組實際上是一個包含單個逗號分隔字符串的數組,如["Cochi, Hyderabad"]
,則需要使用STRTOK_TO_ARRAY
進行第三級展平,如下所示:
create or replace table t1 (json variant) as
select parse_json('{"Name": "A", "Gender": "M", "orders": ["completed"], "city": ["Cochi, Hyderabad"]}')
union all
select parse_json('{"Name": "B", "Gender": "M", "orders": ["completed"], "city": ["Cochi, Hyderabad, Delhi"]}')
union all
select parse_json('{"Name": "C", "Gender": "F", "orders": ["cancelled"], "city": ["Mumbai, Pune"]}')
union all
select parse_json('{"Name": "D", "Gender": "M", "orders": ["pending"], "city": ["Cochi"]}')
;
select
json:Name::varchar name,
json:Gender::varchar gender,
orders.value::varchar orders,
city.value::varchar city
from t1,
lateral flatten(json:orders) orders,
lateral flatten(json:city) city_raw,
lateral flatten(strtok_to_array(city_raw.value, ', ')) city
;
/*
NAME GENDER ORDERS CITY
A M completed Cochi
A M completed Hyderabad
B M completed Cochi
B M completed Hyderabad
B M completed Delhi
C F cancelled Mumbai
C F cancelled Pune
D M pending Cochi
*/
LATERAL FLATTEN
是一種將對象 (JSON) 或數組中的值展平(擴展)為行並與父表中的原始行組合的習慣用法。
所以,上面的查詢是:
orders
字段中的數組以將數組元素展開為輸出中ORDERS
列的行city
字段中的數組展平,將數組元素展開為輸出中CITY_RAW
中間列的行CITY_RAW
列中提取的逗號分隔字符串拆分為城市名稱,並將字符串的中間數組展平為CITY
列中的行這種方式使您無需任何復雜的字符串操作即可獲得所需的輸出。
但是,我懷疑存儲在 JSON 中的數組實際上就像["Cochi", "Hyderabad"]
,每個城市名稱用雙引號引用的城市名稱數組。
在這種情況下,查詢將很簡單; 您只需要通過對每個字段使用LATERAL FLATTEN
來展平orders
字段和city
字段中的數組。
create or replace table t2 (json variant) as
select parse_json('{"Name": "A", "Gender": "M", "orders": ["completed"], "city": ["Cochi", "Hyderabad"]}')
union all
select parse_json('{"Name": "B", "Gender": "M", "orders": ["completed"], "city": ["Cochi", "Hyderabad", "Delhi"]}')
union all
select parse_json('{"Name": "C", "Gender": "F", "orders": ["cancelled"], "city": ["Mumbai", "Pune"]}')
union all
select parse_json('{"Name": "D", "Gender": "M", "orders": ["pending"], "city": ["Cochi"]}')
;
select
json:Name::varchar name,
json:Gender::varchar gender,
orders.value::varchar orders,
city.value::varchar city
from t2,
lateral flatten(json:orders) orders,
lateral flatten(json:city) city
;
/*
NAME GENDER ORDERS CITY
A M completed Cochi
A M completed Hyderabad
B M completed Cochi
B M completed Hyderabad
B M completed Delhi
C F cancelled Mumbai
C F cancelled Pune
D M pending Cochi
*/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.