[英]How to unnest/explode/flatten the comma separated value in a column in Amazon Redshift?
我正在嘗試為 col2 中的每個值生成一個新行。 由於該值采用字符串格式,因此在使用任何 Redshift json 函數之前,我需要將其用雙引號括起來。
輸入:
col1(int) col2(varchar)
1 ab,cd,ef
2 gh
3 jk,lm,kn,ut,zx
輸出:
col1(int) col2(varchar)
1 ab
1 cd
1 ef
2 gh
3 jk
3 lm
3 kn
3 ut
3 zx
with NS AS (
select 1 as n union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9 union all
select 10
)
select
TRIM(SPLIT_PART(B.col2, ',', NS.n)) AS col2
from NS
inner join table B ON NS.n <= REGEXP_COUNT(B.col2, ',') + 1
這里,NS(數字序列)是一個 CTE,它返回一個從 1 到 N 的數字列表,這里我們必須確保我們的最大數字大於我們最大標簽的大小,因此您可以嘗試添加更多數字到該列表取決於您的上下文。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.