-- 대략적인 데이타
WITH M_DATA
AS
(
SELECT 'AAAA' AS A
,'A제품' AS B
,'95' AS SIZE_CD
,'338' AS COL_CD
,'10' AS PL_QTY
,'12' AS IN__QTY
FROM DUAL
UNION ALL
SELECT 'AAAA' AS A
,'A제품' AS B
,'100' AS SIZE_CD
,'303' AS COL_CD
,'13' AS PL_QTY
,'14' AS IN__QTY
FROM DUAL
UNION ALL
SELECT 'AAAA' AS A
,'A제품' AS B
,'105' AS SIZE_CD
,'290' AS COL_CD
,'15' AS PL_QTY
,'16' AS IN__QTY
FROM DUAL
UNION ALL
SELECT 'BBB' AS A
,'B제품' AS B
,'100' AS SIZE_CD
,'303' AS COL_CD
,'13' AS PL_QTY
,'14' AS IN__QTY
FROM DUAL
UNION ALL
SELECT 'BBB' AS A
,'B제품' AS B
,'105' AS SIZE_CD -- 사이즈
,'290' AS COL_CD -- 컬러코드
,'15' AS PL_QTY -- 계획수량
,'16' AS IN__QTY -- 입고수량
FROM DUAL
)
SELECT *
FROM M_DATA
원하는 결과 값은 파일로 첨부하겠습니다.
※ 조금 구현하기 힘들다하면 그냥 조회시 프로시져로 데이타 생성해서 그냥 조회하는걸로 할까 생각중입니다.
컬러에 (컬러와 수량구분) 두가지 정보를 표현하고 있는데. 항목을 두개로 나눠야 하지 않나요?
95, 100, 105 가 레코드가 아닌 컬럼 타이틀로 빠져야 하지 않나요?
현재 5줄로 표현되는 걸 4줄로 표현해야 할 것 같은데요?
WITH m_data AS ( SELECT 'AAAA' cd, 'A제품' nm, 95 size_cd, 'WHITE' color_cd , 11 pl_qty, 12 in_qty, 13 ss_qty, 14 tt_qty FROM dual UNION ALL SELECT 'AAAA', 'A제품', 100, 'WHITE' , 12, 13, 14, 15 FROM dual UNION ALL SELECT 'AAAA', 'A제품', 105, 'WHITE' , 16, 17, 18, 19 FROM dual UNION ALL SELECT 'AAAA', 'A제품', 95, 'STEEL GRAY', 20, 21, 22, 23 FROM dual UNION ALL SELECT 'AAAA', 'A제품', 100, 'STEEL GRAY', 24, 25, 26, 27 FROM dual UNION ALL SELECT 'AAAA', 'A제품', 105, 'STEEL GRAY', 28, 29, 30, 31 FROM dual ) SELECT * FROM m_data UNPIVOT (qty FOR gb IN ( pl_qty AS '기획' , in_qty AS '입고' , ss_qty AS '기간판매' , tt_qty AS '총판매' ) ) PIVOT (MIN(qty) FOR size_cd IN (95, 100, 105)) ORDER BY cd, color_cd , INSTR('기획,입고,기간판매,총판매', gb) ;
억지로 만들어 낸다면 가능은 합니다.
다만 결과표를 해석하기가 어려워지죠.
컬러라는 컬럼에 구분이 함께 표현되고.
사이즈라는 컬럼에 수량이 함께 표현되고.
약간 끼워 맞추기식 결과표가 되는 건 아닌가 우려가 될 뿐입니다.
WITH m_data AS ( SELECT 'AAAA' cd, 'A제품' nm, 95 size_cd, 'WHITE' color_cd , 11 pl_qty, 12 in_qty, 13 ss_qty, 14 tt_qty FROM dual UNION ALL SELECT 'AAAA', 'A제품', 100, 'WHITE' , 12, 13, 14, 15 FROM dual UNION ALL SELECT 'AAAA', 'A제품', 105, 'WHITE' , 16, 17, 18, 19 FROM dual UNION ALL SELECT 'AAAA', 'A제품', 95, 'STEEL GRAY', 20, 21, 22, 23 FROM dual UNION ALL SELECT 'AAAA', 'A제품', 100, 'STEEL GRAY', 24, 25, 26, 27 FROM dual UNION ALL SELECT 'AAAA', 'A제품', 105, 'STEEL GRAY', 28, 29, 30, 31 FROM dual ) SELECT cd, nm , NVL(gb, color_cd) color , NVL2(gb, MIN(DECODE(size_cd, 95, qty)), 95) size_095 , NVL2(gb, MIN(DECODE(size_cd, 100, qty)), 100) size_100 , NVL2(gb, MIN(DECODE(size_cd, 105, qty)), 105) size_105 FROM m_data UNPIVOT (qty FOR gb IN ( pl_qty AS '기획' , in_qty AS '입고' , ss_qty AS '기간판매' , tt_qty AS '총판매' ) ) GROUP BY cd, nm, color_cd, ROLLUP(gb) ORDER BY cd, nm, color_cd , GROUPING(gb) DESC , INSTR('기획,입고,기간판매,총판매', gb) ;
WITH m_data AS ( SELECT 'AAAA' cd, 'A제품' nm, 95 size_cd, 'WHITE' color_cd , 11 pl_qty, 12 in_qty, 13 ss_qty, 14 tt_qty FROM dual UNION ALL SELECT 'AAAA', 'A제품', 100, 'WHITE' , 12, 13, 14, 15 FROM dual UNION ALL SELECT 'AAAA', 'A제품', 105, 'WHITE' , 16, 17, 18, 19 FROM dual UNION ALL SELECT 'AAAA', 'A제품', 95, 'STEEL GRAY', 20, 21, 22, 23 FROM dual UNION ALL SELECT 'AAAA', 'A제품', 100, 'STEEL GRAY', 24, 25, 26, 27 FROM dual UNION ALL SELECT 'AAAA', 'A제품', 105, 'STEEL GRAY', 28, 29, 30, 31 FROM dual ) SELECT cd, nm , NVL(gb, color_cd) color , "95", "100", "105" FROM (SELECT cd, nm, size_cd, color_cd, pl_qty, in_qty, ss_qty, tt_qty , size_cd sz_qty FROM m_data ) UNPIVOT (qty FOR gb IN ( sz_qty AS '' , pl_qty AS '기획' , in_qty AS '입고' , ss_qty AS '기간판매' , tt_qty AS '총판매' ) ) PIVOT (MIN(qty) FOR size_cd IN (95, 100, 105)) ORDER BY cd, color_cd , INSTR('기획,입고,기간판매,총판매', gb) NULLS FIRST ;
컬럼 개수 까지는 가변으로 안됩니다. 고정 개수 라고 하면 가능 할 수 있습니다.
WITH m_data AS ( SELECT 'AAAA' cd, 'A제품' nm, 95 size_cd, 'WHITE' color_cd , 11 pl_qty, 12 in_qty, 13 ss_qty, 14 tt_qty FROM dual UNION ALL SELECT 'AAAA', 'A제품', 100, 'WHITE' , 12, 13, 14, 15 FROM dual UNION ALL SELECT 'AAAA', 'A제품', 105, 'WHITE' , 16, 17, 18, 19 FROM dual UNION ALL SELECT 'AAAA', 'A제품', 100, 'STEEL GRAY', 20, 21, 22, 23 FROM dual UNION ALL SELECT 'AAAA', 'A제품', 105, 'STEEL GRAY', 24, 25, 26, 27 FROM dual UNION ALL SELECT 'AAAA', 'A제품', 110, 'STEEL GRAY', 28, 29, 30, 31 FROM dual ) SELECT cd, nm , NVL(gb, color_cd) color , size_1, size_2, size_3 FROM (SELECT cd, nm, color_cd , ROW_NUMBER() OVER(PARTITION BY cd, color_cd ORDER BY size_cd) rn , size_cd sz_qty , pl_qty, in_qty, ss_qty, tt_qty FROM m_data ) UNPIVOT (qty FOR gb IN ( sz_qty AS '' , pl_qty AS '기획' , in_qty AS '입고' , ss_qty AS '기간판매' , tt_qty AS '총판매' ) ) PIVOT (MIN(qty) FOR rn IN (1 size_1, 2 size_2, 3 size_3)) ORDER BY cd, color_cd , INSTR('기획,입고,기간판매,총판매', gb) NULLS FIRST ;