안녕하세요
여러 컬럼값을 그대로 pivot 하려고 할때 pivot을 여러개주면 그냥 다 한줄로 나와 버리던데
ex pivot(max(col), min(col) ...
col1 | col2 | col3 | col4 |
1 | value1 | value6 | value11 |
2 | value2 | value7 | value12 |
3 | value3 | value8 | value13 |
4 | value4 | value9 | value14 |
5 | value5 | value10 | value15 |
위 데이터를
1 | 2 | 3 | 4 | 5 |
value1 | value2 | value3 | value4 | value5 |
value6 | value7 | value8 | value9 | value10 |
value11 | value12 | value13 | value14 | value15 |
이렇게 가능 할까요? 1:1매칭으로는 잘되던데 1:N의 경우는 너무 어렵네요..
with t as (select 1 c1, 'val1' c2, 'val6' c3, 'val11' c4 from dual union all select 2 c1, 'val2' c2, 'val7' c3, 'val12' c4 from dual union all select 3 c1, 'val3' c2, 'val8' c3, 'val13' c4 from dual union all select 4 c1, 'val4' c2, 'val9' c3, 'val14' c4 from dual union all select 5 c1, 'val5' c2, 'val10' c3, 'val15' c4 from dual ) select v1, v2, v3, v4, v5 from t pivot(max(c2) as c2, max(c3) as c3, max(c4) as c4 for c1 in (1 as v1, 2 v2, 3 v3, 4 v4, 5 v5)) unpivot((v1, v2, v3, v4, v5) for gb in ((v1_c2, v2_c2, v3_c2, v4_c2, v5_c2) , (v1_c3, v2_c3, v3_c3, v4_c3, v5_c3) , (v1_c4, v2_c4, v3_c4, v4_c4, v5_c4) ) ) ;
WITH T AS ( SELECT '1' COL1 , 'value1' COL2 , 'value6' COL3, 'value11' COL4 FROM DUAL UNION ALL SELECT '2' COL1 , 'value2' COL2 , 'value7' COL3, 'value12' COL4 FROM DUAL UNION ALL SELECT '3' COL1 , 'value3' COL2 , 'value8' COL3, 'value13' COL4 FROM DUAL UNION ALL SELECT '4' COL1 , 'value4' COL2 , 'value9' COL3, 'value14' COL4 FROM DUAL UNION ALL SELECT '5' COL1 , 'value5' COL2 , 'value10' COL3, 'value15' COL4 FROM DUAL ) SELECT A, B, C, D, E FROM ( SELECT GB, VAL, RN FROM (SELECT COL1 ,COL2 ,COL3, COL4, ROWNUM RN FROM T) UNPIVOT ( VAL FOR GB IN (COL1 AS 1, COL2 AS 2 ,COL3 AS 3, COL4 AS 4)) ) PIVOT(MAX(VAL) FOR RN IN (1 A,2 B ,3 C, 4 D, 5 E))