여를 들면 우선 쉽게 생각해서
WITH M_DATA
AS
(
SELECT '1' AS CD, '의류' AS NM, 100 AS A, 'AAAA' AS B , 300 AS C FROM DUAL
UNION ALL
SELECT '1' AS CD, '의류' AS NM, 300 AS A, 'BBBB' AS B , 500 AS C FROM DUAL
UNION ALL
SELECT '2' AS CD, '용품' AS NM, 300 AS A, 'AAAA-1' AS B , 500 AS C FROM DUAL
UNION ALL
SELECT '4' AS CD, '캐리어' AS NM, 300 AS A, 'AAAA-3' AS B , 500 AS C FROM DUAL
UNION ALL
SELECT '4' AS CD, '캐리어' AS NM, 800 AS A, 'AAAA-5' AS B , 900 AS C FROM DUAL
UNION ALL
SELECT '4' AS CD, '캐리어' AS NM, 1000 AS A, 'AAAA-7' AS B , 1800 AS C FROM DUAL
UNION ALL
SELECT '7' AS CD, '캐리어구성품' AS NM, 100 AS A, 'BBBB-1' AS B , 800 AS C FROM DUAL
UNION ALL
SELECT '7' AS CD, '캐리어구성품' AS NM, 1300 AS A, 'BBBB-2' AS B , 4800 AS C FROM DUAL
UNION ALL
SELECT '9' AS CD, '기타' AS NM, 1300 AS A, 'CCCC-2' AS B , 4800 AS C FROM DUAL
)
SELECT NEW_GBN
,SUM(A)
,SUM(C)
FROM
(
SELECT CASE WHEN CD IN ('1','9') THEN '1'
WHEN CD IN ('4','7') THEN '4'
ELSE CD
END AS NEW_GBN
,NM
,A
,C
FROM M_DATA
)
WHERE 1=1
GROUP BY NEW_GBN
처음에는
1과 9값을 sum하고
union all
4와 7값을 sum
이런식으로 하려고했는데요
이렇게 간단하게 생각을 했는데 혹시 좀더 좋은방법이 있을까요???
결과값에 대한 이미지도 첨부합니다.
WITH M_DATA AS ( SELECT '1' AS CD, '의류' AS NM, 100 AS A, 'AAAA' AS B , 300 AS C FROM DUAL UNION ALL SELECT '1' AS CD, '의류' AS NM, 300 AS A, 'BBBB' AS B , 500 AS C FROM DUAL UNION ALL SELECT '2' AS CD, '용품' AS NM, 300 AS A, 'AAAA-1' AS B , 500 AS C FROM DUAL UNION ALL SELECT '4' AS CD, '캐리어' AS NM, 300 AS A, 'AAAA-3' AS B , 500 AS C FROM DUAL UNION ALL SELECT '4' AS CD, '캐리어' AS NM, 800 AS A, 'AAAA-5' AS B , 900 AS C FROM DUAL UNION ALL SELECT '4' AS CD, '캐리어' AS NM, 1000 AS A, 'AAAA-7' AS B , 1800 AS C FROM DUAL UNION ALL SELECT '7' AS CD, '캐리어구성품' AS NM, 100 AS A, 'BBBB-1' AS B , 800 AS C FROM DUAL UNION ALL SELECT '7' AS CD, '캐리어구성품' AS NM, 1300 AS A, 'BBBB-2' AS B , 4800 AS C FROM DUAL UNION ALL SELECT '9' AS CD, '기타' AS NM, 1300 AS A, 'CCCC-2' AS B , 4800 AS C FROM DUAL ) SELECT T1.CD, T2.NM, T1.A, T1.C FROM ( SELECT CASE WHEN CD = '9' THEN '1' WHEN CD = '7' THEN '4' ELSE CD END AS CD , SUM(A) AS A, SUM(C) AS C FROM M_DATA GROUP BY CASE WHEN CD = '9' THEN '1' WHEN CD = '7' THEN '4' ELSE CD END ) T1 ,(SELECT DISTINCT CD, NM FROM M_DATA) AS T2 WHERE T1.CD = T2.CD
급하게 테스트 한거라 오타가 있을 수도 있습니다.
-- 인라인뷰 미사용 -- SELECT DECODE(cd, '9', '1', '7', '4', cd) cd , DECODE(cd, '9', '의류', '7', '캐리어', nm) nm , SUM(a) a , SUM(c) c FROM m_data GROUP BY DECODE(cd, '9', '1', '7', '4', cd) , DECODE(cd, '9', '의류', '7', '캐리어', nm) ORDER BY cd ; -- 인라인뷰 사용 -- SELECT cd , nm , SUM(a) a , SUM(c) c FROM (SELECT DECODE(cd, '9', '1', '7', '4', cd) cd , DECODE(cd, '9', '의류', '7', '캐리어', nm) nm , a , c FROM m_data ) GROUP BY cd, nm ORDER BY cd ;
WITH m_data AS ( SELECT '1' cd, '의류' nm, 100 a, 'AAAA' b, 300 c FROM dual UNION ALL SELECT '1', '의류' , 300, 'BBBB' , 500 FROM dual UNION ALL SELECT '2', '용품' , 300, 'AAAA-1', 500 FROM dual UNION ALL SELECT '4', '캐리어' , 300, 'AAAA-3', 500 FROM dual UNION ALL SELECT '4', '캐리어' , 800, 'AAAA-5', 900 FROM dual UNION ALL SELECT '4', '캐리어' , 1000, 'AAAA-7', 1800 FROM dual UNION ALL SELECT '7', '캐리어구성품', 100, 'BBBB-1', 800 FROM dual UNION ALL SELECT '7', '캐리어구성품', 1300, 'BBBB-2', 4800 FROM dual UNION ALL SELECT '9', '기타' , 1300, 'CCCC-2', 4800 FROM dual ) , m_code AS ( -- 맵핑 코드 테이블 정의 -- SELECT '1' cd, '의류' nm, '1' gb FROM dual UNION ALL SELECT '2', '용품' , '2' FROM dual UNION ALL SELECT '4', '캐리어' , '4' FROM dual UNION ALL SELECT '7', '캐리어구성품', '4' FROM dual UNION ALL SELECT '9', '기타' , '1' FROM dual ) SELECT c.cd , c.nm , SUM(a.a) a , SUM(a.c) c FROM m_data a , m_code b , m_code c WHERE a.cd = b.cd AND b.gb = c.cd GROUP BY c.cd, c.nm ORDER BY cd ;