돌려보시면 감이 오실겁니다. 

unpivot 뒤에 줄줄이오는 컬럼헤더1~4까지는 사용자정의 컬럼명으로 들어갈 이름이고, 코드컬럼은 행으로 나오는 컬럼입니다. 

SELECT *
FROM (
   SELECT '282' 맨왼쪽, '1' 두번째
           , '홍길동1' R1_L_02, '12345' R1_L_02_1, '20200101' R1_L_03, 'Y' R1_L_04
           , '홍길동2' R2_L_02, '22345' R2_L_02_1, '10200101' R2_L_03, 'Y' R2_L_04
           , '홍길동3' R3_L_02, '32345' R3_L_02_1, '30200101' R3_L_03, 'Y' R3_L_04
   FROM DUAL
   UNION ALL
   SELECT '2323' COL1, '2' COL2
           , '장길산1' RL_02, '123445' R1_L_02_1, '20200101' R1_L_03, 'Y' R1_L_04
           , '일지매1' R2_L_02, '223345' R2_L_02_1, '10200101' R2_L_03, 'N' R2_L_04
           , '장길산3' R3_L_02, '3D345' R3_L_02_1, '30200101' R3_L_03, 'Y' R3_L_04
   FROM DUAL   
   )
UNPIVOT ((컬럼헤더1, 컬럼헤더2, 컬럼헤더3, 컬럼헤더4) FOR 코드컬럼 IN (
         (R1_L_02, R1_L_02_1, R1_L_03, R1_L_04) AS '1'
        ,(R2_L_02, R2_L_02_1, R2_L_03, R2_L_04) AS '2'
        ,(R3_L_02, R3_L_02_1, R3_L_03, R3_L_04) AS '3'
     )

)
반응형
Posted by cocon