2015-09-25 87 views
0

一个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 
+0

查看http://stackoverflow.com/questions/25112389/redshift-convert-comma-delimited-values-into-rows/31998832#31998832 –

回答

1

在红移,你可以加入对数表,并用它作为分割指数:

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。 :(

+0

Redshift似乎不支持递归子查询 – AlexYes

0

SQL Fiddle

的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 

Results

| REGEXP_SUBSTR(T1.COL,'([^,])+',1,T2.COLUMN_VALUE) | 
|---------------------------------------------------| 
|             a | 
|             c | 
|             b | 
|             d | 
|             a | 
|             b | 
|             e | 
|             c | 
|             d | 
|             f |