GROUPING() ํจ์๋ SELECT ์ ์์ ์ฌ์ฉํ๋ ํจ์์ด๋ค. GROUPING() ํจ์์ ์ธ์๋ก ๊ทธ๋ฃนํ ๋์์ธ ์ปฌ๋ผ์ ์ง์ ํ๋ฉด ํด๋น ์ปฌ๋ผ์ด NULL์ผ ๋ 1, NOT NULL์ผ ๋ 2๋ฅผ ๋ฐํํ๋ ํจ์์ด๋ค.
SELECT B.DNAME
, A.JOB
, SUM(A.SAL)
, GROUPING(B.DNAME) AS GP1
, GROUPING(A.JOB) AS GP2
FROM EMP A
, DEPT B
WHERE A.DEPTNO = B.DEPTNO
GROUP BY ROLLUP(B.DNAME, A.JOB)
ORDER BY 1,2
;
์ด๋ฅผ CASE-WHEN ์ ๊ณผ ํจ๊ป ํ์ฉํ๋ฉด NULL ๊ฐ์ ์๊ณ, ์ด๊ณ ๋ฑ์ผ๋ก ์ง์ ํ ์ ์๋ค.
SELECT CASE WHEN T.GP1 = 1 THEN '์ด๊ณ' ELSE T.DNAME END AS DNAME
, CASE WHEN T.GP2 = 1 AND T.GP1 != 1 THEN '์๊ณ' ELSE T.JOB END AS JOB
, T.SAL
, T.GP1
, T.GP2
FROM (SELECT B.DNAME
, A.JOB
, SUM(A.SAL) AS SAL
, GROUPING(B.DNAME) AS GP1
, GROUPING(A.JOB) AS GP2
FROM EMP A
, DEPT B
WHERE A.DEPTNO = B.DEPTNO
GROUP BY ROLLUP(B.DNAME, A.JOB)) AS T
ORDER BY T.DNAME, T.JOB
;
'๊ฐ๋ฐ์ผ๊ธฐ ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Oracle] ์ค๋ผํด ๊ธฐ์ด ๋ด์ฅํจ์ ๋ชจ์ (2) | 2024.01.23 |
---|---|
[DBMS] SqlDeveloper์์ DB ๋น๊ตํ๊ธฐ (0) | 2023.12.21 |
[Oracle] ํ์ฅ GROUP ํจ์ - CUBE() (0) | 2023.12.06 |
[Oracle] ํ์ฅ GROUP ํจ์ - GROUPING SETS() (2) | 2023.12.06 |
[Oracle] ํ์ฅ GROUP ํจ์ - ROLLUP() (0) | 2023.12.05 |