[SQL] ROLLUP ν¨μ
27 Jun 2023 -
1 minute read
π μκ·Έλ£Ή κ°μ μκ³(SUBTOTAL)μ ν©κ³(GRAND TOTAL)λ₯Ό κ³μ°νλ ROLLUP ν¨μμ λν΄ μμ보μ π
βΆ DEPT
βΆ EMP
GROUP BY μ μ¬μ©
SELECT
B.DNAME,
A.JOB,
COUNT(*) AS EMP_CNT,
SUM(A.SAL) AS SAL_SUM
FROM EMP A,
DEPT B
WHERE B.DEPTNO = A.DEPTNO
GROUP BY B.DNAME, A.JOB;
μ€νκ²°κ³Ό )
GROUP BY μ + ORDER BY μ
SELECT
B.DNAME,
A.JOB,
COUNT(*) AS EMP_CNT,
SUM(A.SAL) AS SAL_SUM
FROM EMP A,
DEPT B
WHERE B.DEPTNO = A.DEPTNO
GROUP BY B.DNAME, A.JOB
ORDER BY B.DNAME, A.JOB;
μ€νκ²°κ³Ό )
ROLLUP ν¨μ
SELECT
B.DNAME,
A.JOB,
COUNT(*) AS EMP_CNT,
SUM(A.SAL) AS SAL_SUM
FROM EMP A,
DEPT B
WHERE B.DEPTNO = A.DEPTNO
GROUP BY ROLLUP (B.DNAME, A.JOB);
μ€νκ²°κ³Ό )
GROUP BY μν μ μμ±λλ νμ€ μ§κ³ 9건 μ΄μΈμ DNAME λ³ λͺ¨λ JOBμ ν©κ³ 3건과 λͺ¨λ νμ ν© 1κ±΄μ΄ μΆκ°λ κ²μ λ³Ό μ μλ€.
ROLLUP ν¨μ + ORDER BY μ
SELECT
B.DNAME,
A.JOB,
COUNT(*) AS EMP_CNT,
SUM(A.SAL) AS SAL_SUM
FROM EMP A,
DEPT B
WHERE B.DEPTNO = A.DEPTNO
GROUP BY ROLLUP (B.DNAME, A.JOB)
ORDER BY B.DNAME, A.JOB;
μ€νκ²°κ³Ό )
WITH ROLLUP
π₯ MySQLμμλ WITH ROLLUPμ μ¬μ© π₯
SELECT
B.DNAME,
A.JOB,
COUNT(*) AS EMP_CNT,
SUM(A.SAL) AS SAL_SUM
FROM EMP A,
DEPT B
WHERE B.DEPTNO = A.DEPTNO
GROUP BY B.DNAME, A.JOB WITH ROLLUP;
μ€νκ²°κ³Ό )