๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ORACLE/SQL

[Oracle] OVER ... PARTITION BY ๋ถ„์„์ ˆ ๋ถ„์„ํ•จ์ˆ˜ ANALYTIC FUNCTION :: ๋งˆ์ด์ž๋ชฝ

by ๐ŸŒปโ™š 2020. 3. 28.
์‚ฌ์› ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๋Š”๋ฐ, ์ „์ฒด ๊ธ‰์—ฌ ํ‰๊ท ๊ฐ’๊ณผ ํ•จ๊ป˜ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

ORACLE SCOTT ๊ณ„์ • EMP ํ…Œ์ด๋ธ”๋กœ ์ถœ๋ ฅ๋œ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค.

์œ„ ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด ์–ด๋–ป๊ฒŒ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜์‹ค๊ฑด๊ฐ€์š”?

 

1
2
3
4
5
6
7
8
SELECT
    DEPTNO
    ,EMPNO
    ,ENAME
    ,JOB
    ,SAL
    ,ROUND((SELECT AVG(SAL) FROM EMP)) AS AVG
FROM EMP;
 

SUBQUERY๋ฅผ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ... ANALYTIC FUNCTION ๋ถ„์„ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ข€ ๋” ํŽธํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

1
2
3
4
5
6
7
8
SELECT
    DEPTNO
    ,EMPNO
    ,ENAME
    ,JOB
    ,SAL
    ,ROUND(AVG(SAL) OVER()) AS AVG
FROM EMP;

 ๋ถ„์„ํ•จ์ˆ˜ OVER ์ ˆ์„ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค.

๊ฐ„๋‹จํ•˜๊ฒŒ ์ „์ฒด ํ‰๊ท ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ๋ผ ๋‘๊ฐœ์˜ ์ฟผ๋ฆฌ์—๋Š” ๋ณ„ ์ฐจ์ด๊ฐ€ ์—†์–ด ๋ณด์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ๊ฐ ๋ถ€์„œ๋ณ„ ํ‰๊ท ์„ ํ‘œ์‹œํ•˜๋ฉด ์–ด๋–จ๊นŒ์š”?

 

 

์‚ฌ์› ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๋Š”๋ฐ, ๊ฐ๊ฐ ๋ถ€์„œ ํ‰๊ท ๊ฐ’๊ณผ ํ•จ๊ป˜ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SELECT
    E1.DEPTNO
    ,EMPNO
    ,ENAME
    ,JOB
    ,SAL
    ,SUM_SAL
FROM EMP E1, (
    SELECT
        DEPTNO
        ,SUM(SAL) AS SUM_SAL
    FROM EMP
    GROUP BY DEPTNO
) E2
WHERE E1.DEPTNO = E2.DEPTNO
ORDER  BY DEPTNO;

SUBQUERY๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์œ„์™€ ๊ฐ™์ด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ๋˜‘๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ๋ถ„์„ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ํ’€๋ฉด ์–ด๋–จ๊นŒ์š”?

 

1
2
3
4
5
6
7
8
SELECT
    DEPTNO
    ,EMPNO
    ,ENAME
    ,JOB
    ,SAL
    ,SUM(SAL) OVER(PARTITION BY DEPTNO) AS SUM_SAL
FROM EMP;
 

๋ˆˆ์œผ๋กœ๋งŒ ๋ด๋„ ํ™•์‹คํžˆ SUBQUERY๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ๋ณด๋‹ค ๊ฐ„๋‹จํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿผ OVER ... PARTITION BY๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ถ„์„ํ•จ์ˆ˜๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”?

 

 

๋ถ„์„ํ•จ์ˆ˜๋ž€?

SELECT
    ๋ถ„์„ํ•จ์ˆ˜ OVER([PARTITION BY ์นผ๋Ÿผ] [ORDER BY ์นผ๋Ÿผ] [WINDOWING ์ ˆ])
FROM ํ…Œ์ด๋ธ”;

๋ถ„์„ํ•จ์ˆ˜๋Š” ์ด๋ฏธ ์‚ฌ์šฉํ•ด๋ณด์…จ์„ ๊ฒ๋‹ˆ๋‹ค. COUNT(), AVG(), SUM() ๋“ฑ๊ณผ ๊ฐ™์ด GROUP BY ์ ˆ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ–ˆ๋˜ ํ•จ์ˆ˜๋“ค๋„ ์žˆ๊ณ , RANK(), ROW_NUMBER(), DENSE_RANK(), LEAD(), LAG(), NTITLE()  ๋“ฑ ORACLE ๋ฒ„์ „์ด ์˜ฌ๋ผ๊ฐ€๋ฉด์„œ ๋งŽ์ด ์ƒ๊ฒจ๋‚˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ถ„์„ํ•จ์ˆ˜ ๋’ค์— ANALYTIC CLAUSE( OVER ์ ˆ)์„ ํ†ตํ•ด์„œ ํ–‰ ๊ทธ๋ฃน์˜ ์ •์˜๋ฅผ ์ง€์ •ํ•˜๊ณ  ๊ฐ ๊ทธ๋ฃน๋‹น ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜๋ณตํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ–‰ ๊ทธ๋ฃน์€ ์นผ๋Ÿผ์— ๋Œ€ํ•œ ํ–‰๋“ค์ด๊ณ  ํ–‰ ๊ทธ๋ฃน์˜ ๋ฒ”์œ„(WINDOW)๋ฅผ PARTITION BY, ORDER BY, WINDOWING์œผ๋กœ ์กฐ์ ˆ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

์œ„์˜ ๋ฌธ์ œ๋ฅผ ์˜ˆ์‹œ๋กœ ๋ดค์„๋•Œ,

SUM(SAL)        --> ๋ถ„์„ํ•จ์ˆ˜ SUM์„ ์‚ฌ์šฉํ–ˆ๊ณ  SAL ์นผ๋Ÿผ์— ๋Œ€ํ•œ ํ–‰๋“ค์ด ํ–‰ ๊ทธ๋ฃน์ž…๋‹ˆ๋‹ค.
OVER               --> ๋ถ„์„์ ˆ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ถ„์„ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์กฐ์ ˆ์„ OVER์ ˆ ์•ˆ์—์„œ ํ•ฉ๋‹ˆ๋‹ค.
PARTITION BY --> GROUP BY์™€ ๋™์ผํ•˜๊ฒŒ ๊ทธ๋ฃน์ง€์–ด ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅ ํ•ฉ๋‹ˆ๋‹ค.

ORDER BY       --> PARTITION BY๋กœ ์ •์˜๋œ WINDOW ๋‚ด์—์„œ ํ–‰๋“ค์˜ ์ •๋ ฌ์ˆœ์„œ๋ฅผ ์ •์˜ํ•ด์ค๋‹ˆ๋‹ค.
1
2
3
4
5
6
7
8
SELECT
    DEPTNO
    ,EMPNO
    ,ENAME
    ,JOB
    ,SAL
    ,SUM(SAL) OVER(PARTITION BY DEPTNO ORDER BY SAL DESC) AS SUM_SAL
FROM EMP;
 

์œ„์˜ ๋ฌธ์ œ์—์„œ ORDER BY์ ˆ์„ ์‚ฌ์šฉํ•ด์„œ ๊ฐ๊ฐ PARTITION ์•ˆ์—์„œ ์ •๋ ฌ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

OVER ์ ˆ ์‹คํ–‰ ์ˆœ์„œ

OVER์ ˆ์—์„œ๋Š” ORDER BY์ ˆ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

 

 

๋Œ“๊ธ€