안녕하세요. 전년대비 증감율을 구현하고 싶은데, 방법을 잘 모르겠어요.
WITH TAB1
AS ( SELECT '계획' AS GBN, '2016' AS YYYY, 50000 AS AMT FROM DUAL
UNION ALL
SELECT '계획' AS GBN, '2015' AS YYYY, 60000 AS AMT FROM DUAL
UNION ALL
SELECT '계획' AS GBN, '2016' AS YYYY, 30000 AS AMT FROM DUAL
UNION ALL
SELECT '계획' AS GBN, '2015' AS YYYY, 20000 AS AMT FROM DUAL
UNION
SELECT '실적' AS GBN, '2016' AS YYYY, 70000 AS AMT FROM DUAL
UNION ALL
SELECT '실적' AS GBN, '2015' AS YYYY, 80000 AS AMT FROM DUAL
)
SELECT NVL(GBN, '총계'), NVL(YYYY, '소계'), SUM(AMT)
FROM TAB1
GROUP BY ROLLUP(GBN,YYYY)
NVL(GBN,'총계') | NVL(YYYY,'소계') | SUM(AMT) |
계획 | 2015 | 80000 |
계획 | 2016 | 80000 |
계획 | 소계 | 160000 |
실적 | 2015 | 80000 |
실적 | 2016 | 70000 |
실적 | 소계 | 150000 |
총계 | 소계 | 310000 |
계획 | 전년대비 증감 | 0 |
실적 | 전년대비 증감 | -10000 |
제가 작성한 쿼리로는 총계까지만 나오는데, 총계 아래에 항목별 전년대비 증감액을 보여주고 싶은데
어떻게 해야 될지를 모르겟어요.
쿼리문을 작성을 부탁드립니다.
--10g MODEL문 활용 WITH TAB1 AS ( SELECT '계획' AS GBN, '2016' AS YYYY, 50000 AS AMT FROM DUAL UNION ALL SELECT '계획' AS GBN, '2015' AS YYYY, 60000 AS AMT FROM DUAL UNION ALL SELECT '계획' AS GBN, '2016' AS YYYY, 30000 AS AMT FROM DUAL UNION ALL SELECT '계획' AS GBN, '2015' AS YYYY, 20000 AS AMT FROM DUAL UNION SELECT '실적' AS GBN, '2016' AS YYYY, 70000 AS AMT FROM DUAL UNION ALL SELECT '실적' AS GBN, '2015' AS YYYY, 80000 AS AMT FROM DUAL ) SELECT GBN, SUBTOT, TOTS FROM TAB1 GROUP BY ROLLUP(GBN, YYYY) MODEL DIMENSION BY(NVL(GBN, '총계') GBN, NVL(YYYY, '소계') SUBTOT, 1 NO) MEASURES(SUM(AMT) TOTS) RULES (TOTS ['계획', '증감', 2] = TOTS['계획', '2016', 1] - TOTS['계획', '2015', 1] , TOTS ['실적', '증감', 2] = TOTS['실적', '2016', 1] - TOTS['실적', '2015', 1]) ORDER BY NO, GBN, SUBTOT
--WITH문 활용 WITH TAB1 AS ( SELECT '계획' AS GBN, '2016' AS YYYY, 50000 AS AMT FROM DUAL UNION ALL SELECT '계획' AS GBN, '2015' AS YYYY, 60000 AS AMT FROM DUAL UNION ALL SELECT '계획' AS GBN, '2016' AS YYYY, 30000 AS AMT FROM DUAL UNION ALL SELECT '계획' AS GBN, '2015' AS YYYY, 20000 AS AMT FROM DUAL UNION SELECT '실적' AS GBN, '2016' AS YYYY, 70000 AS AMT FROM DUAL UNION ALL SELECT '실적' AS GBN, '2015' AS YYYY, 80000 AS AMT FROM DUAL ) , T AS ( SELECT NVL(GBN, '총계') gbn, NVL(YYYY, '소계') subtot, SUM(AMT) tot FROM TAB1 GROUP BY ROLLUP(GBN,YYYY) ) SELECT * FROM T UNION ALL SELECT GBN, '증감', SUM(DECODE(SUBTOT, '2015', -TOT, '2016', TOT)) FROM T WHERE GBN IN ('계획', '실적') GROUP BY GBN
전년대비 증감율에 대한 쿼리문을 도움 받았는데도, 막상 적용하려니까 잘 모르겠어요. 염치없이 다시 질문을 드립니다. WITH TAB1 AS ( SELECT '2015' AS YYYY, '서울' AS PROD, 189605 AS QTY FROM DUAL UNION ALL SELECT '2015' AS YYYY, '경기' AS PROD, 211392 AS QTY FROM DUAL UNION ALL SELECT '2015' AS YYYY, '부산' AS PROD, 618721 AS QTY FROM DUAL UNION ALL SELECT '2015' AS YYYY, '대구' AS PROD, 86968 AS QTY FROM DUAL UNION ALL SELECT '2015' AS YYYY, '인천' AS PROD, 29825 AS QTY FROM DUAL UNION ALL SELECT '2015' AS YYYY, '광주' AS PROD, 49810 AS QTY FROM DUAL UNION ALL SELECT '2015' AS YYYY, '대전' AS PROD, 144698 AS QTY FROM DUAL UNION ALL SELECT '2015' AS YYYY, '울산' AS PROD, 307855 AS QTY FROM DUAL UNION ALL SELECT '2016' AS YYYY, '서울' AS PROD, 376221 AS QTY FROM DUAL UNION ALL SELECT '2016' AS YYYY, '경기' AS PROD, 223522 AS QTY FROM DUAL UNION ALL SELECT '2016' AS YYYY, '부산' AS PROD, 430019 AS QTY FROM DUAL UNION ALL SELECT '2016' AS YYYY, '대구' AS PROD, 180503 AS QTY FROM DUAL UNION ALL SELECT '2016' AS YYYY, '인천' AS PROD, 32931 AS QTY FROM DUAL UNION ALL SELECT '2016' AS YYYY, '광주' AS PROD, 149506 AS QTY FROM DUAL UNION ALL SELECT '2016' AS YYYY, '대전' AS PROD, 221965 AS QTY FROM DUAL UNION ALL SELECT '2016' AS YYYY, '울산' AS PROD, 260988 AS QTY FROM DUAL UNION ALL SELECT '2016' AS YYYY, '충청' AS PROD, 0 AS QTY FROM DUAL ) SELECT YYYY, PROD, QTY FROM TAB1 ;
YY | PROD | QTY |
2015 | 서울 | 189,605 |
2015 | 경기 | 211,392 |
2015 | 부산 | 618,721 |
2015 | 대구 | 86,968 |
2015 | 인천 | 29,825 |
2015 | 광주 | 49,810 |
2015 | 대전 | 144,698 |
2015 | 울산 | 307,855 |
2016 | 서울 | 376,221 |
2016 | 경기 | 223,522 |
2016 | 부산 | 430,019 |
2016 | 대구 | 180,503 |
2016 | 인천 | 32,931 |
2016 | 광주 | 149,506 |
2016 | 대전 | 221,965 |
2016 | 울산 | 260,988 |
2016 | 충청 | |
증감액 | 서울 | 186,616 |
증감액 | 경기 | 12,130 |
증감액 | 부산 | - 188,702 |
증감액 | 대구 | 93,535 |
증감액 | 인천 | 3,106 |
증감액 | 광주 | 99,696 |
증감액 | 대전 | 77,267 |
증감액 | 울산 | - 46,867 |
증감액 | 충청 | - |
위와같이 증감액을 보여줘야 하는데..
지역은 다른조건으로 바뀔 수 있습니다.
사용자가 지역별을 선택하면 위와같이 지역별 데이타가 나오고요, 담당별 선택하면 담당별 데이타가 조회가 되도록 만들 예정입니다. (PROD 항목의 값과 갯수는 변할 수 있습니다.)
이런경우, 모델절을 어떻게 만들어야 될지 알려주세요.
아래는 알려주신 쿼리문을 제가 응용한건데요, ROWNUM을 만들고, ROWNUM값을 1부터 맥스까지 하나씩 증가시키려고 했는데 MAX값을 어떻게 표현해야 될지도 모르겠어요.
MODEL
DIMENSION BY (NVL(YY, '증감액') YY, ROWNUM )
MEASURES(QTY, PROD)
IGNORE NAV
RULES (
MM01['증감액', FOR ROWNUM FROM 1 TO MAX(???) INCREMENT 1] = MM01['2016', CV(RN)] - MM01['2015', CV(RN)] )
1. 지역별 전년대비 증감액 쿼리문과
2. MAX(???) 이 부분에 어떤 값을 넣어야 되는지도 좀 부탁드립니다.
하루종일 고민했는데 혼잔 잘 안되네요. 꼭 좀 부탁드려요