합계 항목을 표시할때 svc_ttl : seq, svc_cd 한자리별 합 seq_ttl : seq 별 합 다만, 위의 기준별 마지막 row에서만 합계 표시를 하고 싶습니다. 현재는 모든 row에서 합이 표기되다 보니 그룹별 합을 계산할때 중복표기가 되어서 전체 합을 계산할때 불편한 형태입니다. rollup이나 cube 형태도 상관은 없는데 svc_cd 가 두자리로 표시되면서 합은 한자리 기준으로 표시되어야만 합니다. * 원하는 결과값
SEQ | SVC_CD | QTY | SVC_TTL | SEQ_TTL |
1 | 15 | 1 | ||
1 | 17 | 2 | 3 | |
1 | 25 | 1 | ||
1 | 27 | 1 | 2 | |
1 | 35 | 2 | ||
1 | 37 | 1 | 3 | 8 |
2 | 35 | 2 | ||
2 | 37 | 2 | 4 | 4 |
=== sample query === SELECT seq ,svc_cd ,qty ,sum(qty) over (partition by seq, SUBSTR(svc_cd, 1,1)) svc_ttl ,sum(qty) over (partition by seq) seq_ttl FROM ( SELECT 1 seq, 15 svc_cd, 1 qty FROM dual union all SELECT 1 seq, 17 svc_cd, 2 qty FROM dual union all SELECT 1 seq, 25 svc_cd, 1 qty FROM dual union all SELECT 1 seq, 27 svc_cd, 1 qty FROM dual union all SELECT 1 seq, 35 svc_cd, 2 qty FROM dual union all SELECT 1 seq, 37 svc_cd, 1 qty FROM dual union all SELECT 2 seq, 35 svc_cd, 2 qty FROM dual union all SELECT 2 seq, 37 svc_cd, 2 qty FROM dual)
WITH t AS ( SELECT 1 seq, 15 svc_cd, 1 qty FROM dual UNION ALL SELECT 1, 17, 2 FROM dual UNION ALL SELECT 1, 25, 1 FROM dual UNION ALL SELECT 1, 27, 1 FROM dual UNION ALL SELECT 1, 35, 2 FROM dual UNION ALL SELECT 1, 37, 1 FROM dual UNION ALL SELECT 2, 35, 2 FROM dual UNION ALL SELECT 2, 37, 2 FROM dual ) -- 1. 요청 포멧, 합계를 옆으로(분석함수) -- SELECT seq , svc_cd , qty , DECODE(1 , ROW_NUMBER() OVER(PARTITION BY seq, SUBSTR(svc_cd, 1, 1) ORDER BY svc_cd DESC) , SUM(qty) OVER(PARTITION BY seq, SUBSTR(svc_cd, 1, 1)) ) svc_ttl , DECODE(1 , ROW_NUMBER() OVER(PARTITION BY seq ORDER BY svc_cd DESC) , SUM(qty) OVER(PARTITION BY seq) ) seq_ttl FROM t ORDER BY seq, svc_cd ; WITH t AS ( SELECT 1 seq, 15 svc_cd, 1 qty FROM dual UNION ALL SELECT 1, 17, 2 FROM dual UNION ALL SELECT 1, 25, 1 FROM dual UNION ALL SELECT 1, 27, 1 FROM dual UNION ALL SELECT 1, 35, 2 FROM dual UNION ALL SELECT 1, 37, 1 FROM dual UNION ALL SELECT 2, 35, 2 FROM dual UNION ALL SELECT 2, 37, 2 FROM dual ) -- 2. 다른 포멧, 합계를 아래로(롤업) -- SELECT seq , DECODE(GROUPING_ID(SUBSTR(svc_cd, 1, 1), svc_cd) , 3, '합계', 1, '소계', svc_cd) svc_cd , SUM(qty) qty FROM t a GROUP BY seq, ROLLUP(SUBSTR(svc_cd, 1, 1), svc_cd) ORDER BY seq, SUBSTR(a.svc_cd, 1, 1), a.svc_cd ;