inblog logo
|
harimmon
    데이터베이스

    [Database] 18. ROLL UP

    백하림's avatar
    백하림
    Feb 28, 2025
    [Database] 18. ROLL UP
    💡
    Roll up은 소계, 총계를 구할 때 사용하는 함수이다.

    1. 노가다 코드

    select job, deptno, avg(sal), count(*) cnt from emp where job = 'CLERK' group by job, deptno union all select job, null deptno, avg(sal), count(*) from emp where job = 'CLERK' union all select job, deptno, avg(sal), count(*) cnt from emp where job = 'SALESMAN' group by job, deptno union all select job, null deptno, avg(sal), count(*) from emp where job = 'SALESMAN' union all select job, deptno, avg(sal), count(*) cnt from emp where job = 'ANALYST' group by job, deptno union all select job, null deptno, avg(sal), count(*) from emp where job = 'ANALYST' union all select job, deptno, avg(sal), count(*) cnt from emp where job = 'MANAGER' group by job, deptno union all select job, null deptno, avg(sal), count(*) from emp where job = 'MANAGER' union all select job, deptno, avg(sal), count(*) cnt from emp where job = 'PRESIDENT' group by job, deptno union all select job, null deptno, avg(sal), count(*) from emp where job = 'PRESIDENT' union all select null, null, avg(sal), count(*) from emp;
    notion image

    2. Rollup 사용 코드

    select job, deptno, avg(sal), count(*) cnt from emp group by job, deptno with rollup;
    notion image
    Share article

    harimmon

    RSS·Powered by Inblog