一個LISTAGG我有一個表,可能是由於一個LISTAGG,與此類似:撤消紅移
# select * from s;
s
-----------
a,c,b,d,a
b,e,c,d,f
(2 rows)
我怎樣才能把它變成該行集:
a
c
b
d
a
b
e
c
d
f
一個LISTAGG我有一個表,可能是由於一個LISTAGG,與此類似:撤消紅移
# select * from s;
s
-----------
a,c,b,d,a
b,e,c,d,f
(2 rows)
我怎樣才能把它變成該行集:
a
c
b
d
a
b
e
c
d
f
在紅移,你可以加入對數表,並用它作爲分割指數:
with recursive Numbers as (
select 1 as i
union all
select i + 1 as i from Numbers where i <= 5
)
select split_part(s,',', i) from Numbers, s ORDER by s,i;
編輯:redshift似乎不支持遞歸子查詢,只有postgres。 :(
Redshift似乎不支持遞歸子查詢 – AlexYes
的Oracle 11g R2架構設置:
create table s(
col varchar2(20));
insert into s values('a,c,b,d,a');
insert into s values('b,e,c,d,f');
查詢1:
SELECT REGEXP_SUBSTR(t1.col, '([^,])+', 1, t2.COLUMN_VALUE)
FROM s t1 CROSS JOIN
TABLE
(
CAST
(
MULTISET
(
SELECT LEVEL
FROM DUAL
CONNECT BY LEVEL <= REGEXP_COUNT(t1.col, '([^,])+')
)
AS SYS.odciNumberList
)
) t2
| REGEXP_SUBSTR(T1.COL,'([^,])+',1,T2.COLUMN_VALUE) |
|---------------------------------------------------|
| a |
| c |
| b |
| d |
| a |
| b |
| e |
| c |
| d |
| f |
查看http://stackoverflow.com/questions/25112389/redshift-convert-comma-delimited-values-into-rows/31998832#31998832 –