1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
WITH t1(code, code_nm, m_seq, x1, x2, x3, x4 , y1, y2, y3, y4) AS ( SELECT '001, '01', 5, 1, 2, 3, 4, 5, 6, 7, 8 FROM dual UNION ALL SELECT '002', '02', 2, 9, 10, 11, 12 ,13, 14, 15, 16 FROM dual ) 안녕하세요. 잘안풀려서 올려봄니다. 결과는 아래처럼 나오고싶구요. 예를들어 grp1 에 x1, x2 나오고 다음줄 grp2 에 x3, x4 가 나오는 식으로 하고싶습니다. 두개씩 끊어서.. 반나절 하구있는데 잘안되네요 ㅠㅠ code code_nm 종류 값1 값2 값3 값4 =================================== 001 01 grp1 1 2 5 6 001 01 grp2 3 4 7 8 002 02 grp1 9 10 13 14 002 02 grp2 11 12 15 16 . . 생략.... |
WITH t1(code, code_nm, m_seq, x1, x2, x3, x4 , y1, y2, y3, y4) AS ( SELECT '001', '01', 5, 1, 2, 3, 4, 5, 6, 7, 8 FROM dual UNION ALL SELECT '002', '02', 2, 9, 10, 11, 12 ,13, 14, 15, 16 FROM dual ) -- 1. 행 복제 후 Decode -- SELECT code , code_nm , 'grp' || lv gb , DECODE(lv, 1, x1, x3) v1 , DECODE(lv, 1, x2, x4) v2 , DECODE(lv, 1, y1, y3) v3 , DECODE(lv, 1, y2, y4) v4 FROM t1 , (SELECT LEVEL lv FROM dual CONNECT BY LEVEL <= 2) ORDER BY code, lv ;
WITH t1(code, code_nm, m_seq, x1, x2, x3, x4 , y1, y2, y3, y4) AS ( SELECT '001', '01', 5, 1, 2, 3, 4, 5, 6, 7, 8 FROM dual UNION ALL SELECT '002', '02', 2, 9, 10, 11, 12 ,13, 14, 15, 16 FROM dual ) -- 2. 다중항목 UNPIVOT -- SELECT * FROM t1 UNPIVOT ((v1, v2, v3, v4) FOR gb IN ( (x1, x2, y1, y2) AS 'grp1' , (x3, x4, y3, y4) AS 'grp2' ) ) ;