[데이터베이스 통계쿼리] 12. rollup

김건우's avatar
Mar 04, 2025
[데이터베이스 통계쿼리] 12. rollup
group by를 잘 이해하자.
ROLLUP그룹별 소계를 자동으로 계산하는 기능입니다.
즉, GROUP BY와 함께 사용하면 부분 합계(소계)와 전체 합계를 자동으로 추가할 수 있습니다.
job, deptno로 그룹을 짓고, job별 소계 구하고, 전체 총계 구한다.
-- job, deptno 그룹화 select job, deptno, avg(sal) avg_sal, count(*) cnt from emp where job = 'CLERK' group by job, deptno; -- 소계 select job, null 'deptno', avg(sal) avg_sal, count(*) 'cnt' from emp where job = 'CLERK' group by job; -- 총계 select null, null, avg(sal) avg_sal, count(*) from emp; -- rollup select job, deptno, avg(sal), count(*) cnt from emp group by job, deptno with rollup;
notion image
Share article

gunwoo