728x90
๋ฐ˜์‘ํ˜•

 

1) JOIN : ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๊ฒฐํ•ฉ / ์ง‘ํ•ฉ๊ณผ ์œ ์‚ฌํ•จ!

*๊ต์ง‘ํ•ฉ & ํ•ฉ์ง‘ํ•ฉ 

  • INNER JOIN
  • LEFT (OUTER) JOIN
  • RIGHT (OUTER) JOIN
  • (FULL) OUTER JOIN

 

* ํ•ฉ์ง‘ํ•ฉ

- UNION (ALL)

 

* ์ฐจ์ง‘ํ•ฉ

- MINUS (#orable) = EXCEPT (#SQL Server)

 

* ๊ฒฐํ•ฉ๋˜๋Š” ๋Œ€์ƒ๊ฐ„์˜ ์ผ์น˜ ์ •๋„

- EQUI ์กฐ์ธ <> non-EQUI ์กฐ์ธ

 

* ์กฐ๊ฑด๊ตฌ ์—†๋Š” CROSS JOIN(์ƒํ˜ธ ๊ฒฐํ•ฉ)

 

INNER JOIN

์™„์ „ํ•œ ๊ต์ง‘ํ•ฉ๋งŒ ๊ฐ€์ ธ์˜ฌ ๋–„ INNER JOIN์„ ์‚ฌ์šฉํ•œ๋‹ค

 

INNER JOIN ํ‘œํ˜„ ๋ฐฉ์‹ 2๊ฐ€์ง€

1๋ฒˆ
SELECT A.*, B.์—ฐ๋ น
FROM GENDER A INNER JOIN AGE B
         ON A.ํšŒ์›์ฝ”๋“œ = B.ํšŒ์›์ฝ”๋“œ
WHERE A.์„ฑ๋ณ„ = 'F';

2๋ฒˆ.
SELECT A.*, B.์—ฐ๋ น
FROM GENDER A, AGE B
WHERE A.ํšŒ์›์ฝ”๋“œ = B.ํšŒ์›์ฝ”๋“œ
           AND A.์„ฑ๋ณ„ = 'F';

 

 

LEFT (OUTER) JOIN

 

 

LEFT (OUTER) JOIN ํ‘œํ˜„๋ฐฉ์‹ 2๊ฐ€์ง€

1๋ฒˆ
SELECT A.*, B.์—ฐ๋ น
FROM GENDER A LEFT JOIN AGE B
        ON A.ํšŒ์›์ฝ”๋“œ = B.ํšŒ์›์ฝ”๋“œ;

2๋ฒˆ
SELECT A.*, B.์—ฐ๋ น
FROM GENDER A, AGE B
WHERE A.ํšŒ์›์ฝ”๋“œ = B.ํšŒ์›์ฝ”๋“œ(+);

 

 

RIGHT (OUTER) JOIN

 

 

RIGHT (OUTER) JOIN ํ‘œํ˜„๋ฐฉ์‹ 2๊ฐ€์ง€

1๋ฒˆ
SELECT B.ํšŒ์›์ฝ”๋“œ, ์„ฑ๋ณ„, ์—ฐ๋ น
FROM GENDER A RIGHT JOIN AGE B
          ON A.ํšŒ์›์ฝ”๋“œ = B.ํšŒ์›์ฝ”๋“œ;

2๋ฒˆ
SELECT B.ํšŒ์›์ฝ”๋“œ, ์„ฑ๋ณ„, ์—ฐ๋ น
FROM GENDER A, AGE B
WHERE A.ํšŒ์›์ฝ”๋“œ(+) = B.ํšŒ์›์ฝ”๋“œ;

 

 

(FULL) OUTER JOIN

 

(FULL) OUTTER JOIN  ํ‘œํ˜„๋ฐฉ์‹ 2๊ฐ€์ง€

1๋ฒˆ.
SELECT A.*, B.์—ฐ๋ น
FROM GENDER A FULL OUTER JOIN AGE B
         ON A.ํšŒ์›์ฝ”๋“œ = B.ํšŒ์›์ฝ”๋“œ;

2๋ฒˆ.
SELECT A.*, B.์—ฐ๋ น
FROM GENDER A , AGE B
WHERE A.ํšŒ์›์ฝ”๋“œ(+) = B.ํšŒ์›์ฝ”๋“œ(+);
# Oracle9 ๋ฒ„์ „ ์ดํ›„๋ถ€ํ„ฐ๋Š” ์–‘์ชฝ์˜ (+) ๊ฐ€๋Šฅ

 

 

์˜ˆ์ œ1)

 

 => ์ •๋‹ต!!

 

SELECT *

FROM T1 [ LEFT OUTER JOIN ] T2

ON T1.c2 = T2.c2;

 

 

UNION, UNION ALL - ๋‘ ํ”Œ๋žซํผ ๊ธฐ์—…์ด ํ•ฉ๋ณ‘ํ•  ๋–„!

UNION : ์ค‘๋ณต๋œ ๋ ˆ์ฝ”๋“œ๋ฅผ ์ œ๊ฑฐํ•œ ํ›„ ๋ฐ˜ํ™˜ / UNION ALL : ์ค‘๋ณต๋œ ๋ ˆ์ฝ”๋“œ๋ฅผ ํฌํ•จํ•˜์—ฌ ๋ฐ˜ํ™˜

 

์˜ˆ์ œ2)

=> ์ •๋‹ต!!

A. JOIN

B. UNION

 

 

์˜ˆ์ œ3)

 

์˜ˆ์ œ4)

 

MINUS - A ํšŒ์‚ฌ์—๋งŒ ์žˆ๋Š” ๊ณ ๊ฐ ์ •๋ณด๋ฅผ ์ถ”๋ฆฌ๋ ค๊ณ  ํ•œ๋‹ค!!!

EQUI JOIN, non-EQUI JOIN

 

CROSS JOIN - A ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰๊ณผ B ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ํ–‰์„ ์กฐ์ธ(A ๋ ˆ์ฝ”๋“œ ์ˆ˜ * B ๋ ˆ์ฝ”๋“œ ์ˆ˜)

๋‘˜์„ ๊ณฑ์…ˆํ•˜๋Š” ๋Š๋‚Œ!!

 

 

2) ๊ณ„์ธตํ˜• ์กฐํšŒ & ๊ทธ๋ฃน ํ•จ์ˆ˜

 

๊ณ„์ธตํ˜• ์กฐํšŒ : ํŠธ๋ฆฌํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์กฐํšŒ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ

 

>> ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์—†๋Š” ๋ฐ์ดํ„ฐ : LEAF ๋…ธ๋“œ

 

์˜ˆ์ œ 1)

=> ์ •๋‹ต!!  12

 

๊ณ„์ธตํ˜• ์กฐํšŒ ๋ฌธ์ œ๋Š”...

์กฐํšŒ๋ฅผ ํ†ตํ•ด ์–ด๋–ป๊ฒŒ ๋ ˆ์ฝ”๋“œ๊ฐ€ ์žฌ๋ฐฐ์น˜ ๋˜๋Š”์ง€ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”!!

1. ์‹œ์ž‘์  ์žก๊ธฐ

START WITH : ๊ณ„์ธต ๊ตฌ์กฐ๊ฐ€ ์‹œ์ž‘๋˜๋Š” ์ง€์ (ROOT NODE)์„ ์•Œ๋ ค์คŒ, ์—ฌ๊ธฐ์— ์ ํžŒ ์กฐ๊ฑด์— ๋งž๋Š” ๋ ˆ์ฝ”๋“œ๊ฐ€ ROOT(๋ถ€๋ชจ), ํ˜„ ๋ฌธ์ œ์—์„œ๋Š” ์ฒซ๋ฒˆ์งธ ๋ ˆ์ฝ”๋“œ๊ฐ€ ํŠธ๋ฆฌ ๊ตฌ์กฐ์˜ ROOT NODE

 

 

2. ์žฌ๋ฐฐ์น˜ ๊ธฐ์ค€

CONNECT BY, PRIOR : ๋‹ค์Œ ๋ ˆ์ฝ”๋“œ๊ฐ€ ์–ด๋–ค ๊ฒƒ์ด ์˜ฌ์ง€ ์•Œ๋ ค์คŒ
- CONNECT BY PRIOR a = b
- CONNECT BY a = PRIOR b
๋ชจ๋“  ๋ ˆ์ฝ”๋“œ์— ๋Œ€ํ•˜์—ฌ ๊ด€๊ณ„์„ฑ(๊ณ„์ธต ๊ตฌ์กฐ)๋ฅผ ํŒŒ์•…ํ•˜์—ฌ ํ…Œ์ด๋ธ”์„ ์žฌ๋ฐฐ์น˜ํ•˜๊ธฐ ์œ„ํ•จ

1) a์ปฌ๋Ÿผ๊ณผ b์ปฌ๋Ÿผ์ด ๋™์ผํ•œ ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฐ„์— ๊ณ„์ธตํ™”๊ฐ€ ๋ฐœ์ƒ(a=b)
2) ์•ž์„ (PRIOR) ๋ ˆ์ฝ”๋“œ์˜ a์ปฌ๋Ÿผ๊ณผ ๋™์ผํ•œ b์ปฟํ—˜์„ ๊ฐ€์ง„ ๋ ˆ์ฝ”๋“œ๋“ค์ด ๋’ค์— ์˜จ๋‹ค
CONNECT BY PRIOR a = b : ์•ž์„  ๋ ˆ์ฝ”๋“œ์˜ a = b์ด๋‹ค

ํ˜„ ๋ฌธ์ œ์—์„œ๋Š” 
1) col1๊ณผ col2๊ฐ€ ๋™์ผํ•œ ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฐ„์— ๊ณ„์ธตํ™”๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค
2) ใ„ฑ์˜ col1๊ณผ ๋™์ผํ•œ col2๋ฅผ ๊ฐ€์ง„ ๋ ˆ์ฝ”๋“œ๊ฐ€ ๋‹ค์Œ ์ˆœ์„œ์— ์˜จ๋‹ค

 

 

๊ทธ๋Ÿผ ใ„ด, ใ„ท ์ค‘์— ๋ญ๊ฐ€ ๋จผ์ € ์˜ฌ์ง€ ์–ด๋–ป๊ฒŒ ์•”?? =? ORDER SIBLINGS BY ๊ฐ€ ํ•ด๊ฒฐํ•ด์คŒ!!

 ORDER SIBLINGS BY col3 
: col3 ๊ธฐ์ค€, ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ ์ˆœ์„œ ๊ฒฐ์ •
>> col3 : [ใ„ด] 12 < [ใ„ท] 13 

 

 

์˜ˆ์ œ 2)

 

GROUP ํ•จ์ˆ˜

GROUP BY ์„ฑ๋ณ„, ์—ฐ๋ น 1. ์„ฑ๋ณ„ * ์—ฐ๋ น ๋ณ„ ์†Œ๊ณ„
GROUP BY
GROUPING SETS(
์„ฑ๋ณ„, ์—ฐ๋ น)
1. ์„ฑ๋ณ„ ๋ณ„ ์†Œ๊ณ„
2. ์—ฐ๋ น ๋ณ„ ์†Œ๊ณ„
* ๊ด„ํ˜ธ ๋ฌถ์€ ์ง‘ํ•ฉ๋ณ„ ์ง‘๊ณ„ ๊ฐ€๋Šฅ
GROUP BY
ROLLUP(์„ฑ๋ณ„, ์—ฐ๋ น)
1. ์„ฑ๋ณ„ * ์—ฐ๋ น ๋ณ„ ์†Œ๊ณ„
2. ์„ฑ๋ณ„ ๋ณ„ ์†Œ๊ณ„
3. ์ „์ฒด ์ง‘๊ณ„
GROUP BY
CUBE(์„ฑ๋ณ„, ์—ฐ๋ น)
1. ์„ฑ๋ณ„ * ์—ฐ๋ น ๋ณ„ ์†Œ๊ณ„
2. ์„ฑ๋ณ„ ๋ณ„ ์†Œ๊ณ„
3. ์—ฐ๋ น ๋ณ„ ์†Œ๊ณ„
4. ์ „์ฒด ์ง‘๊ณ„
* ๊ทธ๋ฃนํ™”๋  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์— ๋Œ€ํ•ด ์ƒ์„ฑ

 

ex) QUERY ์‚ฌ๋ก€

SELECT ์„ฑ๋ณ„, ์—ฐ๋ น, SUM(๊ฒฐ์ œ๊ธˆ์•ก)

FROM ๊ฒฐ์ œ

GROUP BY GROUPING SETS(์„ฑ๋ณ„, ์—ฐ๋ น๋Œ€);

 

์™ธ์šฐ๊ธฐ!!!

  • Grouping Sets (set1, set2) :๊ทธ๋ฃนํ•‘ ์„ธํŠธ ์„ธํŠธ(UNION ALL๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ)
  • Rollup(์•ž, ๋’ค) : ๋กค์—… ์••๋’ค
  • CUBE(๋ชจ๋“ ๊ฒฝ์šฐ) : ํ๋ธŒ

 

์˜ˆ์ œ 3)

 

=> ์ •๋‹ต!! 2๋ฒˆ

CUBE๋Š” ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์กฐํ•ฉ์˜ ๊ฒฝ์šฐ

 

์˜ˆ์ œ 4)

=> ์ •๋‹ต!! 1, 2๋ฒˆ

1๋ฒˆ => ROLLUP์— ํ•ด๋‹นํ•˜๋Š” ์„ค๋ช…

2๋ฒˆ => ์ง‘๊ณ„ ๋Œ€์ƒ ์ปฌ๋Ÿผ ๊ฐ’์€ NULL์ด ์•„๋‹ˆ๋ผ ๊ธฐ์ค€์ด ๋˜๋Š” ๊ฐ’์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค

 

 

3) TOP(N) ์ฟผ๋ฆฌ

: ํ…Œ์ด๋ธ”์—์„œ N ๊ฐœ์˜ ํ–‰๋งŒ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ๋‹ค!

 

1. ORACLE ๊ธฐ์ค€ : ROWNUM ์Šˆ๋„ ์นผ๋Ÿผ
 => ์ปฌ๋Ÿผ๊ณผ ๋น„์Šทํ•œ ์„ฑ๊ฒฉ์˜ Pseudo Column ์œผ๋กœ์„œ, SQL ์ฒ˜๋ฆฌ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์˜ ๊ฐ ํ–‰์— ๋Œ€ํ•ด ์ž„์‹œ์ ์œผ๋กœ ๋ถ€์—ฌ๋˜๋Š” ์ผ๋ จ๋ฒˆํ˜ธ

SELECT *
FROM C_INFO
WHERE ROWNUM <= 100;

** (์˜ค๋ฅ˜) C_INFO ํ…Œ์ด๋ธ”์—์„œ ๋‚˜์ด๊ฐ€ ์–ด๋ฆฐ 100๋ช…์„ ์กฐํšŒํ•˜๋ ค๋ฉด?
SELECT *
FROM C_INFO
WHERE ROWNUM <= 100
ORDER BY AGE;
=> C_INFO์—์„œ ๋ฌด์ž‘์œ„๋กœ ์ถ”์ถœ๋œ 100๊ฐœ์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ AGE ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๊ฒƒ๊ณผ ๊ฐ™๋‹ค

** (์ •๋‹ต) C_INFO ํ…Œ์ด๋ธ”์—์„œ ๋‚˜์ด๊ฐ€ ์–ด๋ฆฐ 100๋ช…์„ ์กฐํšŒํ•˜๋ ค๋ฉด?
=> ์ธ๋ผ์ธ ๋ทฐ์—์„œ ๋จผ์ € ์ •๋ ฌํ•˜๊ธฐ!
SELECT *
FROM (SELECT *
            FROM C_INFO
            ORDER BY AGE)
WHERE ROWNUM <= 100;

 

 

2. SQL SERVER ๊ธฐ์ค€ : TOP ์ ˆ

** C_INFO ํ…Œ์ด๋ธ”์—์„œ ๋‚˜์ด๊ฐ€ ์–ด๋ฅธ 100๋ช…์„ ์กฐํšŒํ•˜๋ ค๋ฉด?
SELECT TOP(100)
              NAME, AGE
FROM C_INFO
ORDER BY AGE;

** WITH TIES
=> TOP N์˜ ๋งˆ์ง€๋ง‰ ํ–‰๊ณผ ๋™์ผํ•œ ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ถ”๊ฐ€ ํ–‰์ด ์ถœ๋ ฅ๋จ
SELECT TOP(2) WITH TIES
               NAME, AGE
FROM C_INFO
ORDER BY AGE;

=> C_INFO ํ…Œ์ด๋ธ”์—์„œ AGE๊ฐ€ ๋‚ฎ์€ 2๋ช…์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•˜๋Š”๋ฐ, ๊ฐ™์€ AGE์ธ ๊ณ ๊ฐ์ด ์ž‡์œผ๋ฉด ๊ฐ™์ด ์ถœ๋ ฅํ•œ๋‹ค
=> ORDER BY๊ฐ€ ์ง€์ •๋œ ๊ฒฝ์šฐ์—๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

 

 

3. ROW LIMITING ์ ˆ(ANSI ํ‘œ์ค€ SQL)

** C_INFO ํ…Œ์ด๋ธ”์—์„œ ๋‚˜์ด๊ฐ€ ์–ด๋ฆฐ 100๋ช…์„ ์กฐํšŒํ•˜๋ ค๋ฉด?
SELECT NAME, AGE
FROM C_INFO
ORDER BY AGE FETCH FIRST 100 ROWS ONLY;

* FETCH : ๋ฐ˜ํ™˜ํ•  ๊ฒƒ ์ง€์ •ํ•ด์คŒ
* ONLY : ์ง€์ •ํ•œ ๋งŒํผ(with ties ๋งˆ์ง€๋ง‰ํ–‰ ๋™์ˆœ์œ„ ํฌํ•จ)

- ORACLE 
SELECT *
FROM (SELECT *
             FROM C_INFO
             ORDER BY AGE )
WHERE ROWNUM <= 100;

- SQL SERVER
SELECT TOP(100)
               NAME, AGE
FROM C_INFO
ORDER BY AGE;

**์ƒ์œ„ 100๊ฐœ ํ–‰์„ ๊ฑด๋„ˆ๋›ด ์ดํ›„์˜ ์ „์ฒด ํ–‰ ๋ฐ˜ํ™˜ํ•˜๋ ค๋ฉด?
SELECT NAME, AGE FROM C_INFO ORDER BY AGE OFFSET 100 ROWS;

* OFFSET : ๊ฑด๋„ˆ๋›ธ ๊ฒƒ ์ง€์ •ํ•ด์คŒ

 

 

3. ์œˆ๋„์šฐ ํ•จ์ˆ˜ & ํŒŒํ‹ฐ์…˜

 

WINDOW ํ•จ์ˆ˜

: ๋ ˆ์ฝ”๋“œ(ํ–‰) ์‚ฌ์ด์˜ ๊ด€๊ณ„๋ฅผ ์‰ฝ๊ฒŒ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜

๋งค์žฅ ์ฝ”๋“œ ์ง€์—ญ๋ช… ๋ฉ”๋‰ด๋ช… ํŒ๋งค๋Ÿ‰ ํŒ๋งค์ˆœ์œ„
101 ์„œ์šธ ์—ฐ์–ด์Šค์‹œ 500 1
101 ์„œ์šธ ์ฐธ์น˜์Šค์‹œ 400 2
101 ์„œ์šธ ์™€๊ทœ 300 3
102 ๋ถ€์‚ฐ ์—ฐ์–ด์Šค์‹œ 600 1
102 ๋ถ€์‚ฐ ํŠ€๊น€ 300 2

 

SELECT WINDOW_FUNCTION (ARGUMENTS) OVER ( [PARTITON BY ์ปฌ๋Ÿผ๋ช…] [ORDER BY ์ปฌ๋Ÿผ๋ช…] [WINDOWING] ) FROM ํ…Œ์ด๋ธ”๋ช…;

- WINDOW_FUNCTION : ์œˆ๋„์šฐ ํ•จ์ˆ˜
- ARGUMENTS : ์ธ์ˆ˜(์ปฌ๋Ÿผ๋ช… ๋“ฑ ํ•จ์ˆ˜์˜ ์ž‘์—…์ด ์ด๋ค„์ง€๋Š” ๋Œ€์ƒ)
- PARTITION BY : ํ…Œ์ด๋ธ”์˜ ๋ ˆ์ฝ”๋“œ๋“ค์„ ์ชผ๊ฐœ๋Š” ๊ธฐ์ค€
- ORDER BY : ์ชผ๊ฐœ์ง„ ๋ ˆ์ฝ”๋“œ๋“ค ๋‚ด์—์„œ ํ˜น์€ ์ „์ฒด ํ…Œ์ด๋ธ”์—์„œ ๋ ˆ์ฝ”๋“œ๋“ค์„ ์–ด๋–ค ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ• ์ง€
- WINDOWING : ํ•จ์ˆ˜์˜ ์—ฐ์‚ฐ ๋Œ€์ƒ์ด ๋˜๋Š” ๋ ˆ์ฝ”๋“œ์˜ ๋ฒ”์œ„๋ฅผ ์ •ํ•จ

 

* WINDOW_FUNCTION ์ข…๋ฅ˜

- ๊ทธ๋ฃน ๋‚ด ์ง‘๊ณ„ ํ•จ์ˆ˜ : COUNT, SUM, MIN, MAX, AVG๋“ฑ

- ๊ทธ๋ฃน ๋‚ด ์ˆœ์œ„(RANK) ํ•จ์ˆ˜

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

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

 

 

WINDOW ํ•จ์ˆ˜ - ๊ทธ๋ฃน ๋‚ด RANK ํ•จ์ˆ˜

RANK - ๋™์ผํ•œ ์ˆœ์œ„์— ๋Œ€ํ•ด ๋™์ผํ•œ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•œ๋‹ค
- ๋™์ผํ•œ ์ˆœ์œ„๋ฅผ ํ•˜๋‚˜์˜ ๊ฑด์ˆ˜๋กœ ๊ณ„์‚ฐํ•˜์ง€ ์•Š๋Š”๋‹ค (1 / 2, 2 / 4)
DENSE_RANK - ๋™์ผํ•œ ์ˆœ์œ„์— ๋Œ€ํ•ด ๋™์ผํ•œ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•œ๋‹ค
- ๋™์ผํ•œ ์ˆœ์œ„๋Š” ํ•˜๋‚˜์˜ ๊ฑด์ˆ˜๋กœ ๊ณ„์‚ฐํ•œ๋‹ค (1 / 2, 2 / 3)
ROW_NUMBER - ๋™์ผํ•œ ์ˆœ์œ„์— ๋Œ€ํ•ด ๊ณ ์œ ํ•œ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•œ๋‹ค (1 / 2 / 3 / 4 )

**RANK์™€ DENSE_RANK ์ฐจ์ด ๋น„๊ต!!

 

์˜ˆ์‹œ1)

 

=> ์ •๋‹ต!! 

 

A : DENSE_RANK

B : RANK

C : ROW_NUMBER

 

์˜ˆ์‹œ2) 

 

=> ์ •๋‹ต!!  1๋ฒˆ (์—ฌํƒ€ SQL๋ฌธ๊ณผ ๋™์ผํ•˜๊ฒŒ ORDER BY๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ค๋ฆ„์ฐจ์ˆœ, ๋‚ด๋ฆผ์ฐจ์ˆœ ์ถœ๋ ฅ์ด ๊ฐ€๋Šฅ / ์ˆœ์œ„๋กœ ์ž๋™์œผ๋กœ ์ •๋ ฌ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹˜)

 

์˜ˆ์‹œ 3)

 

=> ์ •๋‹ต!!

 

SELECT ๋งค์žฅ ์ฝ”๋“œ, ์ง€์—ญ๋ช…, ๋ฉ”๋‰ด๋ช…, ํŒ๋งค๋Ÿ‰

                , DENSE_RANK () OVER (PARTITION BY ์ง€์—ญ๋ช… ORDER BY ํŒ๋งค๋Ÿ‰ DESC) AS

            ํŒ๋งค์ˆœ์œ„

FROM ํŒ๋งค

 

 

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

PERCENT_RANK - ๊ฐ’์ด ์•„๋‹Œ, ์ˆœ์„œ๋ฅผ ๋Œ€์ƒ์œผ๋กœ, ํŒŒํ‹ฐ์…˜ ๋‚ด์—์„œ์˜ ์ˆœ์„œ๋ณ„ ๋ฐฑ๋ถ„์œจ์„ ์กฐํšŒํ•จ
NTILE(n) - ํŒŒํ‹ฐ์…˜๋ณ„๋กœ ์ „์ฒด ๊ฑด์ˆ˜๋ฅผ n๋“ฑ๋ถ„ํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. n=4์ด๋ฉด 4๋“ฑ๋ถ„ํ•œ ๊ฒƒ ๋‚ด์—์„œ ๋ช‡์ธ์ง€ ์กฐํšŒ
CUME_DIST - ํŒŒํ‹ฐ์…˜ ๋‚ด ์ „์ฒด์—์„œ ํ˜„์žฌ ํ–‰์˜ ๊ฐ’ ์ดํ•˜์ธ ๋ ˆ์ฝ”๋“œ ๊ฑด์ˆ˜์— ๋Œ€ํ•ด ๋ˆ„์  ๋ฐฑ๋ถ„์œจ์„ ์กฐํšŒ
- ๋ˆ„์  ๋ถ„ํฌ ์ƒ์— 0~1 ๊ฐ’์„ ๊ฐ€์ง

 

 

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

FIRST_VALUE - ํŒŒํ‹ฐ์…˜ ๋‚ด์—์„œ ๊ฐ€์žฅ ์ฒ˜์Œ ๋‚˜์˜ค๋Š” ๊ฐ’ ๋ฐ˜ํ™˜
- MIN๊ณผ ๋™์ผํ•œ ๊ฒฐ๊ณผ
LAST_VALUE - ํŒŒํ‹ฐ์…˜ ๋‚ด์—์„œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋‚˜์˜ค๋Š” ๊ฐ’ ๋ฐ˜ํ™˜
- MAX์™€ ๋™์ผํ•œ ๊ฒฐ๊ณผ
LAG(์ปฌ๋Ÿผ๋ช…, ๋ ˆ์ฝ”๋“œ ์œ„์น˜ ์ฐจ์ด๊ฐ’) - ์ด์ „ ํ–‰์„ ๊ฐ€์ ธ์˜จ๋‹ค
LEAD(์ปฌ๋Ÿผ๋ช…, ๋ ˆ์ฝ”๋“œ ์œ„์น˜ ์ฐจ์ด๊ฐ’,  null์ผ ๊ฒฝ์šฐ ๋Œ€์ฒด๊ฐ’) - ๋‹ค์Œ(ํŠน์ • ์œ„์น˜์˜) ํ–‰์„ ๊ฐ€์ ธ์˜จ๋‹ค
- default๋Š” 1์ด๋‹ค

 

์˜ˆ์ œ4)

 

 

=> ์ •๋‹ต (ใ„ฑ : E,  ใ„ด : E)

 

 

Window ํ•จ์ˆ˜ ์ค‘ WINDOWING

SELECT WINDOW _FUNCTION (ARGUMENTS) OVER ( [PARTITION BY ์ปฌ๋Ÿผ๋ช…] [ORDER BY ์ปฌ๋Ÿผ๋ช…] [WINDOWING] ) FROM ํ…Œ์ด๋ธ”๋ช…; 

 

- WINDOWING : ํ•จ์ˆ˜์˜ ์—ฐ์‚ฐ ๋Œ€์ƒ์ด ๋˜๋Š” ๋ ˆ์ฝ”๋“œ์˜ ๋ฒ”์œ„๋ฅผ ์ •ํ•จ

 

RANGE ex ) RANGE BETWEEN 100 AND 200
๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด์ค„ ๋•Œ ์‚ฌ์šฉ
BETWEEN a AND b a๋ถ€ํ„ฐ b๊นŒ์ง€ ์œˆ๋„์šฐ๊ฐ€ ์ ์šฉ๋จ
UNBOUNDED PRECEDING ์œˆ๋„์šฐ์˜ ์‹œ์ž‘ ์œ„์น˜ = ์ฒซ ๋ฒˆ์งธ ํ–‰
UNBOUNDED FOLLOWING ์œˆ๋„์šฐ์˜ ๋งˆ์ง€๋ง‰ ์œ„์น˜ = ๋งˆ์ง€๋ง‰ ํ–‰
CURRENT ROW ์œˆ๋„์šฐ ์‹œ์ž‘ ์œ„์น˜๊ฐ€ ํ˜„์žฌ ํ–‰

 

์˜ˆ์ œ5)

 

=> ์ •๋‹ต!! UNBOUNDED PRECEDING

 

 

ํ…Œ์ด๋ธ” ํŒŒํ‹ฐ์…˜

 

- ๋Œ€์šฉ๋Ÿ‰์˜ ํ…Œ์ด๋ธ”์„ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์— ๋ถ„๋ฆฌํ•˜์—ฌ ์ €์žฅํ•˜๋Š” ๊ฒƒ

๋ฌผ๋ฆฌ์ ์œผ๋กœ ๋ถ„๋ฆฌ๋œ ๋ฐ์ดํ„ฐ ํŒŒ์ผ์— ์ €์žฅ๋˜์–ด ์ž…๋ ฅ / ์ˆ˜์ • / ์‚ญ์ œ / ์กฐํšŒ ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ๋˜๊ณ , ๋…๋ฆฝ์  ๊ด€๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅ

์กฐํšŒ์˜ ๋ฒ”์œ„๋ฅผ ์ค„์ด๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ์–ด ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ๋จ

** ๋ง‰ ์—„์ฒญ ์ค‘์š”ํ•˜์ง„ ์•Š์Œ;;

 

RANGE PARTITION ๊ฐ’์˜ ๋ฒ”์œ„๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํŒŒํ‹ฐ์…˜์„ ๋‚˜๋ˆ  ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•
ex) ๋งค์ถœ์•ก์— 50000 ์ด์ƒ์ธ ๋ ˆ์ฝ”๋“œ์™€ ๋ฏธ๋งŒ์ธ ๋ ˆ์ฝ”๋“œ๋ฅผ ๋ณ„๋„๋กœ ์ €์žฅ
LIST PARTITION ํŠน์ • ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋ถ„ํ• 
ex) ํŠน์ • ๊ฐ’์ด 100์ผ ๋•Œ์™€ 250์ผ ๋•Œ ๊ฐ๊ฐ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐํŒŒ์ผ์— ์ €์žฅ
HASH PARTITION ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ฆฌ ์‹œ์Šคํ…œ์ด ์ž์ฒด์ ์œผ๋กœ ํ•ด์‹œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ถ„ํ• ํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ์‹

 

 

728x90
๋ฐ˜์‘ํ˜•

+ Recent posts