๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IT์ž๊ฒฉ์ฆ ๊ณต๋ถ€/SQLD ๊ธฐ์ถœ๋ฌธ์ œ

[SQLD] 33ํšŒ ๊ธฐ์ถœ 23๋ฒˆ ๋ฌธ์ œ - ์œˆ๋„์šฐ ํ•จ์ˆ˜

by yunamom 2022. 5. 27.
๋ฐ˜์‘ํ˜•

 

 

 

 sqld 33ํšŒ ๊ธฐ์ถœ 23๋ฒˆ ๋ฌธ์ œ 

๋‹ค์Œ ์ฃผ์–ด์ง„ ํ…Œ์ด๋ธ”์—์„œ ์•„๋ž˜์˜ SQL์„ ์ˆ˜ํ–‰ํ•˜์˜€์„ ๋•Œ์˜ ๊ฒฐ๊ณผ๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์„ ๋•Œ ๊ฒฐ๊ณผ์—์„œ ๋นˆ์นธ์— ๋“ค์–ด๊ฐˆ MGR_SUM ๊ฐ’์œผ๋กœ ์•Œ๋งž์€ ๊ฒƒ์„ ๊ณ ๋ฅด์‹œ์˜ค.

์ •๋‹ตํ™•์ธ๐ŸŒผ

[SQLD_33_23]

MGR   ENAME   HIREDATE   SAL
-----------------------------
์œ ๋น„    ๊ด€์šฐ      2017    1000
์œ ๋น„    ์žฅ๋น„      2018    1500
์œ ๋น„    ์ œ๊ฐˆ๋Ÿ‰     2015    2000
์กฐ์กฐ    ํ•˜ํ›„๋ˆ     2016    2000
์กฐ์กฐ    ํ•˜ํ›„์—ฐ     2017    3500
์กฐ์กฐ    ์ˆœ์šฑ      2015    3000
์†๊ถŒ    ์ฃผ์œ       2015    2500
์†๊ถŒ    ํ™ฉ๊ฐœ      2019    3000
์†๊ถŒ    ๋…ธ์ˆ™      2013    2000
[SQL]

SELECT MGR, ENAME, HIREDATE, SAL,
   SUM(SAL) OVER(PARTITION BY MGR 
            ORDER BY HIREDATE
            ROWS BETWEEN 1 PRECEDING
            AND 1 FOLLOWING) AS MGR_SUM
    FROM SQLD_33_23;
[RESULT]

MGR   ENAME   HIREDATE   SAL   MGR_SUM
---------------------------------------
์†๊ถŒ    ๋…ธ์ˆ™      2013    2000  (       )
์†๊ถŒ    ์ฃผ์œ       2015    2500  (       )
์†๊ถŒ    ํ™ฉ๊ฐœ      2019    3000  (       )
์œ ๋น„    ์ œ๊ฐˆ๋Ÿ‰     2015    2000  (       )
์œ ๋น„    ๊ด€์šฐ      2017    1000  (       )
์œ ๋น„    ์žฅ๋น„      2018    1500  (       )
์กฐ์กฐ    ์ˆœ์šฑ      2015    3000  (       )
์กฐ์กฐ    ํ•˜ํ›„๋ˆ     2016    2000  (       )
์กฐ์กฐ    ํ•˜ํ›„์—ฐ     2017    3500  (       )
1) 2000, 2500, 3000, 2000, 1000, 1500, 3000, 2000, 3500

2) 4500, 7500, 5500, 3000, 4500, 2500, 5000, 8500, 5500

3) 2000, 4500, 7500, 2000, 3000, 4500, 3000, 5000, 8500

4) 4500, 7500, 7500, 3000, 4500, 4500, 5000, 8500, 8500

 

 ์ •๋‹ต :  2

๐Ÿ“–๋ฌธ์ œํ™•์ธ

 

ํ•ด์„ค : 1 PRECEDING / 1 FOLLOWING - ํ˜„์žฌํ–‰์„ ๊ธฐ์ค€์œผ๋กœ ์ด์ „ํ–‰+ํ˜„์žฌ+๋‹ค์Œํ–‰ ํ•ฉ๊ณ„๋ฅผ ํ‘œ์‹œ

by yunamom

- ROWS : ๋ฌผ๋ฆฌ์ ์ธ ROW ๋‹จ์œ„๋กœ ํ–‰ ์ง‘ํ•ฉ์„ ์ง€์ •ํ•œ๋‹ค.(ํ˜„์žฌ ํ–‰์˜ ์•ž ๋’ค ๊ฑด์ˆ˜)
- RANGE : ํ˜„์žฌ ํ–‰์˜ ๋ฐ์ดํ„ฐ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์•ž ๋’ค ๋ฐ์ดํ„ฐ ๊ฐ’์˜ ๋ฒ”์œ„๋ฅผ ํ‘œ์‹œํ•œ๋‹ค.
- BETWEEN ~ AND ์ ˆ : ์œˆ๋„์šฐ์˜ ์‹œ์ž‘๊ณผ ๋ ์œ„์น˜๋ฅผ ์ง€์ •ํ•œ๋‹ค.
- UNBOUNDED PRECEDING : PARTITION์˜ ์ฒซ ๋ฒˆ์งธ ๋กœ์šฐ์—์„œ ์œˆ๋„์šฐ๊ฐ€ ์‹œ์ž‘ํ•œ๋‹ค.
- UNBOUNDED FOLLOWING : PARTITION์˜ ๋งˆ์ง€๋ง‰ ๋กœ์šฐ์—์„œ ์œˆ๋„์šฐ๊ฐ€ ์‹œ์ž‘ํ•œ๋‹ค.
- CURRENT ROW : ์œˆ๋„์šฐ์˜ ์‹œ์ž‘์ด๋‚˜ ๋ ์œ„์น˜๊ฐ€ ํ˜„์žฌ ๋กœ์šฐ ์ด๋‹ค.

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป์œˆ๋„์šฐ ํ•จ์ˆ˜(WINDOW FUNCTION) ๋ž€? [ ํŽผ์ณ๋ณด๊ธฐ ]

- ํ–‰๊ณผ ํ–‰๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์‰ฝ๊ฒŒ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“  ํ•จ์ˆ˜

- ์œˆ๋„์šฐ์ ˆ์„ ์‚ฌ์šฉํ•˜๋ฉด PARTITION BY ์ ˆ์— ๋ช…์‹œ๋œ ๊ทธ๋ฃน์„ ์ข€ ๋” ์„ธ๋ถ€์ ์œผ๋กœ ๊ทธ๋ฃนํ™” ํ• ์ˆ˜์žˆ๋‹ค.

[ ์ž‘์„ฑ๋ฒ• ์˜ˆ์ œ ]
SELECT SUM(SAL)
       OVER -- ํ•„์ˆ˜ ํ‚ค์›Œ๋“œ
       ([PARTITION BY ์นผ๋Ÿผ] -- ๊ทธ๋ฃน ์ปฌ๋Ÿผ๋ช…
          [ORDER BY ์ ˆ] -- ์ •๋ ฌ ์ปฌ๋Ÿผ๋ช…
          [WINDOWING ์ ˆ])FROM ํ…Œ์ด๋ธ” ๋ช…;

 

- WINDOW_FUNCTION

๊ธฐ์กด์— ์‚ฌ์šฉํ•˜๋˜ ํ•จ์ˆ˜๋„ ์žˆ๊ณ , ๋ถ„์„ํ•จ์ˆ˜์ค‘์—์„œ ์ผ๋ถ€(AVG, COUNT, SUM, MAX, MIN)๋งŒ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.

- ARGUMENTS (์ธ์ˆ˜)

๊ธฐ์กด์— ์‚ฌ์šฉํ•˜๋˜ ํ•จ์ˆ˜๋„ ์žˆ๊ณ ,

- PARTITION BY ์ ˆ

 ์ „์ฒด ์ง‘ํ•ฉ์„ ๊ธฐ์ค€์— ์˜ํ•ด ์†Œ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.(์„ธ๋ถ€์ ์œผ๋กœ ๊ทธ๋ฃนํ•‘)

- ORDER BY ์ ˆ

์–ด๋–ค ํ•ญ๋ชฉ์— ๋Œ€ํ•ด ์ˆœ์œ„๋ฅผ ์ง€์ •ํ• ์ง€์— ๋Œ€ํ•ด ๊ธฐ์ˆ ํ•œ๋‹ค.

- WINDOWING ์ ˆ 

ํ•จ์ˆ˜์˜ ๋Œ€์ƒ์ด ๋˜๋Š” ํ–‰ ๊ธฐ์ค€์˜ ๋ฒ”์œ„๋ฅผ ๊ฐ•๋ ฅํ•˜๊ฒŒ ์ง€์ •ํ•œ๋‹ค. ( ์˜ค๋ผํด ์ง€์› )

ROWS  → ํ˜„์žฌ ํ–‰์˜ ์•ž ๋’ค ๊ฑด์ˆ˜๋ฅผ ์˜๋ฏธ (๋ฌผ๋ฆฌ์ )

RANGE → ํ˜„์žฌ ํ–‰์˜ ๋ฐ์ดํ„ฐ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์•ž ๋’ค ๋ฐ์ดํ„ฐ ๊ฐ’์˜ ๋ฒ”์œ„๋ฅผ ํ‘œ์‹œ (๋…ผ๋ฆฌ์ )

ROWS, RANGE ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.

--[BETWEEN TYPE]
ROWS | RANGE BETWEEN
UNBOUNDED PRECEDING | 
CURRENT ROW | VALUE_EXPR PRECEDING/FOLLOWING 
AND
UNBOUNDED FOLLOWING |
CURRENT ROW | VALUE_EXPR PRECEDING/FOLLOWING

--[NOT BETWEEN]
ROWS | RANGE
UNBOUNDED PRECEDING |
CURRENT ROW | VALUE_EXPR PRECEDING

--[DEFAULT]
RANGE BETWEEN UNBOUNDED PRECEDING
AND
CURRENT ROW

-- UNBOUNDED : ํ•œ๊ณ„๋ฅผ ๋‘์ง€ ์•Š๊ณ , ํ•ด๋‹น ํŒŒํ‹ฐ์…˜์˜ ๋๊นŒ์ง€๋ฅผ ์˜๋ฏธ
-- PRECEDING : ํ˜„์žฌํ–‰์—์„œ ์ด์ „ ํ–‰์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
-- FOLLOWING : ํ˜„์žฌํ–‰์—์„œ ๋‹ค์Œ ํ–‰์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. (BETWEEN ~ AND ~ ํ˜•์‹์œผ๋กœ ์ž‘์„ฑํ•จ)

[RANGE, ROWS์—์„œ ์‚ฌ์šฉํ•˜๋Š” UNBOUNDED PRECEDING, CURRENT ROW, UNBOUNDED FOLLOWING ]

1. UNBOUNDED PRECEDING / UNBOUNDED FOLLOWING 

- ์ฒ˜์Œํ–‰๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ํ–‰ ๊ฒฐ๊ณผ๊นŒ์ง€ ๊ณ„์‚ฐํ•œ ํ•ฉ๊ณ„๋ฅผ ํ‘œ์‹œํ•ด์ค๋‹ˆ๋‹ค 

2. UNBOUNDED PRECEDING / CURRENT ROW

- ์ฒ˜์Œํ–‰๋ถ€ํ„ฐ ํ˜„์žฌํ–‰ ๊ฒฐ๊ณผ๊นŒ์ง€ ๊ณ„์‚ฐํ•œ ํ•ฉ๊ณ„๋ฅผ ํ‘œ์‹œํ•ด์ค๋‹ˆ๋‹ค 

3. CURRENT ROW / UNBOUNDED FOLLOWING

- ํ˜„์žฌํ–‰๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ํ–‰ ๊ฒฐ๊ณผ๊นŒ์ง€ ๊ณ„์‚ฐํ•œ ํ•ฉ๊ณ„๋ฅผ ํ‘œ์‹œํ•ด์ค๋‹ˆ๋‹ค 

4. 1 PRECEDING / 1 FOLLOWING

- ํ˜„์žฌํ–‰์— ์ด์ „ํ–‰+๋ณธ์ธ+๋‹ค์Œํ–‰ ํ•ฉ๊ณ„๋ฅผ ํ‘œ์‹œํ•ด์ค๋‹ˆ๋‹ค

 

๐Ÿ’ก์•Œ์•„๋‘๊ธฐ

๊ทธ๋ฃน ๋‚ด ํ–‰ ์ˆœ์„œ ํ•จ์ˆ˜

 

FIRST_VALUE -  ๊ฐ€์žฅ ๋จผ์ € ๋‚˜์˜จ ๊ฐ’, MIN ํ•จ์ˆ˜์™€ ๊ฐ™๋‹ค

LAST_VALUE - ๊ฐ€์žฅ ๋‚˜์ค‘์— ๋‚˜์˜จ ๊ฐ’, MAX ํ•จ์ˆ˜์™€ ๊ฐ™๋‹ค

 

LAG : ์ด์ „ ๊ฐ’

- LAG(SAL, 2, 0)

์ฒซ๋ฒˆ์งธ : ๋Œ€์ƒ ์ปฌ๋Ÿผ๋ช…

๋‘๋ฒˆ์งธ : ๋ช‡ ๋ฒˆ์งธ ์ด์ „ ํ–‰์„ ๊ฐ€์ ธ์˜ฌ ๊ฑด์ง€ ๊ฒฐ์ •(DEFAULT๋Š” 1)

์„ธ๋ฒˆ์งธ : NULL์˜ ๊ฒฝ์šฐ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค(NVL, ISNULL๊ณผ ๊ฐ™์Œ)

 

LEAD : ๋‹ค์Œ ๊ฐ’

- LEAD(SAL, 2, 0)

์ฒซ๋ฒˆ์งธ : ๋Œ€์ƒ ์ปฌ๋Ÿผ๋ช…

๋‘๋ฒˆ์งธ : ๋ช‡ ๋ฒˆ์งธ ๋‹ค์Œ ํ–‰์„ ๊ฐ€์ ธ์˜ฌ ๊ฑด์ง€ ๊ฒฐ์ •(DEFAULT๋Š” 1)

์„ธ๋ฒˆ์งธ : NULL์˜ ๊ฒฝ์šฐ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค(NVL, ISNULL๊ณผ ๊ฐ™์Œ)

 

RATIO_TO_REPORT - ํŒŒํ‹ฐ์…˜ ๋‚ด ์ „์ฒด SUM()๊ฐ’์— ๋Œ€ํ•œ ํ–‰๋ณ„ ์นผ๋Ÿผ ๊ฐ’์˜ ๋ฐฑ๋ถ„์œจ์„ ์†Œ์ˆ˜์ ์œผ๋กœ ๊ตฌํ•œ๋‹ค.

PERCENT_RANK - ๋จผ์ €๋‚˜์˜ค๋Š”๊ฒƒ์„ 0, ๊ฐ€์žฅ ๋‚˜์ค‘์— ๋‚˜์˜ค๋Š” ๊ฒƒ์„ 1๋กœ ํ•˜์—ฌ ๊ฐ’์ด ์•„๋‹Œ ํ–‰์˜ ์ˆœ์„œ๋ณ„ ๋ฐฑ๋ถ„์œจ์„ ๊ตฌํ•œ๋‹ค.

CUME_DIST - ํŒŒํ‹ฐ์…˜๋ณ„ ์œˆ๋„์šฐ์˜ ์ „์ฒด๊ฑด์ˆ˜์—์„œ ํ˜„์žฌ ํ–‰๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฑด์ˆ˜์— ๋Œ€ํ•œ ๋ˆ„์  ๋ฐฑ๋ถ„์œจ์„ ๊ตฌํ•œ๋‹ค.

NTILE - ํŒŒํ‹ฐ์…˜๋ณ„ ์ „์ฒด ๊ฑด์ˆ˜๋ฅผ ARGUMENT ๊ฐ’์œผ๋กœ N๋“ฑ๋ถ„ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํ•œ๋‹ค.(์˜ˆ: ์ˆ˜๋Šฅ๋“ฑ๊ธ‰)

 

300x250

์ฝ”๋“œ