수정하다그만.. 추후 다시올릴게요 질문내용
# 단순한 피벗은 가능한데. 피벗과 고정행 고정열 맞추어 없으면 자리까지 만들어줘야하는게 잘안풀리네요.
데이터는 년월일에 seq로 쭈욱있는형태구요 100건이있어도 6개만 dt6까지
1. 우선 GBN 은 CODE 에 맨뒤 L과 R 을 구분한 것이고.
2.
3. M GBN 값은 MX, MY, MZ 에 뒷자리 3개 를 열로 가져온것입니다. (이 x y z 값이 없어도 0 으로 나오도록 )
4. DT1 ~ DT6 까지 잇는데 DT6 값은 있는데 구멍을 일부러 만들었구요. (고정 컬럼입니다.) 없어도 나오도록.
일자에 SEQ 순으로 값을 행으로 열로 나오도록요. 없는 값은 0으로 표기.
집합이 안되네요.. 꽉막혀서 도움청합니다. 이게 가능한지.;;
에궁.. 날씨가 덥습니다 고생하세요 ^^;
WITH t1(code, code1, code2, m_dt, m_seq, mx, my, mz, l1, l2, l3) AS ( SELECT '001L', '01', '02', '20190813', 5, 1.0, 1.1, 2.1, 1, 11, 111 FROM dual UNION ALL SELECT '001R', '01', '02', '20190813', 5, 1.5, 1.2, 2.2, 2, 22, 222 FROM dual UNION ALL SELECT '001L', '01', '02', '20190813', 4, 1.2, 1.3, 2.3, 3, 33, 333 FROM dual UNION ALL SELECT '001L', '01', '02', '20190813', 3, 1.0, 1.4, 2.4, 4, 44, 444 FROM dual UNION ALL SELECT '001R', '01', '02', '20190813', 3, 1.3, 1.5, 2.5, 5, 55, 555 FROM dual UNION ALL SELECT '001L', '01', '02', '20190726', 2, 1.4, 1.6, 2.6, 6, 66, 666 FROM dual UNION ALL SELECT '001R', '01', '02', '20190726', 2, 1.5, 1.7, 2.7, 7, 77, 777 FROM dual UNION ALL SELECT '001L', '01', '02', '20190726', 1, 1.6, 1.8, 2.8, 8, 88, 888 FROM dual UNION ALL SELECT '001R', '01', '02', '20190726', 1, 1.7, 1.9, 2.9, 9, 99, 999 FROM dual ) SELECT * FROM (SELECT SUBSTR(code, 4, 1) gbn , DENSE_RANK() OVER(ORDER BY m_dt DESC, m_seq DESC) dr , mx x , my y , mz z FROM t1 WHERE code1 = '01' AND code2 = '02' AND code IN ('001L', '001R') ) UNPIVOT (v FOR m_gbn IN (x, y, z)) PIVOT (MIN(v) FOR dr IN (1 dt1, 2 dt2, 3 dt3, 4 dt4, 5 dt5, 6 dt6)) ORDER BY gbn, m_gbn ;
기존 질문에 있던 l_val 포함.
WITH t1(code, code1, code2, m_dt, m_seq, mx, my, mz, l1, l2, l3) AS ( SELECT '001L', '01', '02', '20190813', 5, 1.0, 1.1, 2.1, 1, 11, 111 FROM dual UNION ALL SELECT '001R', '01', '02', '20190813', 5, 1.5, 1.2, 2.2, 2, 22, 222 FROM dual UNION ALL SELECT '001L', '01', '02', '20190813', 4, 1.2, 1.3, 2.3, 3, 33, 333 FROM dual UNION ALL SELECT '001L', '01', '02', '20190813', 3, 1.0, 1.4, 2.4, 4, 44, 444 FROM dual UNION ALL SELECT '001R', '01', '02', '20190813', 3, 1.3, 1.5, 2.5, 5, 55, 555 FROM dual UNION ALL SELECT '001L', '01', '02', '20190726', 2, 1.4, 1.6, 2.6, 6, 66, 666 FROM dual UNION ALL SELECT '001R', '01', '02', '20190726', 2, 1.5, 1.7, 2.7, 7, 77, 777 FROM dual UNION ALL SELECT '001L', '01', '02', '20190726', 1, 1.6, 1.8, 2.8, 8, 88, 888 FROM dual UNION ALL SELECT '001R', '01', '02', '20190726', 1, 1.7, 1.9, 2.9, 9, 99, 999 FROM dual ) SELECT gbn , NVL("1_L", 0) l_val , m_gbn , NVL("1_M", 0) dt1 , NVL("2_M", 0) dt2 , NVL("3_M", 0) dt3 , NVL("4_M", 0) dt4 , NVL("5_M", 0) dt5 , NVL("6_M", 0) dt6 FROM (SELECT SUBSTR(code, 4, 1) gbn , DENSE_RANK() OVER(ORDER BY m_dt DESC, m_seq DESC) dr , mx, my, mz , l1, l2, l3 FROM t1 WHERE code1 = '01' AND code2 = '02' AND code IN ('001L', '001R') ) UNPIVOT ((m, l) FOR m_gbn IN ((mx, l1) AS 'X', (my, l2) AS 'Y', (mz, l3) AS 'Z')) PIVOT (MIN(m) m, MIN(l) l FOR dr IN (1, 2, 3, 4, 5, 6)) ORDER BY gbn, m_gbn ;