--현재 이렇게 쿼리 작성해 본 상태입니다 select max(case when grsc = 0 then a.rscnm else 'total' end) as rscnm , max(case when grsc = 0 then a.spec end) as spec , sum(b.previousyearsum) as previousyearsum , sum(b.previousmonsum) as previousmonsum , sum(b.mon) as mon , sum(b.monsum) as monsum , sum(b.total) as totalsum from edu_rsc a , ( select rsccd , grouping(rsccd) as grsc , nvl(sum(previousyear), 0) as previousyearsum --전년누계 , nvl(sum(previousmon), 0) as previousmonsum --전월누계 , nvl(sum(mon), 0) as mon --당월 , nvl(sum(previousmon + mon), 0) as monsum --당월누계 , nvl(sum(previousyear + previousmon + mon), 0) as total --총누계 from ( select rsccd , nvl(case when whyearmon < substr(:param, 1, 4) then qty * unitcost end, 0) as previousyear -- 전년 , nvl(case when whyearmon > substr(:param, 1, 4) and whyearmon < :param then qty * unitcost end, 0) as previousmon -- 전월 , nvl(case when whyearmon = :param then qty * unitcost end, 0) as mon -- 당월 from edu_warehousing where whyearmon <= :param ) b group by rsccd --그룹핑 ) b where a.rsccd = b.rsccd --조인 group by rollup(b.rsccd) order by b.rsccd ; --정렬 --그런데 결과값에서 합계행의 이름(rscnm)이 total 이 아니라 흰페인트, spec은 적색으로 나오네요 --어느 부분이 잘못된건가요?
SELECT rsccd , NVL(CASE WHEN whyearmon < SUBSTR(:param, 1, 4) THEN qty * unitcost END, 0) AS previousyear -- 전년 , NVL(CASE WHEN whyearmon > SUBSTR(:param, 1, 4) AND whyearmon < :param THEN qty * unitcost END, 0) AS previousmon -- 전월 , NVL(CASE WHEN whyearmon = :param THEN qty * unitcost END, 0) AS mon -- 당월
-- 안에서 Grouping 해봐야 소용 없습니다. 롤업 하는데서 그룹핑 하셔야죠. -- rscnm과 spec을 Group By 에 포함시켜 Max 를 제거하세요. -- 이 때 Rollup 항목을 괄호로 묶어주는것이 포인트. SELECT CASE WHEN GROUPING(a.rsccd) = 0 THEN a.rscnm ELSE 'total' END AS rscnm , a.spec FROM a , (...여기서 Grouping 해봐야 아무 소용 없음...) b WHERE a.rsccd = b.rsccd GROUP BY ROLLUP((a.rsccd, a.rscnm, a.spec)) -- 괄호로 묶어줍니다. ORDER BY a.rsccd ;
--마농님 덕분에 아래처럼 쿼리문 작성해서 해결됐습니다 select case grouping(a.rsccd) when 0 then max(a.rscnm) else 'total' end as rscnm --그룹핑 , case grouping(a.rsccd) when 0 then max(a.spec) end as spec , nvl(sum(b.previousyear), 0) as previousyearsum --전년누계 , nvl(sum(b.previousmon), 0) as previousmonsum --전월누계 , nvl(sum(b.mon), 0) as mon --당월 , sum(nvl(b.previousmon, 0) + nvl(b.mon, 0)) as monsum --당월누계 , sum(nvl(b.previousyear, 0) + nvl(b.previousmon, 0) + nvl(b.mon, 0)) as total --총누계 from edu_rsc a , ( select rsccd , sum(case when whyearmon < substr(:param, 1, 4) then qty * unitcost end) as previousyear -- 전년 , sum(case when whyearmon > substr(:param, 1, 4) and whyearmon < :param then qty * unitcost end) as previousmon -- 전월 , sum(case when whyearmon = :param then qty * unitcost end) as mon -- 당월 from edu_warehousing where whyearmon <= :param group by rsccd ) b where a.rsccd = b.rsccd group by rollup(a.rsccd) order by a.rsccd; --롤업에는 a.rsccd 하나만 넣으라고해서 위처럼 수정했습니다 --union을 이용해 인라인뷰로 쿼리문 작성하는 부분은 위 쿼리문 맨 마지막 2줄 rollup과 order by절에 union을 적용하면 된다고 하는데 이해가 안됩니다 --지난번에 알려주신 'UNION ALL 은 두개 쿼리 만들어 합치면 됩니다. 1개는 GROUP BY b.rsccd 로 SUM 하고 나머지 하나는 GROUP BY 없이 SUM 하시면 됩니다.' 라는 부분 조금만 더 자세히 설명해주실 수 있으신지요?
-- 출제자의 의도와 다를수 있습니다만... 일단 UNION 하는 방식으로 -- 1. rsccd별 합계 -- SELECT a.rsccd , a.rscnm , a.sect , b.previousyear AS previousyearsum -- 전년누계 , b.previousmon AS previousmonsum -- 전월누계 , b.mon AS mon -- 당월 , b.previousmon + b.mon AS monsum -- 당월누계 , b.previousyear + b.previousmon + b.mon AS total -- 총누계 FROM edu_rsc a , (SELECT rsccd , NVL(SUM(CASE WHEN whyearmon < SUBSTR(:param, 1, 4) THEN qty * unitcost END), 0) AS previousyear -- 전년 , NVL(SUM(CASE WHEN whyearmon > SUBSTR(:param, 1, 4) AND whyearmon < :param THEN qty * unitcost END), 0) AS previousmon -- 전월 , NVL(SUM(CASE WHEN whyearmon = :param THEN qty * unitcost END), 0) AS mon -- 당월 FROM edu_warehousing WHERE whyearmon <= :param GROUP BY rsccd ) b WHERE a.rsccd = b.rsccd UNION ALL -- 2. 전체 합계 -- SELECT Null AS rsccd , 'total' AS rscnm , Null AS sect , b.previousyear AS previousyearsum -- 전년누계 , b.previousmon AS previousmonsum -- 전월누계 , b.mon AS mon -- 당월 , b.previousmon + b.mon AS monsum -- 당월누계 , b.previousyear + b.previousmon + b.mon AS total -- 총누계 FROM (SELECT NVL(SUM(CASE WHEN whyearmon < SUBSTR(:param, 1, 4) THEN qty * unitcost END), 0) AS previousyear -- 전년 , NVL(SUM(CASE WHEN whyearmon > SUBSTR(:param, 1, 4) AND whyearmon < :param THEN qty * unitcost END), 0) AS previousmon -- 전월 , NVL(SUM(CASE WHEN whyearmon = :param THEN qty * unitcost END), 0) AS mon -- 당월 FROM edu_warehousing WHERE whyearmon <= :param ) b ORDER BY 1 ;