oracle PIVOT, UNPIVOT

2016. 7. 29. 15:1499. 정리전 - IT/15. SQL 따라잡기

행을 열로

열을 행으로



WITH T AS (

   SELECT 'AA' AS KEY1, 

          'BB' AS KEY2,

           10  AS VAL1,

           20  AS VAL2

     FROM DUAL 

UNION ALL 

   SELECT 'CC' AS KEY1, 

          'DD' AS KEY2, 

           30  AS VAL1,

           40  AS VAL2

     FROM DUAL 

)

SELECT KEY1 AS KEY_, VAL1 AS VAL_ FROM T UNPIVOT (KEY1 FOR G IN (KEY1))

UNION ALL

SELECT KEY2, VAL2 FROM T UNPIVOT (KEY2 FOR G IN (KEY2))

  ->