rollup 시 row4개단위로 sum을 구할수가 있나요? 0 5 2,273

by 김용한 [SQL Query] rollup [2017.07.14 15:23:08]


원쿼리는 아래와같구요

WITH T AS (
SELECT 'MODEL01' MODEL, 'LINE01' LINE, 'MCODE01' MCODE, 'SCODE01' SCODE, 1 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
SELECT 'MODEL01' MODEL, 'LINE01' LINE, 'MCODE01' MCODE, 'SCODE02' SCODE, 2 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
SELECT 'MODEL01' MODEL, 'LINE01' LINE, 'MCODE02' MCODE, 'SCODE03' SCODE, 3 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
SELECT 'MODEL01' MODEL, 'LINE01' LINE, 'MCODE02' MCODE, 'SCODE04' SCODE, 4 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
SELECT 'MODEL01' MODEL, 'LINE02' LINE, 'MCODE01' MCODE, 'SCODE01' SCODE, 11 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
SELECT 'MODEL01' MODEL, 'LINE02' LINE, 'MCODE01' MCODE, 'SCODE02' SCODE, 22 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
SELECT 'MODEL01' MODEL, 'LINE02' LINE, 'MCODE02' MCODE, 'SCODE03' SCODE, 33 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
SELECT 'MODEL01' MODEL, 'LINE02' LINE, 'MCODE02' MCODE, 'SCODE04' SCODE, 44 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
SELECT 'MODEL01' MODEL, 'LINE03' LINE, 'MCODE01' MCODE, 'SCODE01' SCODE, 3 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
SELECT 'MODEL01' MODEL, 'LINE03' LINE, 'MCODE01' MCODE, 'SCODE02' SCODE, 6 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
SELECT 'MODEL01' MODEL, 'LINE03' LINE, 'MCODE02' MCODE, 'SCODE03' SCODE, 9 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
SELECT 'MODEL01' MODEL, 'LINE03' LINE, 'MCODE02' MCODE, 'SCODE04' SCODE, 4 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
SELECT 'MODEL02' MODEL, 'LINE01' LINE, 'MCODE01' MCODE, 'SCODE01' SCODE, 33 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
SELECT 'MODEL02' MODEL, 'LINE01' LINE, 'MCODE01' MCODE, 'SCODE02' SCODE, 66 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
SELECT 'MODEL02' MODEL, 'LINE01' LINE, 'MCODE02' MCODE, 'SCODE03' SCODE, 99 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
SELECT 'MODEL02' MODEL, 'LINE01' LINE, 'MCODE02' MCODE, 'SCODE04' SCODE, 55 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
SELECT 'MODEL03' MODEL, 'LINE01' LINE, 'MCODE01' MCODE, 'SCODE01' SCODE, 7 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
SELECT 'MODEL03' MODEL, 'LINE01' LINE, 'MCODE01' MCODE, 'SCODE02' SCODE, 8 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
SELECT 'MODEL03' MODEL, 'LINE01' LINE, 'MCODE02' MCODE, 'SCODE03' SCODE, 5 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
SELECT 'MODEL03' MODEL, 'LINE01' LINE, 'MCODE02' MCODE, 'SCODE04' SCODE, 1 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL
)
SELECT T.*
  FROM T
 ORDER BY
       T.MODEL
      ,T.LINE
      ,T.MCODE
      ,T.SCODE

 

MODEL LINE MCODE SCODE VAL1 VAL2 VAL3 VAL4 VAL5
MODEL01 LINE01 MCODE01 SCODE01 1 0 0 0 0
MODEL01 LINE01 MCODE01 SCODE02 2 0 0 0 0
MODEL01 LINE01 MCODE02 SCODE03 3 0 0 0 0
MODEL01 LINE01 MCODE02 SCODE04 4 0 0 0 0
MODEL01 LINE02 MCODE01 SCODE01 11 0 0 0 0
MODEL01 LINE02 MCODE01 SCODE02 22 0 0 0 0
MODEL01 LINE02 MCODE02 SCODE03 33 0 0 0 0
MODEL01 LINE02 MCODE02 SCODE04 44 0 0 0 0
MODEL01 LINE03 MCODE01 SCODE01 3 0 0 0 0
MODEL01 LINE03 MCODE01 SCODE02 6 0 0 0 0
MODEL01 LINE03 MCODE02 SCODE03 9 0 0 0 0
MODEL01 LINE03 MCODE02 SCODE04 4 0 0 0 0
MODEL02 LINE01 MCODE01 SCODE01 33 0 0 0 0
MODEL02 LINE01 MCODE01 SCODE02 66 0 0 0 0
MODEL02 LINE01 MCODE02 SCODE03 99 0 0 0 0
MODEL02 LINE01 MCODE02 SCODE04 55 0 0 0 0
MODEL03 LINE01 MCODE01 SCODE01 7 0 0 0 0
MODEL03 LINE01 MCODE01 SCODE02 8 0 0 0 0
MODEL03 LINE01 MCODE02 SCODE03 5 0 0 0 0
MODEL03 LINE01 MCODE02 SCODE04 1 0 0 0 0

 

원데이터가 위와같고

 

MODEL LINE MCODE SCODE VAL1 VAL2 VAL3 VAL4 VAL5
총계 총계 총계 SCODE01 55 0 0 0 0
총계 총계 총계 SCODE02 104 0 0 0 0
총계 총계 총계 SCODE03 149 0 0 0 0
총계 총계 총계 SCODE04 108 0 0 0 0
MODEL01 소계 소계 SCODE01 15 0 0 0 0
MODEL01 소계 소계 SCODE02 30 0 0 0 0
MODEL01 소계 소계 SCODE03 45 0 0 0 0
MODEL01 소계 소계 SCODE04 52 0 0 0 0
MODEL01 LINE01 MCODE01 SCODE01 1 0 0 0 0
MODEL01 LINE01 MCODE01 SCODE02 2 0 0 0 0
MODEL01 LINE01 MCODE02 SCODE03 3 0 0 0 0
MODEL01 LINE01 MCODE02 SCODE04 4 0 0 0 0
MODEL01 LINE02 MCODE01 SCODE01 11 0 0 0 0
MODEL01 LINE02 MCODE01 SCODE02 22 0 0 0 0
MODEL01 LINE02 MCODE02 SCODE03 33 0 0 0 0
MODEL01 LINE02 MCODE02 SCODE04 44 0 0 0 0
MODEL01 LINE03 MCODE01 SCODE01 3 0 0 0 0
MODEL01 LINE03 MCODE01 SCODE02 6 0 0 0 0
MODEL01 LINE03 MCODE02 SCODE03 9 0 0 0 0
MODEL01 LINE03 MCODE02 SCODE04 4 0 0 0 0
MODEL02 소계 소계 SCODE01 33 0 0 0 0
MODEL02 소계 소계 SCODE02 66 0 0 0 0
MODEL02 소계 소계 SCODE03 99 0 0 0 0
MODEL02 소계 소계 SCODE04 55 0 0 0 0
MODEL02 LINE01 MCODE01 SCODE01 33 0 0 0 0
MODEL02 LINE01 MCODE01 SCODE02 66 0 0 0 0
MODEL02 LINE01 MCODE02 SCODE03 99 0 0 0 0
MODEL02 LINE01 MCODE02 SCODE04 55 0 0 0 0
MODEL03 소계 소계 SCODE01 7 0 0 0 0
MODEL03 소계 소계 SCODE02 8 0 0 0 0
MODEL03 소계 소계 SCODE03 5 0 0 0 0
MODEL03 소계 소계 SCODE04 1 0 0 0 0
MODEL03 LINE01 MCODE01 SCODE01 7 0 0 0 0
MODEL03 LINE01 MCODE01 SCODE02 8 0 0 0 0
MODEL03 LINE01 MCODE02 SCODE03 5 0 0 0 0
MODEL03 LINE01 MCODE02 SCODE04 1 0 0 0 0

위와같이 결과가 나와야하는데

4개단위로 sum이 돼나요?

 

rollup하면 1row씩 결과가나와서요 .... ㅠㅠ

by jkson [2017.07.14 16:10:25]

꼭 rollup으로 해야하나요?

WITH T AS (
 SELECT 'MODEL01' MODEL, 'LINE01' LINE, 'MCODE01' MCODE, 'SCODE01' SCODE, 1 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
 SELECT 'MODEL01' MODEL, 'LINE01' LINE, 'MCODE01' MCODE, 'SCODE02' SCODE, 2 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
 SELECT 'MODEL01' MODEL, 'LINE01' LINE, 'MCODE02' MCODE, 'SCODE03' SCODE, 3 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
 SELECT 'MODEL01' MODEL, 'LINE01' LINE, 'MCODE02' MCODE, 'SCODE04' SCODE, 4 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
 SELECT 'MODEL01' MODEL, 'LINE02' LINE, 'MCODE01' MCODE, 'SCODE01' SCODE, 11 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
 SELECT 'MODEL01' MODEL, 'LINE02' LINE, 'MCODE01' MCODE, 'SCODE02' SCODE, 22 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
 SELECT 'MODEL01' MODEL, 'LINE02' LINE, 'MCODE02' MCODE, 'SCODE03' SCODE, 33 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
 SELECT 'MODEL01' MODEL, 'LINE02' LINE, 'MCODE02' MCODE, 'SCODE04' SCODE, 44 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
 SELECT 'MODEL01' MODEL, 'LINE03' LINE, 'MCODE01' MCODE, 'SCODE01' SCODE, 3 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
 SELECT 'MODEL01' MODEL, 'LINE03' LINE, 'MCODE01' MCODE, 'SCODE02' SCODE, 6 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
 SELECT 'MODEL01' MODEL, 'LINE03' LINE, 'MCODE02' MCODE, 'SCODE03' SCODE, 9 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
 SELECT 'MODEL01' MODEL, 'LINE03' LINE, 'MCODE02' MCODE, 'SCODE04' SCODE, 4 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
 SELECT 'MODEL02' MODEL, 'LINE01' LINE, 'MCODE01' MCODE, 'SCODE01' SCODE, 33 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
 SELECT 'MODEL02' MODEL, 'LINE01' LINE, 'MCODE01' MCODE, 'SCODE02' SCODE, 66 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
 SELECT 'MODEL02' MODEL, 'LINE01' LINE, 'MCODE02' MCODE, 'SCODE03' SCODE, 99 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
 SELECT 'MODEL02' MODEL, 'LINE01' LINE, 'MCODE02' MCODE, 'SCODE04' SCODE, 55 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
 SELECT 'MODEL03' MODEL, 'LINE01' LINE, 'MCODE01' MCODE, 'SCODE01' SCODE, 7 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
 SELECT 'MODEL03' MODEL, 'LINE01' LINE, 'MCODE01' MCODE, 'SCODE02' SCODE, 8 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
 SELECT 'MODEL03' MODEL, 'LINE01' LINE, 'MCODE02' MCODE, 'SCODE03' SCODE, 5 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL UNION ALL
 SELECT 'MODEL03' MODEL, 'LINE01' LINE, 'MCODE02' MCODE, 'SCODE04' SCODE, 1 VAL1, 0 VAL2, 0 VAL3, 0 VAL4, 0 VAL5 FROM DUAL
 )
SELECT NVL(MODEL,'총계') MODELCD
     , NVL(LINE,NVL2(MODEL,'소계','총계')) LINECD 
     , NVL(MCODE,NVL2(MODEL,'소계','총계')) MCODECD
     , SCODE, SUM(VAL1) VAL1, SUM(VAL2) VAL2, SUM(VAL3) VAL3, SUM(VAL4) VAL4, SUM(VAL5) VAL5 
  FROM T
 GROUP BY GROUPING SETS(SCODE, (MODEL, SCODE),(MODEL, LINE, MCODE, SCODE))
 ORDER BY MODEL NULLS FIRST, LINE NULLS FIRST, MCODE, SCODE

 


by jkson [2017.07.14 16:26:54]
SELECT NVL(MODEL,'총계') MODELCD
, NVL(LINE,NVL2(MODEL,'소계','총계')) LINECD 
, NVL(MCODE,NVL2(MODEL,'소계','총계')) MCODECD
, SCODE, SUM(VAL1) VAL1, SUM(VAL2) VAL2, SUM(VAL3) VAL3, SUM(VAL4) VAL4, SUM(VAL5) VAL5
FROM T
GROUP BY ROLLUP(SCODE, MODEL, (LINE, MCODE))
HAVING GROUPING_ID(MODEL, LINE, MCODE, SCODE) != 15 
ORDER BY MODEL NULLS FIRST, LINE NULLS FIRST, MCODE, SCODE

아.. 막연히 어렵게 생각했네요. rollup이 더 쉽네요;


by 마농 [2017.07.14 16:33:58]

롤업 대상과 비대상 항목을 구별할 줄 알아야 하구요. scode 는 롤업에서 제외해야 하구요.
롤업 항목중 괄호로 묶어줘야 할 항목이 있는지 파악하셔야 합니다. (line, mcode)
마지막으로 출력 순서와, 출력값 조정 등을 잘 하시면 됩니다.

SELECT DECODE(1, GROUPING(model), '총계', model) model
     , DECODE(1, GROUPING(model), '총계', GROUPING(line), '소계', line) line
     , DECODE(1, GROUPING(model), '총계', GROUPING(line), '소계', mcode) mcode
     , scode
     , SUM(val1) val1
     , SUM(val2) val2
     , SUM(val3) val3
     , SUM(val4) val4
     , SUM(val5) val5
     , GROUPING(model)
  FROM t a
 GROUP BY ROLLUP(model, (line, mcode)), scode
 ORDER BY a.model NULLS FIRST
        , a.line  NULLS FIRST
        , a.mcode NULLS FIRST
        , a.scode
;

 


by jkson [2017.07.14 16:41:42]

아~ scode를 빼면 되는군요.

rollup은 헷갈려요.


by 김용한 [2017.07.15 14:24:34]

rollup의 사용법에 대해 더공부해야겠군요 답변감사드립니다 ~~

 

SELECT DECODE(1, GROUPING(model), '총계', model) model
     , DECODE(1, GROUPING(model), '총계', GROUPING(line), '소계', line) line
     , DECODE(1, GROUPING(model), '총계', GROUPING(line), '소계', mcode) mcode
     , scode
     , SUM(val1) val1
     , SUM(val2) val2
     , SUM(val3) val3
     , SUM(val4) val4
     , SUM(val5) val5
     , GROUPING(model)
     , DENSE_RANK() OVER (ORDER BY a.model NULLS FIRST
        , a.line  NULLS FIRST
        , a.mcode NULLS FIRST) GR /* MODEL, LINE 별 순서 */ 필요해서추가했습니다 ^^
  FROM t a
 GROUP BY ROLLUP(model, (line, mcode)), scode
 ORDER BY a.model NULLS FIRST
        , a.line  NULLS FIRST
        , a.mcode NULLS FIRST
        , a.scode

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입