안녕하세요.
통계를 뽑으려고 rollup을 사용중에 null이 있는경우 처리가 가능한지 궁금합니다
경기지역에 구군값이 null인경우
경기(전체)값이 2개가 생기는데 1개로 나오게 할수있나요?
WITH t AS ( SELECT '서울' sido, '마포구' gugun, 1 cnt FROM dual UNION ALL SELECT '서울', '관악구' , 2 FROM dual UNION ALL SELECT '경기', '수원시' , 2 FROM dual UNION ALL SELECT '경기', '남양주시', 9 FROM dual UNION ALL SELECT '경기', null, 9 FROM dual ) SELECT NVL2(sido, sido ||'('|| NVL(gugun, '전체') ||')', '계') loc , SUM(cnt) cnt FROM t GROUP BY ROLLUP(sido, gugun) ORDER BY sido, gugun NULLS FIRST ;
WITH t AS ( SELECT '서울' sido, '마포구' gugun, 1 cnt FROM dual UNION ALL SELECT '서울', '관악구' , 2 FROM dual UNION ALL SELECT '경기', '수원시' , 2 FROM dual UNION ALL SELECT '경기', '남양주시', 9 FROM dual UNION ALL SELECT '경기', null, 9 FROM dual ) SELECT DECODE(GROUPING_ID(sido, gugun), 0, sido ||'('|| gugun ||')' , 1, sido || '(전체)' , 3, '계' ) loc , SUM(cnt) cnt FROM t GROUP BY ROLLUP(sido, gugun) ORDER BY sido, GROUPING(gugun) DESC, gugun ; -- http://gurubee.net/lecture/2679