본문 바로가기
DataBase/oracle

Oracle 가로데이터 세로정렬

by 아이티.파머 2011. 8. 2.
반응형

 

가로데이터를 세로로 만들어야 할때, 

 

with main_T as (
  SELECT '1' A1 , '2' A2 , '3' A3 , '4' A4,'5' A5, '6' A6 , '7' A7 FROM DUAL 
), copy_t as (
SELECT 'A' gubun FROM dual 
UNION ALL SELECT 'B' FROM dual
UNION ALL SELECT 'C' FROM dual
UNION ALL SELECT 'D' FROM dual
UNION ALL SELECT 'E' FROM dual
UNION ALL SELECT 'F' FROM dual
UNION ALL SELECT 'G' FROM dual
)SELECT
      c.gubun
     , CASE WHEN c.gubun = 'A' THEN m.a1
            WHEN c.gubun = 'B' THEN m.a2
            WHEN c.gubun = 'C' THEN m.a3
            WHEN c.gubun = 'D' THEN m.a4
            WHEN c.gubun = 'E' THEN m.a5
            WHEN c.gubun = 'F' THEN m.a6
            WHEN c.gubun = 'G' THEN m.a7
        END amt
  FROM main_t m   , copy_t c
 ORDER BY c.gubun
;
반응형