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

[SQL ์ „๋ฌธ๊ฐ€] ๊ณผ๋ชฉII. ์ œ2์žฅ SQL ํ™œ์šฉ

by yunamom 2022. 4. 14.
๋ฐ˜์‘ํ˜•

๐Ÿ“–์ œ2์žฅ SQL ํ™œ์šฉ

  • ์ œ1์ ˆ ์„œ๋ธŒ ์ฟผ๋ฆฌ
  • ์ œ2์ ˆ ์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž
  • ์ œ3์ ˆ ๊ทธ๋ฃน ํ•จ์ˆ˜
  • ์ œ4์ ˆ ์œˆ๋„์šฐ ํ•จ์ˆ˜
  • ์ œ5์ ˆ Top N ์ฟผ๋ฆฌ
  • ์ œ6์ ˆ ๊ณ„์ธตํ˜• ์งˆ์˜์™€ ์…€ํ”„ ์กฐ์ธ
  • ์ œ7์ ˆ PIVOT ์ ˆ๊ณผ UNPIVOT ์ ˆ
  • ์ œ8์ ˆ ์ •๊ทœ ํ‘œํ˜„์‹
  • ์žฅ ์š”์•ฝ
  • ์—ฐ์Šต๋ฌธ์ œ

โœจ์ œ1์ ˆ ์„œ๋ธŒ ์ฟผ๋ฆฌ(Subquery)


์„œ๋ธŒ์ฟผ๋ฆฌ๋ž€?

์„œ๋ธŒ ์ฟผ๋ฆฌ ์ข…๋ฅ˜ ์„ค๋ช…
Un-Correlated(๋น„์—ฐ๊ด€)
์„œ๋ธŒ ์ฟผ๋ฆฌ
์„œ๋ธŒ ์ฟผ๋ฆฌ๊ฐ€ ๋ฉ”์ธ ์ฟผ๋ฆฌ ์นผ๋Ÿผ์„ ๊ฐ–๊ณ  ์žˆ์ง€ ์•Š๋Š” ํ˜•ํƒœ์˜ ์„œ๋ธŒ ์ฟผ๋ฆฌ๋‹ค. ๋ฉ”์ธ ์ฟผ๋ฆฌ์— ๊ฐ’(์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ์‹คํ–‰๋œ ๊ฒฐ๊ณผ)์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์œผ๋กœ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
Correlated(์—ฐ๊ด€)
์„œ๋ธŒ ์ฟผ๋ฆฌ
์„œ๋ธŒ ์ฟผ๋ฆฌ๊ฐ€ ๋ฉ”์ธ ์ฟผ๋ฆฌ ์นผ๋Ÿผ์„ ๊ฐ–๊ณ  ์žˆ๋Š” ํ˜•ํƒœ์˜ ์„œ๋ธŒ ์ฟผ๋ฆฌ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฉ”์ธ ์ฟผ๋ฆฌ๊ฐ€ ๋จผ์ € ์ˆ˜ํ–‰๋ผ ์ฝํ˜€์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ธŒ ์ฟผ๋ฆฌ์—์„œ ์กฐ๊ฑด์ด ๋งž๋Š”์ง€ ํ™•์ธํ•˜๊ณ ์ž ํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

1. ๋‹จ์ผ ํ–‰ ์„œ๋ธŒ ์ฟผ๋ฆฌ

์„œ๋ธŒ ์ปค๋ฆฌ ์ข…๋ฅ˜ ์„ค๋ช…
Single Row ์„œ๋ธŒ ์ฟผ๋ฆฌ
(๋‹จ์ผ ํ–‰ ์„œ๋ธŒ ์ฟผ๋ฆฌ)
์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ€ ํ•ญ์ƒ 1๊ฑด ์ดํ•˜์ธ ์„œ๋ธŒ ์ฟผ๋ฆฌ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ๋‹จ์ผ ํ–‰ ์„œ๋ธŒ ์ฟผ๋ฆฌ๋Š” ๋‹จ์ผ ํ–‰ ๋น„๊ต ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋œ๋‹ค. ๋‹จ์ผ ํ–‰ ๋น„๊ต ์—ฐ์‚ฐ์ž์—๋Š” =, <, <=, >, >=, <> ๋“ฑ์ด ์žˆ๋‹ค.
Multi Row ์„œ๋ธŒ ์ฟผ๋ฆฌ
(๋‹ค์ค‘ ํ–‰ ์„œ๋ธŒ ์ฟผ๋ฆฌ)
์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฑด์ธ ์„œ๋ธŒ ์ฟผ๋ฆฌ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ๋‹ค์ค‘ ํ–‰ ์„œ๋ธŒ ์ฟผ๋ฆฌ๋Š” ๋‹ค์ค‘ํ–‰ ๋น„๊ต ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋œ๋‹ค. ๋‹ค์ค‘ ํ–‰ ๋น„๊ต ์—ฐ์‚ฐ์ž์—๋Š” IN, ALL, ANY, SOME, EXISTS๊ฐ€ ์žˆ๋‹ค.
Multi Column ์„œ๋ธŒ ์ฟผ๋ฆฌ
(๋‹ค์ค‘ ์นผ๋Ÿผ ์„œ๋ธŒ ์ฟผ๋ฆฌ)
์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋กœ ์—ฌ๋Ÿฌ ์นผ๋Ÿผ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋ฉ”์ธ ์ฟผ๋ฆฌ์˜ ์กฐ๊ฑด์ ˆ์— ์—ฌ๋Ÿฌ ์นผ๋Ÿผ์„ ๋™์‹œ์— ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋‹ค. ์„œ๋ธŒ ์ฟผ๋ฆฌ์™€ ๋ฉ”์ธ ์ฟผ๋ฆฌ์—์„œ ๋น„๊ตํ•˜๊ณ ์ž ํ•˜๋Š” ์นผ๋Ÿผ ๊ฐœ์ˆ˜์™€ ์นผ๋Ÿผ์˜ ์œ„์น˜๊ฐ€ ๋™์ผํ•ด์•ผ ํ•œ๋‹ค.

2. ๋‹ค์ค‘ ํ–‰ ์„œ๋ธŒ ์ฟผ๋ฆฌ

๋‹ค์ค‘ ํ–‰ ์—ฐ์‚ฐ์ž ์„ค๋ช…
IN(์„œ๋ธŒ ์ฟผ๋ฆฌ) ์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ์— ์กด์žฌํ•˜๋Š” ์ž„์˜์˜ ๊ฐ’๊ณผ ๋™์ผํ•œ ์กฐ๊ฑด์„ ์˜๋ฏธํ•œ๋‹ค(Multiple OR ์กฐ๊ฑด)
๋น„๊ต์—ฐ์‚ฐ์ž
ALL(์„œ๋ธŒ ์ฟผ๋ฆฌ)
์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๊ฐ’์„ ๋งŒ์กฑํ•˜๋Š” ์กฐ๊ฑด์„ ์˜๋ฏธํ•œ๋‹ค. ๋น„๊ต ์—ฐ์‚ฐ์ž๋กœ '>'๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค๋ฉด ๋ฉ”์ธ ์ฟผ๋ฆฌ๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ๋ชจ๋“  ๊ฒฐ๊ณผ ๊ฐ’์„ ๋งŒ์กฑํ•ด์•ผ ํ•˜๋ฏ€๋กœ, ์„œ๋ธŒ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์˜ ์ตœ๋Œ“๊ฐ’๋ณด๋‹ค ํฐ ๋ชจ๋“  ๊ฑด์ด ์กฐ๊ฑด์„ ๋งŒ์กฑํ•œ๋‹ค.
๋น„๊ต์—ฐ์‚ฐ์ž
ANY(์„œ๋ธŒ ์ฟผ๋ฆฌ)
์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ์— ์กด์žฌํ•˜๋Š” ์–ด๋Š ํ•˜๋‚˜์˜ ๊ฐ’์ด๋ผ๋„ ๋งŒ์กฑํ•˜๋Š” ์กฐ๊ฑด์„ ์˜๋ฏธํ•œ๋‹ค. ๋น„๊ต ์—ฐ์‚ฐ์ž๋กœ '>'๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค๋ฉด ๋ฉ”์ธ ์ฟผ๋ฆฌ๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ๊ฐ’๋“ค ์ค‘ ์–ด๋–ค ๊ฐ’์ด๋ผ๋„ ๋งŒ์กฑํ•˜๋ฉด ๋˜๋ฏ€๋กœ, ์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ์˜ ์ตœ์†Œ๊ฐ’๋ณด๋‹ค ํฐ ๋ชจ๋“  ๊ฑด์ด ์กฐ๊ฑด์„๋งŒ์กฑํ•œ๋‹ค(SOME์€ ANY์™€ ๋™์ผํ•จ)
EXISTS(์„œ๋ธŒ ์ฟผ๋ฆฌ) ์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š” ์กฐ๊ฑด์„ ์˜๋ฏธํ•œ๋‹ค. ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฑด์ด ์—ฌ๋Ÿฌ ๊ฑด์ด๋”๋ผ๋„ 1๊ฑด๋งŒ ์ฐพ์œผ๋ฉด ๋”์ด์ƒ ๊ฒ€์ƒ‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.

3. ๋‹ค์ค‘ ์นผ๋Ÿผ ์„œ๋ธŒ ์ฟผ๋ฆฌ

๋‹ค์ค‘ ์นผ๋Ÿผ ์„œ๋ธŒ ์ฟผ๋ฆฌ๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์นผ๋Ÿผ์ด ๋ฐ˜ํ™˜๋ผ ๋ฉ”์ธ ์ฟผ๋ฆฌ์˜ ์กฐ๊ฑด๊ณผ ๋™์‹œ์— ๋น„๊ต๋˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. SQL Server์—์„œ๋Š” ์ง€์›Œ๋˜์ง€ ์•Š๋Š” ๋ฌธ๋ฒ•์ด๋‹ค.

์†Œ์†ํŒ€๋ณ„ ํ‚ค๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์‚ฌ๋žŒ๋“ค์˜ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ๊ฐ–๊ณ  ๋‹ค์ค‘ ์นผ๋Ÿผ ์„œ๋ธŒ ์ฟผ๋ฆฌ๋ฅผ ์•Œ์•„๋ณด๋„๋ก ํ•œ๋‹ค. ์†Œ์†ํŒ€๋ณ„ ํ‚ค๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์‚ฌ๋žŒ๋“ค์˜ ์ •๋ณด๋Š” GROUP BY๋ฅผ ์ด์šฉํ•ด ์ฐพ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด SQL๋ฌธ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

SELECT TEAM_ID AS ํŒ€์ฝ”๋“œ, PLAYER_NAME AS ์„ ์ˆ˜๋ช…, POSITION AS ํฌ์ง€์…˜
     , BACK_NO AS ๋ฐฑ๋„˜๋ฒ„, HEIGHT AS ํ‚ค
    FROM PLAYER
  WHERE (TEAM_ID, HEIGHT) IN (SELECT TEAM_ID, MIN(HEIGHT)
                                            FROM PLAYER
                                     GROUP BY TEAM_ID)
ORDER BY TEAM_ID, PLAYER_NAME;

 

4. ์—ฐ๊ด€ ์„œ๋ธŒ ์ฟผ๋ฆฌ

์—ฐ๊ด€ ์„œ๋ธŒ ์ฟผ๋ฆฌ(Correlated Subquery)๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ ๋‚ด์— ๋ฉ”์ธ ์ฟผ๋ฆฌ ์นผ๋Ÿผ์ด ์‚ฌ์šฉ๋œ ์„œ๋ธŒ ์ฟผ๋ฆฌ๋‹ค.

์„ ์ˆ˜ ์ž์‹ ์ด ์†ํ•œ ํŒ€์˜ ํ‰๊ท ํ‚ค๋ณด๋‹ค ์ž‘์€ ์„ ์ˆ˜๋“ค์˜ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” SQL ๋ฌธ์„ ์—ฐ๊ด€ ์„œ๋ธŒ ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž‘์„ฑํ•ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

SELECT B.TEAM_NAME AS ํŒ€๋ช…, A.PLAYER_NAME AS ์„ ์ˆ˜๋ช…, A.POSITION AS ํฌ์ง€์…˜
     , A.BACK_NO AS ๋ฐฑ๋„˜๋ฒ„, A.HEIGHT AS ํ‚ค
    FROM PLAYER A, TEAM B
  WHERE A.HEIGHT < (SELECT AVG(X.HEIGHT)
                           FROM PLAYER
                         WHERE X.TEAM_ID = A.TEAM_ID
                      GROUP BY X.TEAM_ID)
  AND B.TEAM_ID = A.TEAM_ID
ORDER BY ์„ ์ˆ˜๋ช…;

 

EXISTS ์„œ๋ธŒ ์ฟผ๋ฆฌ๋Š” ํ•ญ์ƒ ์—ฐ๊ด€ ์„œ๋ธŒ ์ฟผ๋ฆฌ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ๋˜ํ•œ EXISTS ์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ํŠน์ง•์€ ์•„๋ฌด๋ฆฌ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฑด์ด ์—ฌ๋Ÿฌ ๊ฑด์ด๋”๋ผ๋„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” 1๊ฑด๋งŒ ์ฐพ์œผ๋ฉด ์ถ”๊ฐ€์ ์ธ ๊ฒ€์ƒ‰์„ ์ง„ํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.

๋‹ค์Œ์€ EXISTS ์„œ๋ธŒ ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด '20220501'๋ถ€ํ„ฐ '20220502' ์‚ฌ์ด์— ๊ฒฝ๊ธฐ๊ฐ€ ์žˆ๋Š” ๊ฒฝ๊ธฐ์žฅ์„ ์กฐํšŒํ•˜๋Š” SQL ๋ฌธ์ด๋‹ค.

SELECT A.STADIUM_ID AS ID, A.STADIUM_NAME AS ๊ฒฝ๊ธฐ์žฅ๋ช…
    FROM STADIUM A
  WHERE EXISTS (SELECT 1 
                   FROM SCHEDULE X
                 WHERE X.STADIUM_ID = A.STADIUM_ID
                   AND X.SCHE_DATE BETWEEN '20220501' AND '20220502');

5. ๊ทธ ๋ฐ–์˜ ์œ„์น˜์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ

์ข…๋ฅ˜ ์„ค๋ช…
SELECT ์ ˆ - ์Šค์นผ๋ผ ์„œ๋ธŒ ์ฟผ๋ฆฌ(Scallar Subquery) ํ•œ ํ–‰, ํ•œ ์นผ๋Ÿผ๋งŒ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ **
๋‹จ์ผ ํ–‰ ์„œ๋ธŒ ์ฟผ๋ฆฌ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ๊ฐ€ 2๊ฑด ์ด์ƒ ๋ฐ˜ํ™˜๋˜๋ฉด SQL ๋ฌธ์€ ์˜ค๋ฅ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
FROM ์ ˆ - ์ธ๋ผ์ธ ๋ทฐ(Inline View) (์ค‘์š”) ์ธ๋ผ์ธ ๋ทฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์„œ๋ธŒ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋งˆ์น˜ ํ…Œ์ด๋ธ”์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ธ๋ผ์ธ ๋ทฐ๋Š” ์ฟผ๋ฆฌ ๋‚ด์—์„œ ์ฆ‰์‹œ ์ฒ˜๋ฆฌ๋œ๋‹ค.
(๊ฒŒ์‹œํŒ ํŽ˜์ด์ง• ํ• ๋•Œ ์œ ์šฉํ•˜๋‹ค ์ž์„ธํžˆ ์•Œ์•„๋‘๊ธฐ!)
HAVING ์ ˆ  ์ง‘๊ณ„ํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋  ๋•Œ ๊ทธ๋ฃนํ•‘ ๋œ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด ๋ถ€๊ฐ€์ ์ธ ์กฐ๊ฑด์„ ์ฃผ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

 

6. ๋ทฐ(View)

ํ…Œ์ด๋ธ”์€ ์‹ค์ œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” ๋ฐ˜๋ฉด, ๋ทฐ(View)๋Š” ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ์ง€ ์•Š๋‹ค. ๋ทฐ๋Š” ๋‹จ์ง€ ๋ทฐ ์ •์˜(View Definition)๋งŒ์„ ๊ฐ–๊ณ  ์žˆ๋‹ค. ์งˆ์˜์—์„œ ๋ทฐ๊ฐ€ ์‚ฌ์šฉ๋˜๋ฉด ๋ทฐ ์ •์˜๋ฅผ ์ฐธ์กฐํ•ด์„œ DBMS ๋‚ด๋ถ€์ ์œผ๋กœ ์งˆ์˜๋ฅผ ์žฌ์ž‘์„ฑ(Rewrite)ํ•ด ์งˆ์˜๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค.

๋ทฐ์˜ ์žฅ์  ์„ค๋ช…
๋…๋ฆฝ์„ฑ ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๊ฐ€ ๋ณ€๊ฒฝ๋ผ๋„ ๋ทฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์€ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
ํŽธ๋ฆฌ์„ฑ ๋ณต์žกํ•œ ์งˆ์˜๋ฅผ ๋ทฐ๋กœ ์ƒ์„ฑํ•จ์œผ๋กœ์จ ๊ด€๋ จ ์งˆ์˜๋ฅผ ๋‹จ์ˆœํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ํ•ด๋‹น ํ˜•ํƒœ์˜ SQL๋ฌธ์„ ์ž์ฃผ ์‚ฌ์šฉํ•  ๋•Œ ๋ทฐ๋ฅผ ์ด์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
๋ณด์•ˆ์„ฑ ์ง์›์˜ ๊ธ‰์—ฌ์ •๋ณด์™€ ๊ฐ™์ด ์ˆจ๊ธฐ๊ณ  ์‹ถ์€ ์ •๋ณด๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด, ๋ทฐ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ•ด๋‹น ์นผ๋Ÿผ์„ ๋นผ๊ณ  ์ƒ์„ฑํ•จ์œผ๋กœ์จ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ •๋ณด๋ฅผ ๊ฐ์ถœ ์ˆ˜ ์žˆ๋‹ค.

๋ทฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด CREATE VIEW ๋ฌธ์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

CREATE VIEW V_PLAYER_TEAM AS
SELECT A.PLAYER_NAME, A.POSITION, A.BACK_NO
     , B.TEAM_ID, B.TEAM_NAME
  FROM PLAYER A, TEAM B
WHERE B.TEAM_ID = A.TEAM_ID;

ํ•ด๋‹น ๋ทฐ๋Š” ์„ ์ˆ˜ ์ •๋ณด์™€ ํ•ด๋‹น ์„ ์ˆ˜๊ฐ€ ์†ํ•œ ํŒ€๋ช…์„ ํ•จ๊ป˜ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋ทฐ์˜ ๋ช…์นญ์€ 'V_PLAYER_TEAM'์ด๋‹ค.๋ทฐ๋Š” ํ…Œ์ด๋ธ”๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ทฐ๋ฅผ ์ฐธ์กฐํ•ด์„œ๋„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

CREATE VIEW V_PLAYER_TEAM_FILTER AS
SELECT PLAYER_NAME, POSITION, BACK_NO, TEAM_NAME
  FROM V_PLAYER_TEAM
WHERE POSITION IN ('GK', 'MF');

V_PLAYER_TEAM_FILTER ๋ทฐ๋Š” ์ด๋ฏธ ์•ž์—์„œ ์ƒ์„ฑํ–ˆ๋˜ V_PLAYER_TEAM ๋ทฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ด์„œ ์ƒ์„ฑ๋œ ๋ทฐ๋‹ค.

V_PLAYER_TEAM_FILTER ๋ทฐ๋Š” ์„ ์ˆ˜ ํฌ์ง€์…˜์ด ๊ณจํ‚คํผ(GK), ๋ฏธ๋“œํ•„๋”(MF)์ธ ์„ ์ˆ˜๋งŒ์„ ์ถ”์ถœํ•˜๊ณ ์ž ํ•˜๋Š” ๋ทฐ๋‹ค(๋ทฐ๋ฅผ ํฌํ•จํ•˜๋Š” ๋ทฐ๋ฅผ ์ž˜๋ชป ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ ์„ฑ๋Šฅ์ƒ์˜ ๋ฌธ์ œ๋ฅผ ์œ ๋ฐœํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๋ทฐ์™€ SQL์˜ ์ˆ˜ํ–‰์›๋ฆฌ๋ฅผ ์ž˜ ์ดํ•ดํ•˜๊ณ  ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.)

-- ์‚ฌ์šฉ๋ฒ•

SELECT PLAYER_NAME, POSITION, BACK_NO, TEAM_ID, TEAM_NAME
  FROM V_PLAYER_TEAM
WHERE PLAYER_NAME LIKE 'ํ™ฉ%';


-- ๋ทฐ ์ œ๊ฑฐํ•˜๊ธฐ

DROP VIEW V_PLAYER_TEAM;

 

โœจ์ œ2์ ˆ ์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž


์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž์˜ ์ข…๋ฅ˜

์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž ์—ฐ์‚ฐ์ž์˜ ์˜๋ฏธ
UNION (ํ•ฉ์ง‘ํ•ฉ)์—ฌ๋Ÿฌ ๊ฐœ์˜ SQL๋ฌธ์˜ ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ํ•ฉ์ง‘ํ•ฉ์œผ๋กœ ๊ฒฐ๊ณผ์—์„œ ๋ชจ๋“  ์ค‘๋ณต๋œ ํ–‰์€ ํ•˜๋‚˜์˜ ํ–‰์œผ๋กœ ๋งŒ๋“ ๋‹ค.
UNION ALL (ํ•ฉ์ง‘ํ•ฉ)์—ฌ๋Ÿฌ ๊ฐœ์˜ SQL๋ฌธ์˜ ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ํ•ฉ์ง‘ํ•ฉ์œผ๋กœ ์ค‘๋ณต๋œ ํ–‰๋„ ๊ทธ๋Œ€๋กœ ๊ฒฐ๊ณผ๋กœ ํ‘œ์‹œ๋œ๋‹ค. ์ฆ‰, ๋‹จ์ˆœํžˆ ๊ฒฐ๊ณผ๋งŒ ํ•ฉ์ณ๋†“์€ ๊ฒƒ์ด๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์—ฌ๋Ÿฌ ์งˆ์˜ ๊ฒฐ๊ณผ๊ฐ€ ์ƒํ˜ธ ๋ฐฐํƒ€์ ์ธ(Exclusive)์ผ๋•Œ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค. ๊ฐœ๋ณ„ SQL๋ฌธ์˜ ๊ฒฐ๊ณผ๊ฐ€ ์„œ๋กœ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ, UNION๊ณผ ๊ฒฐ๊ณผ๊ฐ€ ๋™์ผํ•˜๋‹ค. (๊ฒฐ๊ณผ์˜ ์ •๋ ฌ ์ˆœ์„œ์—๋Š” ์ฐจ์ด๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Œ)
INTERSECT (๊ต์ง‘ํ•ฉ)์—ฌ๋Ÿฌ ๊ฐœ์˜ SQL๋ฌธ์˜ ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ๊ต์ง‘ํ•ฉ์ด๋‹ค. ์ค‘๋ณต๋œ ํ–‰์€ ํ•˜๋‚˜์˜ ํ–‰์œผ๋กœ ๋งŒ๋“ ๋‹ค.
EXCEPT (์ฐจ์ง‘ํ•ฉ)์•ž์˜ SQL๋ฌธ์˜ ๊ฒฐ๊ณผ์—์„œ ๋’ค์˜ SQL๋ฌธ์˜ ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ์ฐจ์ง‘ํ•ฉ์ด๋‹ค. ์ค‘๋ณต๋œ ํ–‰์€ ํ•˜๋‚˜์˜ ํ–‰์œผ๋กœ ๋งŒ๋“ ๋‹ค. (์ผ๋ถ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” MINUS๋ฅผ ์‚ฌ์šฉํ•จ)

์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด ๋งŒ๋“ค์–ด์ง€๋Š” SQL ๋ฌธ์˜ ํ˜•ํƒœ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

SELECT ์นผ๋Ÿผ๋ช…1, ์นผ๋Ÿผ๋ช…2, ...
	FROM ํ…Œ์ด๋ธ”๋ช…1
[ WHERE ์กฐ๊ฑด์‹ ]
[ GROUP BY ์นผ๋Ÿผ or ํ‘œํ˜„์‹ ]
[ HAVING ๊ทธ๋ฃน์กฐ๊ฑด์‹ ]
์ง‘ํ•ฉ์—ฐ์‚ฐ์ž -- ( UNION/UNION ALL/INTERSECT/MINUS or EXCEPT )
	SELECT ์นผ๋Ÿผ๋ช…1, ์นผ๋Ÿผ๋ช…2, ...
    	FROM ํ…Œ์ด๋ธ”๋ช…2
    [ WHERE ์กฐ๊ฑด์‹ ]
    [ GROUP BY ์นผ๋Ÿผ or ํ‘œํ˜„์‹ ]
    [ HAVING ๊ทธ๋ฃน์กฐ๊ฑด์‹ ]
    [ ORDER BY ์นผ๋Ÿผ or ํ‘œํ˜„์‹ [ ASC or DESC ] ];

 

โœจ์ œ3์ ˆ ๊ทธ๋ฃน ํ•จ์ˆ˜


1. ๋ฐ์ดํ„ฐ ๋ถ„์„ ๊ฐœ์š”

ANSI/ISO SQL ํ‘œ์ค€ ํ•จ์ˆ˜ ํ•จ์ˆ˜ ์ข…๋ฅ˜
AGGREGATE FUNCTION COUNT, SUM, AVG,MAX,MIN ์™ธ ๊ฐ์ข… ์ง‘๊ณ„ ํ•จ์ˆ˜๋“ค
GROUP FUNCTION ROLLUP, CUBE, GROUPING SETS
WINDOW FUNCTION RANK(์ˆœ์œ„) function, AGGREGATE(์ง‘๊ณ„) function, ํ–‰ ์ˆœ์„œ ๊ด€๋ จ ํ•จ์ˆ˜

2. ROLLUP ํ•จ์ˆ˜

์†Œ๊ทธ๋ฃน ๊ฐ„์˜ ์†Œ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ROLLUP ํ•จ์ˆ˜

-- ์ž‘์„ฑ๋ฒ• ์˜ˆ์ œ
SELECT B.DNAME,A.JOB, COUNT(*) AS EMP_CNT, SUM(A.SAL) AS SAL_SUM
	FROM EMP B, DEPT A
  WHERE B.DEPTNO = A.DEPTNO
GROUP BY ROLLUP (B.DNAME, A.JOB);

 

3. CUBE ํ•จ์ˆ˜

GROUP BY ํ•ญ๋ชฉ ๊ฐ„ ๋‹ค์ฐจ์›์ ์ธ ์†Œ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋Š” CUBE ํ•จ์ˆ˜

CUBE๋Š” ๊ฒฐํ•ฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฐ’์— ๋Œ€ํ•ด ๋‹ค์ฐจ์›์ ์ธ ์ง‘๊ณ„๋ฅผ ์ƒ์„ฑํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ ROLLUP์— ๋น„ํ•ด ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป๋Š” ์žฅ์ ์ด ์žˆ๋Š” ๋ฐ˜๋ฉด,

์‹œ์Šคํ…œ์— ๋ถ€ํ•˜๋ฅผ ๋งŽ์ด ์ฃผ๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

-- ์ž‘์„ฑ๋ฒ• ์˜ˆ์ œ
SELECT B.DNAME,A.JOB, COUNT(*) AS EMP_CNT, SUM(A.SAL) AS SAL_SUM
	FROM EMP B, DEPT A
  WHERE B.DEPTNO = A.DEPTNO
GROUP BY CUBE (B.DNAME, A.JOB);

 

4. GROUPING SETS ํ•จ์ˆ˜

์›ํ•˜๋Š” ๋ถ€๋ถ„์˜ ์†Œ๊ณ„๋งŒ ์†์‰ฝ๊ฒŒ ์ถ”์ถœํ•˜์—ฌ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋Š” GROUPING SETS ํ•จ์ˆ˜

-- ์ž‘์„ฑ๋ฒ• ์˜ˆ์ œ Oracle

SELECT CASE GROUPING (B.DNAME) 
            WHEN 1 THEN 'All Departments' 
            ELSE B.DNAME END AS DNAME
      ,CASE GROUPING (A.JOB)
            WHEN 1 THEN 'ALL Jobs'
            ELSE A.JOB END AS JOB
      ,COUNT (*) AS EMP_CNT, SUM (A.SAL) AS SAL_SUM
    FROM EMP A, DEPT B
  WHERE B.DEPTNO = A.DEPTNO
GROUP BY GROUPING SETS (B.DNAME, A.JOB)
ORDER BY B.DNAME, A.JOB;

 

โœจ์ œ4์ ˆ ์œˆ๋„์šฐ ํ•จ์ˆ˜


1. ์œˆ๋„์šฐ ํ•จ์ˆ˜ ๊ฐœ์š”

์œˆ๋„์šฐ ํ•จ์ˆ˜ ์ข…๋ฅ˜ ์„ค๋ช…
์ˆœ์œ„(RANK) ๊ด€๋ จ ํ•จ์ˆ˜
RANK - ํŠน์ •ํ•ญ๋ชฉ ๋ฐ ํŒŒํ‹ฐ์…˜์— ๋Œ€ํ•ด์„œ ์ˆœ์œ„๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
- ๋™์ผํ•œ ์ˆœ์œ„๋Š” ๋™์ผํ•œ ๊ฐ’์ด ๋ถ€์—ฌ๋œ๋‹ค.
DENSE_RANK - ๋™์ผํ•œ ์ˆœ์œ„๋ฅผ ํ•˜๋‚˜์˜ ๊ฑด์ˆ˜๋กœ ๊ณ„์‚ฐํ•œ๋‹ค.
ROW_NUMBER - ๋™์ผํ•œ ์ˆœ์œ„์— ๋Œ€ํ•ด์„œ ๊ณ ์œ ์˜ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•œ๋‹ค.
์ง‘๊ณ„(AGGREGATE) ๊ด€๋ จ ํ•จ์ˆ˜
SUM - ํ•ฉ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
AVG - ํ‰๊ท ์„ ๊ณ„์‚ฐํ•œ๋‹ค.
COUNT - ํ–‰ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
MAX, MIN
- ์ตœ๋Œ€๊ฐ’๊ณผ ์ตœ์†Œ๊ฐ’์„ ๊ณ„์‚ฐํ•œ๋‹ค.
์ˆœ์„œ ๊ด€๋ จ ํ•จ์ˆ˜
FIRST_VALUE - ํŒŒํ‹ฐ์…˜์—์„œ ๊ฐ€์žฅ ์ฒ˜์Œ์— ๋‚˜์˜ค๋Š” ๊ฐ’์„ ๊ตฌํ•œ๋‹ค.
- MIN ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
LAST_VALUE - ํŒŒํ‹ฐ์…˜์—์„œ ๊ฐ€์žฅ ๋‚˜์ค‘์— ๋‚˜์˜ค๋Š” ๊ฐ’์„ ๊ตฌํ•œ๋‹ค.
LAG - ์ด์ „ ํ–‰์„ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค.
LEAD - ์œˆ๋„์šฐ์—์„œ ํŠน์ • ์œ„์น˜์˜ ํ–‰์„ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค.
- ๊ธฐ๋ณธ๊ฐ’์€ 1์ด๋‹ค.
๋น„์œจ ๊ด€๋ จ ํ•จ์ˆ˜

CUME_DIST - ํŒŒํ‹ฐ์…˜ ์ „์ฒด ๊ฑด์ˆ˜์—์„œ ํ˜„์žฌ ํ–‰๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฑด์ˆ˜์— ๋Œ€ํ•œ ๋ˆ„์  ๋ฐฑ๋ถ„์œจ์„ ์กฐํšŒํ•œ๋‹ค.
- ๋ˆ„์  ๋ถ„ํฌ์ƒ์— ์œ„์น˜๋ฅผ 0~1์‚ฌ์ด์˜ ๊ฐ’์„ ๊ฐ€์ง„๋‹ค.
PERCENT_RANK - ํŒŒํ‹ฐ์…˜์—์„œ ์ œ์ผ ๋จผ์ € ๋‚˜์˜จ ๊ฒƒ์„ 0์œผ๋กœ ์ œ์ผ ๋Šฆ๊ฒŒ ๋‚˜์˜จ ๊ฒƒ์„ 1๋กœ ํ•˜์—ฌ ๊ฐ’์ด ์•„๋‹Œ ํ–‰์˜ ์ˆœ์„œ๋ณ„ ๋ฐฑ๋ถ„์œจ์„ ์กฐํšŒํ•œ๋‹ค.
NTILE ํŒŒํ‹ฐ์…˜ ๋ณ„๋กœ ์ „์ฒด ๊ฑด์ˆ˜๋ฅผ ARGUMENT ๊ฐ’์œผ๋กœ N๋“ฑ๋ถ„ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์กฐํšŒํ•œ๋‹ค.
RATIO_TO_REPORT ํŒŒํ‹ฐ์…˜ ๋‚ด์— ์ „์ฒด SUM(์นผ๋Ÿผ)์— ๋Œ€ํ•œ ํ–‰ ๋ณ„ ์นผ๋Ÿผ ๊ฐ’์˜ ๋ฐฑ๋ถ„์œจ์„ ์†Œ์ˆ˜์ ๊นŒ์ง€ ์กฐํšŒํ•œ๋‹ค.
ํ†ต๊ณ„ ๋ถ„์„ ๊ด€๋ จ ํ•จ์ˆ˜ CORR, COVAR_POP, COVAR_SAMP, STDDEV, STDDEV_POP, STDDEV_SAMP, VARIANCE, VAR_POP, VAR_SAMP, REGR_(LINEAR REGRESSION), REGR_SLOPE, REGR_INTERCEPT, REGR_COUNT, REGR_R2, REGR_AVGX, REGR_AVGY, REGR_SXX, REGR_SYY, REGR_SXY

โ—‡ ์œˆ๋„์šฐ ํ•จ์ˆ˜ ๊ตฌ์กฐ

-- ์œˆ๋„์šฐ ํ•จ์ˆ˜์—๋Š” OVER ๋ฌธ๊ตฌ๊ฐ€ ํ‚ค์›Œ๋“œ๋กœ ํ•„์ˆ˜ ํฌํ•จ๋œ๋‹ค.

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

 

โœจ์ œ5์ ˆ Top N ์ฟผ๋ฆฌ


1. ROWNUM ์Šˆ๋„ ์นผ๋Ÿผ

Oracle์˜ ROWNUM์€ ์นผ๋Ÿผ๊ณผ ๋น„์Šทํ•œ ์„ฑ๊ฒฉ์˜ Pseudo Column์œผ๋กœ์„œ SQL ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์˜ ๊ฐ ํ–‰์— ๋Œ€ํ•ด ์ž„์‹œ๋กœ ๋ถ€์—ฌ๋˜๋Š” ์ผ๋ จ๋ฒˆํ˜ธ๋‹ค. ํ…Œ์ด๋ธ”์ด๋‚˜ ์ง‘ํ•ฉ์—์„œ ์›ํ•˜๋Š” ๋งŒํผ์˜ ํ–‰๋งŒ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์„ ๋•Œ WHERE ์ ˆ์—์„œ ํ–‰์˜ ๊ฐœ์ˆ˜๋ฅผ ์ œํ•œํ•˜๋Š” ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. (์˜ˆ : ๊ฒŒ์‹œ๊ธ€)

-- ํ•œ ๊ฑด์˜ ํ–‰๋งŒ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์„ ๋•Œ ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

SELECT PLAYER_NAME FROM PLAYER WHERE ROWNUM <= 1;
SELECT PLAYER_NAME FROM PLAYER WHERE ROWNUM < 2;
-- ๋‘ ๊ฑด ์ด์ƒ์˜ N ํ–‰์„ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์„ ๋•Œ๋Š” ์ถœ๋ ฅ๋˜๋Š” ํ–‰์˜ ํ•œ๊ณ„๋ฅผ ์ง€์ •ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

SELECT PLAYER_NAME FROM PLAYER WHERE ROWNUM <= N;
SELECT PLAYER_NAME FROM PLAYER WHERE ROWNUM < N+1;

 

[์˜ˆ์ œ] ์‚ฌ์› ํ…Œ์ด๋ธ”์—์„œ ๊ธ‰์—ฌ๊ฐ€ ๋†’์€ 3๋ช…๋งŒ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

-- [์˜ˆ์ œ] Oracle

SELECT ENAME, SAL
	FROM (SELECT ENAME, SAL
    		FROM EMP
          ORDER BY SAL DESC)
WHERE ROWNUM <= 3;


-- [์ถœ๋ ฅ ํ™”๋ฉด]

ENAME			SAL
-----			----
KING			5000
SCOTT			3000
FORD			3000

 

2. TOP ์ ˆ

SQL Server๋Š” TOP์ ˆ์„ ์‚ฌ์šฉํ•ด ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์œผ๋กœ ์ถœ๋ ฅ๋˜๋Š” ํ–‰์˜ ์ˆ˜๋ฅผ ์ œํ•œํ•  ์ˆ˜ ์žˆ๋‹ค.

TOP (Expression) [PERCENT] [WITH TIES]
TOP ์ ˆ์˜ ํ‘œํ˜„์‹ ์„ค๋ช…
Expression ๋ฐ˜ํ™˜ํ•  ํ–‰ ์ˆ˜๋ฅผ ์ง€์ •ํ•˜๋Š” ์ˆซ์ž์ด๋‹ค.
PERCENT ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์—์„œ ์ฒ˜์Œ Expression%์˜ ํ–‰๋งŒ ๋ฐ˜ํ™˜๋จ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
WITH TIES ORDER BY ์ ˆ์ด ์ง€์ •๋œ ๊ฒฝ์šฐ์—๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, TOP N(PERCENT)์˜ ๋งˆ์ง€๋ง‰ ํ–‰๊ณผ ๊ฐ™์€ ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ถ”๊ฐ€ ํ–‰์ด ์ถœ๋ ฅ๋˜๋„๋ก ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.(๋™์ผ ์ˆ˜์น˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€๋กœ ๋” ์ถ”์ถœํ•œ๋‹ค.)

 

[์˜ˆ์ œ] ์‚ฌ์› ํ…Œ์ด๋ธ”์—์„œ ๊ธ‰์—ฌ๊ฐ€ ๋†’์€ 2๋ช…์„ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

-- [์˜ˆ์ œ] SQL Server

SELECT TOP(2)
       ENAME, SAL
   FROM EMP
ORDER BY SAL DESC;

-- [์ถœ๋ ฅ ํ™”๋ฉด]

ENAME		SAL
-----		----
KING		5000
SCOTT		3000

[์˜ˆ์ œ] ์‚ฌ์› ํ…Œ์ด๋ธ”์—์„œ ๊ธ‰์—ฌ๊ฐ€ ๋†’์€ 2๋ช…์„ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅ, ๊ฐ™์€ ๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š” ์‚ฌ์›์ด ์žˆ์œผ๋ฉด ๊ฐ™์ด ์ถœ๋ ฅํ•˜์‹œ์˜ค.

( TOP(2) WITH TIES ์˜ต์…˜์€ ๋™์ผ ์ˆ˜์น˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€๋กœ ๋” ์ถ”์ถœํ•˜๋Š” ๊ฒƒ์œผ๋กœ, SCOTT์™€ FORD์˜ ๊ธ‰์—ฌ๊ฐ€ ๊ณต๋™ 2์œ„์ด๋ฏ€๋กœ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค. )

-- [์˜ˆ์ œ] SQL Server

SELECT TOP(2) WITH TIES
       ENAME, SAL
   FROM EMP
ORDER BY SAL DESC;

-- [์ถœ๋ ฅ ํ™”๋ฉด]

ENAME 		SAL
-----		----
KING		5000
SCOTT		3000
FORD		3000

 

3. ROW LIMITING ์ ˆ

Oracle์€ 12.1 ๋ฒ„์ „, SQL Server๋Š” 2012 ๋ฒ„์ „๋ถ€ํ„ฐ ROW LIMITING ์ ˆ๋กœ Top N ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

ROW LIMITING ์ ˆ์€ ANSI ํ‘œ์ค€ SQL ๋ฌธ๋ฒ•์ด๋‹ค.

 

์•„๋ž˜๋Š” ROW LIMITING ์ ˆ์˜ ๊ตฌ๋ฌธ์ด๋‹ค. ROW LIMITING ์ ˆ์€ ORDER BY ์ ˆ ๋‹ค์Œ์— ๊ธฐ์ˆ ํ•˜๋ฉฐ, ORDER BY ์ ˆ๊ณผ ํ•จ๊ป˜ ์ˆ˜ํ–‰๋œ๋‹ค. ROW์™€ ROWS๋Š” ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

[OFFSET offset {ROW | ROWS}]
[FETCH {FIRST | NEXT} [{rowcount | percent PERCENT}] {ROW | ROWS} {ONLY | WITH TIES}]
์ข…๋ฅ˜ ์„ค๋ช…
OFFSET offset ๊ฑด๋„ˆ๋›ธ ํ–‰์˜ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •ํ•œ๋‹ค.
FETCH ๋ฐ˜ํ™˜ํ•  ํ–‰์˜ ๊ฐœ์ˆ˜๋‚˜ ๋ฐฑ๋ถ„์œจ์„ ์ง€์ •ํ•œ๋‹ค.
ONLY ์ง€์ •๋œ ํ–‰์˜ ๊ฐœ์ˆ˜๋‚˜ ๋ฐฑ๋ถ„์œจ๋งŒํผ ํ–‰์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
WITH TIES ๋งˆ์ง€๋ง‰ ํ–‰์— ๋Œ€ํ•œ ๋™์ˆœ์œ„๋ฅผ ํฌํ•จํ•ด์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

[์˜ˆ์ œ] ์•„๋ž˜๋Š” ROW LIMITING ์ ˆ์„ ์‚ฌ์šฉํ•œ Top N ์ฟผ๋ฆฌ๋‹ค.

-- [์˜ˆ์ œ]

SELECT EMPNO, SAL FROM EMP ORDER BY SAL, EMPNO FETCH FIRST 5 ROWS ONLY;

-- [์ถœ๋ ฅ ํ™”๋ฉด]

EMPNO		SAL
-----		----
7369		800
7900		950
7876		1100
7654		1250
7521		1250

5 ํ–‰์ด ์„ ํƒ๋์Šต๋‹ˆ๋‹ค.

 

[์˜ˆ์ œ] ์•„๋ž˜์™€ ๊ฐ™์ด OFFSET๋งŒ ๊ธฐ์ˆ ํ•˜๋ฉด ๊ฑด๋„ˆ๋›ด ํ–‰ ์ดํ›„์˜ ์ „์ฒด ํ–‰์ด ๋ฐ˜ํ™˜๋œ๋‹ค.

-- [์˜ˆ์ œ]

SELECT EMPNO, SAL FROM EMP ORDER BY SAL, EMPNO OFFSET 5 ROWS;

-- [์ถœ๋ ฅ ํ™”๋ฉด]

EMPNO		SAL
-----		----
7934		1300
7844		1500
7499		1600

...

9 ํ–‰์ด ์„ ํƒ๋์Šต๋‹ˆ๋‹ค.

 

โœจ์ œ6์ ˆ ๊ณ„์ธตํ˜• ์งˆ์˜์™€ ์…€ํ”„ ์กฐ์ธ(์ค‘์š”)


1. ๊ฐœ์š”

ํ…Œ์ด๋ธ”์— ๊ณ„์ธตํ˜• ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ณ„์ธตํ˜• ์งˆ์˜(Hierarchical Query)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

๊ณ„์ธตํ˜• ๋ฐ์ดํ„ฐ๋ž€ ๋™์ผ ํ…Œ์ด๋ธ”์— ๊ณ„์ธต์ ์œผ๋กœ ์ƒ์œ„์™€ ํ•˜์œ„ ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋งํ•œ๋‹ค.

 

๊ณ„์ธตํ˜• ๋ฐ์ดํ„ฐ

์œ„์˜ ๊ทธ๋ฆผ์€ ์‚ฌ์›์— ๋Œ€ํ•œ ์ˆœํ™˜ ๊ด€๊ณ„ ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์„ ํ‘œํ˜„ํ•œ ๊ฒƒ์ด๋‹ค.

(2)๊ณ„์ธตํ˜• ๊ตฌ์กฐ์—์„œ A์˜ ํ•˜์œ„ ์‚ฌ์›์€ B, C์ด๊ณ  B ๋ฐ‘์—๋Š” ํ•˜์œ„ ์‚ฌ์›์ด ์—†๊ณ  C์˜ ํ•˜์œ„ ์‚ฌ์›์€ D, E๊ฐ€ ์žˆ๋‹ค.

๊ณ„์ธตํ˜• ๊ตฌ์กฐ๋ฅผ ๋ฐ์ดํ„ฐ๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ์ด (3)์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ๋‹ค.

์ˆœํ™˜ ๊ด€๊ณ„ ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์€ ์…€ํ”„ ์กฐ์ธ์ด๋‚˜ ๊ณ„์ธตํ˜• ์งˆ์˜๋กœ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

2. ์…€ํ”„ ์กฐ์ธ ( ์ค‘์š” ) ๐Ÿ’ก

์…€ํ”„ ์กฐ์ธ(Self Join)์ด๋ž€ ๋™์ผ ํ…Œ์ด๋ธ” ์‚ฌ์ด์˜ ์กฐ์ธ์„ ๋งํ•œ๋‹ค. ๋”ฐ๋ผ์„œ FROM ์ ˆ์— ๋™์ผ ํ…Œ์ด๋ธ”์ด ๋‘ ๋ฒˆ ์ด์ƒ ๋‚˜ํƒ€๋‚œ๋‹ค. ๋™์ผ ํ…Œ์ด๋ธ” ์‚ฌ์ด์˜ ์กฐ์ธ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ํ…Œ์ด๋ธ”๊ณผ ์นผ๋Ÿผ ์ด๋ฆ„์ด ๋ชจ๋‘ ๋™์ผํ•˜๋ฏ€๋กœ ์‹๋ณ„์„ ์œ„ํ•ด ๋ฐ˜๋“œ์‹œ ํ…Œ์ด๋ธ” ๋ณ„์นญ(Alias)์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์นผ๋Ÿผ์—๋„ ๋ชจ๋‘ ํ…Œ์ด๋ธ” ๋ณ„์นญ์„ ์‚ฌ์šฉํ•ด์„œ ์–ด๋Š ํ…Œ์ด๋ธ”์˜ ์นผ๋Ÿผ์ธ์ง€ ์‹๋ณ„ํ•ด์ค˜์•ผ ํ•œ๋‹ค. ์ด์™ธ ์‚ฌํ•ญ์€ ์กฐ์ธ๊ณผ ๋™์ผํ•˜๋‹ค.

-- ์…€ํ”„ ์กฐ์ธ์— ๋Œ€ํ•œ ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

SELECT A.์นผ๋Ÿผ, B.์นผ๋Ÿผ, ...
	FROM EMP A, EMP B
WHERE A.์นผ๋Ÿผ = B.์นผ๋Ÿผ;

-- [์˜ˆ์‹œ]

SELECT WORKER.EMPNO AS ์‚ฌ์›๋ฒˆํ˜ธ, WORKER.ENAME AS ์‚ฌ์›๋ช…, MANAGER.ENAME AS ๊ด€๋ฆฌ์ž๋ช…
	FROM EMP WORKER, EMP MANAGER
WHERE MANAGER.EMPNO = WORKER.MGR;

selfjoin

3. ๊ณ„์ธตํ˜• ์งˆ์˜

๊ณ„์ธตํ˜• ๋ฐ์ดํ„ฐ ์กฐํšŒ๋Š” DBMS ๋ฒค๋”์™€ ๋ฒ„์ „์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ์ง€์›ํ•œ๋‹ค.

 

Oracle ๊ณ„์ธตํ˜• ์งˆ์˜ ๊ตฌ๋ฌธ

SELECT	...
      FROM ํ…Œ์ด๋ธ”
     WHERE condition
       AND condition ...
START WITH condition ...
       AND condition ...
CONNECT BY [NOCYCLE] conditon
       AND condition ...
[ORDER SIBLINGS BY column, column, ...]
์ข…๋ฅ˜ ์„ค๋ช…
START WITH  ๊ณ„์ธต ๊ตฌ์กฐ ์ „๊ฐœ์˜ ์‹œ์ž‘ ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ตฌ๋ฌธ, ์ฆ‰ ๋ฃจํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •ํ•œ๋‹ค(์•ก์„ธ์Šค)
CONNECT BY ๋‹ค์Œ์— ์ „๊ฐœ๋  ์ž์‹ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ตฌ๋ฌธ, ์ž์‹๋ฐ์ดํ„ฐ๋Š” CONNECT BY ์ ˆ์— ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•ด์•ผ ํ•œ๋‹ค.(์กฐ์ธ)
PRIOR CONNECT BY์ ˆ์— ์‚ฌ์šฉ๋˜๋ฉฐ, ํ˜„์žฌ ์ฝ์€ ์นผ๋Ÿผ์„ ์ง€์ •ํ•œ๋‹ค. 
(FK) = PRIOR (PK) ํ˜•ํƒœ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด (๋ถ€๋ชจ -> ์ž์‹) ์ˆœ๋ฐฉํ–ฅ ์ „๊ฐœ๋ฅผ ํ•œ๋‹ค.
(PK) = PRIOR (FK) ํ˜•ํƒœ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด (์ž์‹ -> ๋ถ€๋ชจ) ์—ญ๋ฐฉํ–ฅ ์ „๊ฐœ๋ฅผ ํ•œ๋‹ค.
NOCYCLE ๋ฐ์ดํ„ฐ๋ฅผ ์ „๊ฐœํ•˜๋ฉด์„œ ์ด๋ฏธ ๋‚˜ํƒ€๋‚ฌ๋˜ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ „๊ฐœ ์ค‘์— ๋‹ค์‹œ ๋‚˜ํƒ€๋‚œ๋‹ค๋ฉด, ์ด๊ฒƒ์„ ๊ฐ€๋ฆฌ์ผœ ์‚ฌ์ดํด(Cycle)์ด ๋ฐœ์ƒํ–ˆ๋‹ค๊ณ  ํ•œ๋‹ค. ์‚ฌ์ดํด์ด ๋ฐœ์ƒํ•œ ๋ฐ์ดํ„ฐ๋Š” ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. NOCYCLE์„ ์ถ”๊ฐ€ํ•˜๋ฉด์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๊ณ  ์‚ฌ์ดํด์ด ๋ฐœ์ƒํ•œ ์ดํ›„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๊ฐœํ•˜์ง€ ์•Š๋Š”๋‹ค.
ORDER SIBLINGS BY ํ˜•์ œ ๋…ธ๋“œ(๋™์ผ LEVEL) ์‚ฌ์ด์—์„œ ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
WHERE ๋ชจ๋“  ์ „๊ฐœ๋ฅผ ์ˆ˜ํ–‰ํ•œ ํ›„์— ์ง€์ •๋œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ ์ถ”์ถœํ•œ๋‹ค(ํ•„ํ„ฐ๋ง)

Oracle์€ ๊ณ„์ธตํ˜• ์งˆ์˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ€์ƒ ์นผ๋Ÿผ(Pseudo Column)์„ ์ œ๊ณตํ•œ๋‹ค.

๊ฐ€์ƒ ์นผ๋Ÿผ ์„ค๋ช…
LEVEL ๋ฃจํŠธ ๋ฐ์ดํ„ฐ์ด๋ฉด 1, ๊ทธ ํ•˜์œ„ ๋ฐ์ดํ„ฐ์ด๋ฉด 2๋‹ค. ๋ฆฌํ”„(Leaf) ๋ฐ์ดํ„ฐ๊นŒ์ง€ 1์”ฉ ์ฆ๊ฐ€ํ•œ๋‹ค.
CONNECT_BY_ISLEAF ์ „๊ฐœ ๊ณผ์ •์—์„œ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฆฌํ”„ ๋ฐ์ดํ„ฐ์ด๋ฉด 1, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด 0์ด๋‹ค.
CONNECT_BY_ISCYCLE ์ „๊ฐœ ๊ณผ์ •์—์„œ ์ž์‹์„ ๊ฐ–๋Š”๋ฐ, ํ•ด๋‹น ๋ฐ์ดํ„ฐ๊ฐ€ ์กฐ์ƒ์œผ๋กœ์„œ ์กด์žฌํ•˜๋ฉด 1, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด 0์ด๋‹ค. ์—ฌ๊ธฐ์„œ ์กฐ์ƒ์ด๋ž€ ์ž์‹ ์œผ๋กœ๋ถ€ํ„ฐ ๋ฃจํŠธ๊นŒ์ง€์˜ ๊ฒฝ๋กœ์— ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋งํ•œ๋‹ค.
CYCLE ์˜ต์…˜์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹ค์Œ์€ emp ํ…Œ์ด๋ธ”์„ ๊ณ„์ธตํ˜• ์งˆ์˜ ๊ตฌ๋ฌธ์„ ์ด์šฉํ•ด์„œ ์กฐํšŒํ•œ ๊ฒƒ์ด๋‹ค. PK์ธ empno ์•ž์ชฝ์— PRIOR ์—ฐ์‚ฐ์ž๋ฅผ ๊ธฐ์ˆ ํ–ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ๋ฅผ ๋“ค์—ฌ์“ฐ๊ธฐํ•˜๊ธฐ ์œ„ํ•ด LPAD ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

-- [์˜ˆ์ œ]

SELECT LEVEL
      ,LPAD ('', 4 * (LEVEL - 1)) || ์‚ฌ์› AS ์‚ฌ์›, ์ง์†์ƒ์‚ฌ
      ,CONNECT_BY_ISLEAF AS ISLEAF
      FROM ์‚ฌ์›
START WITH ์ง์†์ƒ์‚ฌ IS NULL
CONNECT BY ์ง์†์ƒ์‚ฌ = PRIOR ์‚ฌ์›;

CONNECT BY ์ ˆ์€ ๊ฐ ํ–‰์ด ์–ด๋–ป๊ฒŒ ์—ฐ๊ฒฐ๋˜๋Š”์ง€๋ฅผ Oracle์—๊ฒŒ ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ํ˜„์žฌ ํ–‰๊ณผ ๋‹ค๋ฅธ ํ–‰์€ PRIOR ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ๊ตฌ๋ณ„๋œ๋‹ค.CONNECT BY PRIOR ์‚ฌ์› = ์ง์†์ƒ์‚ฌ; ์ด๋œป์€ "๋ฐฉ๊ธˆ ์ „ ํ–‰์˜ ์‚ฌ์› ๊ฐ’์ด ํ˜„์žฌ ํ–‰์˜ ์ง์†์ƒ์‚ฌ ๊ฐ’์ธ ํ–‰์„ ๋ชจ๋‘ ์ฐพ์•„๋ผ" ๋ผ๋Š” ๋œป์ด๋‹ค.์‰ฝ๊ฒŒ ํ’€์ดํ•˜์ž๋ฉด, ๋ฐฉ๊ธˆ์ „์— ์‚ดํŽด๋ณธ ์‚ฌ์›์ด ํ˜„์žฌ ์‚ฌ์›์˜ ์ƒ์‚ฌ๊ฐ€ ๋˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ถœ๋ ฅํ•˜๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ** CONNECT BY ์‚ฌ์› = PRIOR ์ง์†์ƒ์‚ฌ; (== CONNECT BY PRIOR ์ง์†์ƒ์‚ฌ = ์‚ฌ์›;) ์™€ ๊ฐ™์ด PRIOR ํ‚ค์›Œ๋“œ๋ฅผ "=" ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์ด์— ๋‘๊ณ  ๋ฐ˜๋Œ€ํŽธ์œผ๋กœ ์˜ฎ๊ธธ ๊ฒฝ์šฐ, ๊ฒฐ๊ณผ๋Š” Tree๋ฅผ ๊ฑฐ์Šฌ๋Ÿฌ ๋‚ด๋ ค๊ฐ€๋Š” ๊ฒƒ(์ˆœ๋ฐฉํ–ฅ ์ „๊ฐœ)์ด ์•„๋‹ˆ๋ผ, ๊ฑฐ์Šฌ๋Ÿฌ ์˜ฌ๋ผ๊ฐ€๋Š” ๋ฐฉ์‹(์—ญ๋ฐฉํ–ฅ ์ „๊ฐœ)์œผ๋กœ ๋ฆฌํ„ด๋œ๋‹ค.

 

์ˆœ๋ฐฉํ–ฅ

์œ„ ๊ทธ๋ฆผ์€ LEVEL ์นผ๋Ÿผ์ด ์‚ฌ์šฉ๋œ ์ˆœ๋ฐฉํ–ฅ ๊ณ„์ธตํ˜• ์งˆ์˜์— ๋Œ€ํ•œ ๋…ผ๋ฆฌ์ ์ธ ์‹คํ–‰ ๋ชจ์Šต์ด๋‹ค. ๊ทธ๋ฆผ์„ ๋ณด๋ฉด ์ˆœ๋ฐฉํ–ฅ ์ „๊ฐœ ์‹œ, A๋Š” ๋ฃจํŠธ ๋ฐ์ดํ„ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ ˆ๋ฒจ์ด 1์ด๋‹ค. A์˜ ํ•˜์œ„ ๋ฐ์ดํ„ฐ์ธ B, C๋Š” ๋ ˆ๋ฒจ 2 ๐Ÿ‘‰๐Ÿป C์˜ ํ•˜์œ„ ๋ฐ์ดํ„ฐ์ธ D, E๋Š” ๋ ˆ๋ฒจ 3 ๐Ÿ‘‰๐Ÿป ๋ฆฌํ”„ ๋ฐ์ดํ„ฐ๋Š” B, D, E ๊ฐ€ ๋œ๋‹ค.

์—ญ๋ฐฉํ–ฅ ์ „๊ฐœ

์—ญ๋ฐฉํ–ฅ ๊ณ„์ธตํ˜• ์ „๊ฐœ ์‹œ์—๋Š” D๊ฐ€ ๋ฃจํŠธ ๋ฐ์ดํ„ฐ๋กœ ๋ ˆ๋ฒจ 1์ด ๋˜๋ฉฐ, ์ƒ์œ„ ๋ ˆ๋ฒจ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์—ญ์ˆœ์˜ ๋ ˆ๋ฒจ์„ ๊ฐ–๋Š”๋‹ค.

๊ณ„์ธตํ˜• ์งˆ์˜์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜ ์„ค๋ช…
SYS_CONNECT_BY_PATH ๋ฃจํŠธ ๋ฐ์ดํ„ฐ๋ถ€ํ„ฐ ํ˜„์žฌ ์ „๊ฐœํ•  ๋ฐ์ดํ„ฐ๊นŒ์ง€์˜ ๊ฒฝ๋กœ๋ฅผ ํ‘œ์‹œํ•œ๋‹ค.
์‚ฌ์šฉ๋ฒ• : SYS_CONNECT_BY_PATH(์นผ๋Ÿผ, ๊ฒฝ๋กœ๋ถ„๋ฆฌ์ž)
CONNECT_BY_ROOT ํ˜„์žฌ ์ „๊ฐœํ•  ๋ฐ์ดํ„ฐ์˜ ๋ฃจํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ์‹œํ•œ๋‹ค. ๋‹จํ•ญ ์—ฐ์‚ฐ์ž์ด๋‹ค.
์‚ฌ์šฉ๋ฒ• : CONNECT_BY_ROOT ์นผ๋Ÿผ

 

โœจ์ œ7์ ˆ PIVOT ์ ˆ๊ณผ UNPIVOT ์ ˆ


1. ๊ฐœ์š”

PIVOT์€ ํšŒ์ „์‹œํ‚จ๋‹ค๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค. PIVOT ์ ˆ์€ ํ–‰์„ ์—ด๋กœ ํšŒ์ „์‹œํ‚ค๊ณ , UNPIVOT ์ ˆ์€ ์—ด์„ ํ–‰์œผ๋กœ ํšŒ์ „์‹œํ‚จ๋‹ค.

 

2. PIVOT ์ ˆ

PIVOT ์ ˆ์€ ํ–‰์„ ์—ด๋กœ ์ „ํ™˜ ํ•œ๋‹ค. PIVOT ์ ˆ์˜ ๊ตฌ๋ฌธ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

PIVOT [XML]
        (aggregate_function (expr) [[AS] alias]
       [,aggregate_function (expr) [[AS] alias]]...
         FOR {column | (column [, column]...)}
         IN ({{{expr | (expr [, expr]...)} [[AS] alias]}...
             | subquery
             | ANY [, ANY]...
             })
         )
PIVOT ์ ˆ ( ํ–‰ -> ์—ด )
aggregate_function ์ง‘๊ณ„ํ•  ์—ด์„ ์ง€์ •
FOR PIVOT ํ•  ์—ด์„ ์ง€์ •ํ•œ๋‹ค.
IN PIVOT ํ•  ์—ด ๊ฐ’์„ ์ง€์ •ํ•œ๋‹ค.

PIVOT ์ ˆ์€ ์ง‘๊ณ„ํ•จ์ˆ˜์™€ FOR์ ˆ์— ์ง€์ •๋˜์ง€ ์•Š์€ ์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ง‘๊ณ„๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ธ๋ผ์ธ ๋ทฐ๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉํ•  ์—ด์„ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.

[์˜ˆ์ œ 1]

-- [์˜ˆ์ œ 1] PIVOT ์ ˆ์„ ์‚ฌ์šฉํ•œ ์ฟผ๋ฆฌ

SELECT *
    FROM (SELECT JOB, DEPTNO, SAL FROM EMP)
  PIVOT (SUM (SAL) FOR DEPTNO IN (10, 20, 30)
ORDER BY 1;

-- [์ถœ๋ ฅ ํ™”๋ฉด]

JOB        10     20     30
----       ---    ---    ---
ANALYST          6000		
CLERK      1300  1900     950
MANAGER    2450  2975    2850
PRESIDENT  5000	
SALESMAN                 5600

5 ํ–‰์ด ์„ ํƒ๋์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ ์ฟผ๋ฆฌ๋Š” ์ธ๋ผ์ธ ๋ทฐ์— yyyyํ‘œํ˜„์‹์„ ์ถ”๊ฐ€ํ•œ ๊ฒƒ์ด๋‹ค. ํ–‰ ๊ทธ๋ฃน์— yyyy ํ‘œํ˜„์‹์ด ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

[์˜ˆ์ œ 2]

-- [์˜ˆ์ œ 2]

SELECT *
   FROM (SELECT TO_CHAR (HIREDATE, 'YYYY') AS YYYY, JOB, DEPTNO, SAL FROM EMP)
  PIVOT (SUM (SAL) FOR DEPTNO IN (10, 20, 30))
ORDER BY 1, 2;

-- [์ถœ๋ ฅ ํ™”๋ฉด]

YYYY    JOB       10     20      30
----    ----      ---    ---    ---
1980    CLERK            800
1981    ANALYST         3000
1981    CLERK                    950
1981    MANAGER   2450  2975    2850
...

9ํ–‰์ด ์„ ํƒ๋์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ ์ฟผ๋ฆฌ๋Š” ์ง‘๊ณ„ํ•จ์ˆ˜์™€ IN ์ ˆ์— ๋ณ„์นญ์„ ์ง€์ •ํ–ˆ๋‹ค. ๋ณ„์นญ์„ ์ง€์ •ํ•˜๋ฉด ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์˜ ์—ด ๋ช…์ด ๋ณ€๊ฒฝ๋œ๋‹ค.

[์˜ˆ์ œ 3]

-- [์˜ˆ์ œ 3]

SELECT *
   FROM (SELECT JOB, DEPTNO, SAL FROM EMP)
  PIVOT (SUM (SAL) AS SAL FOR DEPTNO IN (10 AS D10, 20 AS D20, 30 AS D30))
ORDER BY 1;

-- [์ถœ๋ ฅ ํ™”๋ฉด]

JOB        D10_SAL     D20_SAL     D30_SAL
----       -------     -------     -------
ANALYST                 6000		
CLERK       1300        1900           950
MANAGER     2450        2975          2850
PRESIDENT   5000	
SALESMAN                              5600

5 ํ–‰์ด ์„ ํƒ๋์Šต๋‹ˆ๋‹ค.

SELECT ์ ˆ์— ๋ถ€์—ฌ๋œ ์—ด ๋ช…์„ ์ง€์ •ํ•˜๋ฉด ํ•„์š”ํ•œ ์—ด๋งŒ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

[์˜ˆ์ œ 4]

-- [์˜ˆ์ œ 4]

SELECT JOB, D20_SAL
    FROM(SELECT JOB, DEPTNO, SAL FROM EMP)
   PIVOT(SUM (SAL) AS SAL FOR DEPTNO IN (10 AS D10, 20 AS D20, 30 AS D30))
   WHERE D20_SAL > 2500
ORDER BY 1;

-- [์ถœ๋ ฅ ํ™”๋ฉด]

JOB         D20_SAL
----        ------
ANALYST      6000
MANAGER      2975

2 ํ–‰์ด ์„ ํƒ๋์Šต๋‹ˆ๋‹ค.

 

3. UNPIVOT ์ ˆ

UNPIVOT ์ ˆ์€  PIVOT ์ ˆ๊ณผ ๋ฐ˜๋Œ€๋กœ ๋™์ž‘ํ•œ๋‹ค. ์—ด์ด ํ–‰์œผ๋กœ ์ „ํ™˜๋œ๋‹ค.  UNPIVOT ์ ˆ์˜ ๊ตฌ๋ฌธ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

UNPIVOT [{INCLUDE | EXCLUDE} NULLS]
        (   {column | (column [, col]...)}
        FOR {column | (column [, col]...)}
        IN  {column | (column [, col]...)} [AS {literal | (literal [, literal]...)}]
         [, {column | (column [, col]...)} [AS {literal | (literal [, literal]...)}]]...
           )
        )
        
-- ์ž‘์„ฑ๋ฒ• ์˜ˆ์‹œ 

SELECT *
    FROM ( ํ”ผ๋ฒ— ๋Œ€์ƒ ์ฟผ๋ฆฌ๋ฌธ )
UNPIVOT ( ์ปฌ๋Ÿผ๋ณ„์นญ(๊ฐ’) FOR ์ปฌ๋Ÿผ๋ณ„์นญ(์—ด) IN (ํ”ผ๋ฒ—์—ด๋ช… AS '๋ณ„์นญ', ... )
UNPIVOT ์ ˆ ( ์—ด -> ํ–‰ )
UNPIVOT column UNPIVOT๋œ ๊ฐ’์ด ๋“ค์–ด๊ฐˆ ์—ด์„ ์ง€์ •ํ•œ๋‹ค.
FOR UNPIVOT๋œ ๊ฐ’์„ ์„ค๋ช…ํ•  ๊ฐ’์ด ๋“ค์–ด๊ฐˆ ์—ด์„ ์ง€์ •ํ•œ๋‹ค.
IN UNPIVOTํ•  ์—ด๊ณผ ์„ค๋ช…ํ•  ๊ฐ’์˜ ๋ฆฌํ„ฐ๋Ÿด ๊ฐ’์„ ์ง€์ •ํ•œ๋‹ค.

์ถœ์ฒ˜ : https://gent.tistory.com/382

 

โœจ์ œ8์ ˆ ์ •๊ทœ ํ‘œํ˜„์‹


1. ๊ฐœ์š”

์ •๊ทœ ํ‘œํ˜„์‹(regular expression)์€ ๋ฌธ์ž์—ด์˜ ๊ทœ์น™์„ ํ‘œํ˜„ํ•˜๋Š” ๊ฒ€์ƒ‰ ํŒจํ„ด์œผ๋กœ ์ฃผ๋กœ ๋ฌธ์ž์—ด ๊ฒ€์ƒ‰๊ณผ ์น˜ํ™˜์— ์‚ฌ์šฉ๋œ๋‹ค.

2. ๊ธฐ๋ณธ๋ฌธ๋ฒ•

์—ฐ์‚ฐ์ž ์„ค๋ช…
. (dot) ๋ชจ๋“  ๋ฌธ์ž์™€ ์ผ์น˜(newline ์ œ์™ธ)
| (or) ๋Œ€์ฒด ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„
\ (backslash) ๋‹ค์Œ ๋ฌธ์ž๋ฅผ ์ผ๋ฐ˜ ๋ฌธ์ž๋กœ ์ทจ๊ธ‰
^ (carrot) ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘
$ (dollar) ๋ฌธ์ž์—ด์˜ ๋
์ˆ˜๋Ÿ‰์‚ฌ(quantifier) ํŒจํ„ด์„ ์ตœ๋Œ€๋กœ ์ผ์น˜์‹œํ‚ค๋Š” ํƒ์š•์ (greedy) ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘

PERL ์ •๊ทœ ํ‘œํ˜„์‹ ์—ฐ์‚ฐ์ž๋Š” ์ˆ˜๋Ÿ‰์‚ฌ์™€ ์œ ์‚ฌํ•˜๊ฒŒ ๋™์ž‘, ํŒจํ„ด์„ ์ตœ์†Œ๋กœ ์ผ์น˜์‹œํ‚ค๋Š” ๋น„ํƒ์š•์  (nongreedy) ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘
? / ?? 0ํšŒ ๋˜๋Š” 1ํšŒ ์ผ์น˜
* / *? 0ํšŒ ๋˜๋Š” ๊ทธ ์ด์ƒ์˜ ํšŸ์ˆ˜๋กœ ์ผ์น˜
+ / +? 1ํšŒ ๋˜๋Š” ๊ทธ ์ด์ƒ์˜ ํšŸ์ˆ˜๋กœ ์ผ์น˜
{M} / {M}? mํšŒ ์ผ์น˜
{m,} / {m,}? ์ตœ์†Œ mํšŒ ์ผ์น˜
{,m} / {,m}? ์ตœ๋Œ€ mํšŒ ์ผ์น˜
{m,n} / {m,n}? ์ตœ์†Œ mํšŒ, ์ตœ๋Œ€ nํšŒ ์ผ์น˜
(expr) ๊ด„ํ˜ธ ์•ˆ์˜ ํ‘œํ˜„์‹์„ ํ•˜๋‚˜์˜ ๋‹จ์œ„๋กœ ์ทจ๊ธ‰  
\n n๋ฒˆ์งธ ์„œ๋ธŒ ํ‘œํ˜„์‹๊ณผ ์ผ์น˜, n์€ 1์—์„œ 9 ์‚ฌ์ด์˜ ์ •์ˆ˜
[a-z] a๋ถ€ํ„ฐ z์ค‘ ํ•œ ๋ฌธ์ž์™€ ์ผ์น˜
[^a-z] a๋ถ€ํ„ฐ z์— ํฌํ•จ๋˜์ง€ ์•Š๋Š” ๋ฌธ์ž

๋ฌธ์ž ๋ฆฌ์ŠคํŠธ(character list)๋Š” ๋ฌธ์ž๋ฅผ ๋Œ€๊ด„ํ˜ธ๋กœ ๋ฌถ์€ ํ‘œํ˜„์‹์ด๋‹ค. 

๋ฌธ์ž ๋ฆฌ์ŠคํŠธ ์ค‘ ํ•œ ๋ฌธ์ž๋งŒ ์ผ์น˜ํ•˜๋ฉด ํŒจํ„ด์ด ์ผ์น˜ํ•œ ๊ฒƒ์œผ๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค. ๋ฌธ์ž ๋ฆฌ์ŠคํŠธ์—์„œ ํ•˜์ดํ”ˆ(-)์€ ๋ฒ”์œ„ ์—ฐ์‚ฐ์ž๋กœ ๋™์ž‘ํ•œ๋‹ค.

์—ฐ์‚ฐ์ž ์„ค๋ช… ๋™์ผ
[[:digit:]] ์ˆซ์ž [0-9], \d
[^[:digit:]] ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ชจ๋“  ๋ฌธ์ž \D
[[:lower:]] ์†Œ๋ฌธ์ž [a-z]
[[:upper:]] ๋Œ€๋ฌธ์ž [A-Z]
[[:alpha:]] ์˜๋ฌธ์ž [a-zA-Z]
[[:alnum:]] ์ˆซ์ž์™€ ์˜๋ฌธ์ž [0-9a-zA-Z]
[[:alnum:]_] ์ˆซ์ž์™€ ์˜๋ฌธ์ž(underbar ํฌํ•จ) \w
[^[:alnum:]_] ์ˆซ์ž์™€ ์˜๋ฌธ์ž๊ฐ€ ์•„๋‹Œ ๋ชจ๋“  ๋ฌธ์ž(underbar ์ œ์™ธ) \W
[[:xdigit:]] 16์ง„์ˆ˜ [0-9a-fA-F]
[[:punct:]] ๊ตฌ๋‘์  ๊ธฐํ˜ธ [^[:alnum:][:cntrl:]]
[[:blank:]] ๊ณต๋ฐฑ ๋ฌธ์ž  
[[:space:]] ๊ณต๊ฐ„ ๋ฌธ์ž (space, enter, tab) \s
[^[:space:]] ๊ณต๊ฐ„ ๋ฌธ์ž๊ฐ€ ์•„๋‹Œ ๋ชจ๋“  ๋ฌธ์ž \S

 

3. ์ •๊ทœ ํ‘œํ˜„์‹ ์กฐ๊ฑด๊ณผ ํ•จ์ˆ˜

์ข…๋ฅ˜ ์„ค๋ช…
REGEXP_LIKE ์กฐ๊ฑด
source_char ๊ฐ€ pattern๊ณผ ์ผ์น˜ํ•˜๋ฉด TRUE, ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด FALSE๋ฅผ ๋ฐ˜ํ™˜ [ ์ž‘์„ฑ๋ฒ• ]
REGEXP_LIKE (source_char, pattern [, match_param])
source_char ๊ฒ€์ƒ‰ ๋ฌธ์ž์—ด์„ ์ง€์ •
pattern ๊ฒ€์ƒ‰ ํŒจํ„ด์„ ์ง€์ •
match_param ์ผ์น˜ ์˜ต์…˜์„ ์ง€์ • (i: ๋Œ€์†Œ๋ฌธ์ž ๋ฌด์‹œ, c: ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„, n: dot(.)๋ฅผ ๊ฐœํ–‰ ๋ฌธ์ž์™€ ์ผ์น˜, m: ๋‹ค์ค‘ ํ–‰ ๋ชจ๋“œ, x: ๊ฒ€์ƒ‰ ํŒจํ„ด์˜ ๊ณต๋ฐฑ ๋ฌธ์ž๋ฅผ ๋ฌด์‹œ, ๊ธฐ๋ณธ๊ฐ’์€ c๋‹ค. icnmx ํ˜•์‹์œผ๋กœ ๋‹ค์ˆ˜์˜ ์˜ต์…˜์„ ํ•จ๊ป˜ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.)
REGEXP_REPLACE
source_char ์—์„œ ์ผ์น˜ํ•œ pattern์„ replace_string์œผ๋กœ ๋ณ€๊ฒฝํ•œ ๋ฌธ์ž ๊ฐ’์„ ๋ฐ˜ํ™˜ [ ์ž‘์„ฑ๋ฒ• ]
REGEXP_REPLACE (source_char, pattern [, replace_string [, position [, occurrence [, match_param]]]])
replace_string ๋ณ€๊ฒฝ ๋ฌธ์ž์—ด์„ ์ง€์ •
positoin ๊ฒ€์ƒ‰ ์‹œ์ž‘ ์œ„์น˜๋ฅผ ์ง€์ •
occurrence ํŒจํ„ด ์ผ์น˜ ํšŸ์ˆ˜๋ฅผ ์ง€์ •
REGEXP_SUBSTR
source_char์—์„œ ์ผ์น˜ํ•œ pattern์„ ๋ฐ˜ํ™˜ [ ์ž‘์„ฑ๋ฒ• ]
REGEXP_SUBSTR (source_char, pattern [, position [, occurrence [, match_param [, subexpr]]]])
subexpr ์„œ๋ธŒ ํ‘œํ˜„์‹์„ ์ง€์ •ํ•œ๋‹ค (0์€ ์ „์ฒด ํŒจํ„ด, 1์ด์ƒ์€ ์„œ๋ธŒ ํ‘œํ˜„์‹, ๊ธฐ๋ณธ๊ฐ’์€ 0)
REGEXP_INSTR
source_char์—์„œ ์ผ์น˜ํ•œ pattern์˜ ์‹œ์ž‘ ์œ„์น˜๋ฅผ ์ •์ˆ˜๋กœ ๋ฐ˜ํ™˜ [ ์ž‘์„ฑ๋ฒ• ]
REGEXP_INSTR (source_char, pattern [, position [, occurrence [, return_opt [, match_param [, subexpr]]]]])
return_opt ๋ฐ˜ํ™˜ ์˜ต์…˜์„ ์ง€์ •ํ•œ๋‹ค (0์€ ์‹œ์ž‘ ์œ„์น˜, 1์€ ๋‹ค์Œ ์œ„์น˜, ๊ธฐ๋ณธ๊ฐ’์€ 0)
REGEXP_COUNT
source_char์—์„œ ์ผ์น˜ํ•œ pattern์˜ ํšŸ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ [ ์ž‘์„ฑ๋ฒ• ]
REGEXO_COUNT (source_char, pattern [, position [, match_param]])

 

โœจ์žฅ ์š”์•ฝ


์ œ1์ ˆ ์„œ๋ธŒ์ฟผ๋ฆฌ
์„œ๋ธŒ ์ฟผ๋ฆฌ๋ž€ ํ•˜๋‚˜์˜ ๋ฉ”์ธ ์ฟผ๋ฆฌ ์•ˆ์— ํฌํ•จ๋œ ์ข…์†์ ์ธ SQL ๋ฌธ์žฅ์„ ๋งํ•˜๋Š” ๊ฒƒ์œผ๋กœ, ์œ„์น˜๋‚˜ ๊ธฐ๋Šฅ์— ๋”ฐ๋ผ NESTED SUBQUERY, INLINE VIEW, SCALAR SUBQUERY๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค.
์ œ2์ ˆ ์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž
์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž๋Š” ๊ฒฐ๊ณผ ์ง‘ํ•ฉ ๊ฐ„์˜ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ๊ฒฐํ•ฉํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค. UNION ์—ฐ์‚ฐ์ž์™€ UNION ALL ์—ฐ์‚ฐ์ž๋Š” ๊ฐœ๋ณ„ SQL ๋ฌธ์˜ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด ํ•ฉ์ง‘ํ•ฉ ์—ฐ์‚ฐ์„, INTERSECT ์—ฐ์‚ฐ์ž๋Š” ๊ต์ง‘ํ•ฉ ์—ฐ์‚ฐ์„, EXCEPT/MINUS ์—ฐ์‚ฐ์ž๋Š” ์ฐจ์ง‘ํ•ฉ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. UNION ALL ์—ฐ์‚ฐ์ž๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ์ง‘ํ•ฉ ์—ฐ์‚ฐ์ž๋Š” ์ตœ์ข… ๊ฒฐ๊ณผ์—์„œ ์ค‘๋ณต๋œ ํ–‰์€ ์ œ์™ธํ•˜๊ณ  ์ถœ๋ ฅํ•œ๋‹ค.
์ œ3์ ˆ ๊ทธ๋ฃน ํ•จ์ˆ˜
๋ฐ์ดํ„ฐ ๋ถ„์„์„ ์œ„ํ•œ GROUP FUNCTION์œผ๋กœ๋Š” ์†Œ๊ทธ๋ฃน ๊ฐ„์˜ ์†Œ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ROLLUP ํ•จ์ˆ˜, GROUP BY ํ•ญ๋ชฉ๋“ค๊ฐ„์˜ ๋‹ค์ฐจ์›์ ์ธ ์†Œ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋Š” CUBE ํ•จ์ˆ˜, ํŠน์ • ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ์†Œ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” GROUPING SETS ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค.
์ œ4์ ˆ ์œˆ๋„์šฐ ํ•จ์ˆ˜
๋ฐ์ดํ„ฐ ๋ถ„์„์„ ์œ„ํ•œ WINDOW FUNCTION์€ ๋ถ€๋ถ„์ ์ด๋‚˜๋งˆ ํ–‰๊ณผ ํ–‰๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์‰ฝ๊ฒŒ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“  ํ•จ์ˆ˜๋‹ค. WINDOW FUNCTION์„ ์ด์šฉํ•œ ์ˆœ์œ„(RANK) ๊ด€๋ จ ํ•จ์ˆ˜๋Š” RANK, DENSE_RANK, ROW_NUMBER๊ฐ€ ์žˆ์œผ๋ฉฐ, ๊ทธ์™ธ ๊ทธ๋ฃน ๋‚ด ์ง‘๊ณ„(AGGREGATE) ๊ด€๋ จ ํ•จ์ˆ˜, ๊ทธ๋ฃน ๋‚ด ๋น„์œจ ๊ด€๋ จ ํ•จ์ˆ˜ ๋“ฑ์ด ์žˆ๋‹ค.
์ œ5์ ˆ Top N ์ฟผ๋ฆฌ
Top-N ์ฟผ๋ฆฌ๋Š” ๋ง ๊ทธ๋Œ€๋กœ ์ƒ์œ„(Top) N๊ฐœ์˜ ํ–‰์„ ์กฐํšŒํ•˜๋Š” ์ฟผ๋ฆฌ๋‹ค. Oracle์€ ROWNUM ์Šˆ๋„ ์นผ๋Ÿผ, SQL Server๋Š” TOP ์ ˆ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. Oracle์€ 12.1๋ฒ„์ „, SQL Server๋Š” 2012 ๋ฒ„์ „๋ถ€ํ„ฐ ROW LIMITING ์ ˆ๋กœ Top N ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
์ œ6์ ˆ ๊ณ„์ธตํ˜• ์งˆ์˜์™€ ์…€ํ”„ ์กฐ์ธ
ํ…Œ์ด๋ธ”์— ๊ณ„์ธตํ˜• ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ณ„์ธตํ˜• ์งˆ์˜(HIERARCHICAL QUERY)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์…€ํ”„ ์กฐ์ธ(SELF JOIN)์ด๋ž€ ๋™์ผ ํ…Œ์ด๋ธ” ์‚ฌ์ด์˜ ์กฐ์ธ์„ ๋งํ•˜๋ฉฐ, FROM ์ ˆ์— ๋™์ผ ํ…Œ์ด๋ธ”์ด ๋‘ ๋ฒˆ ์ด์ƒ ๋‚˜ํƒ€๋‚œ๋‹ค.
์ œ7์ ˆ PIVOT ์ ˆ๊ณผ UNPIVOT ์ ˆ
PIVOT์€ ํšŒ์ „์‹œํ‚จ๋‹ค๋Š” ์˜๋ฏธ๋‹ค. PIVOT ์ ˆ์€ ํ–‰์„ ์—ด๋กœ ํšŒ์ „์‹œํ‚ค๊ณ , UNPIVOT ์ ˆ์€ ์—ด์„ ํ–‰์œผ๋กœ ํšŒ์ „์‹œํ‚จ๋‹ค. PIVOT์ ˆ์€ CASE ํ‘œํ˜„์‹๊ณผ GROUP BY ์ ˆ, UNPIVOT ์ ˆ์€ ํ–‰ ๋ณต์ œ๋ฅผ ์‚ฌ์šฉํ•ด ์ˆ˜ํ–‰ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
์ œ8์ ˆ ์ •๊ทœํ‘œํ˜„์‹
์ •๊ทœ ํ‘œํ˜„์‹(regular expression)์€ ๋ฌธ์ž์—ด์˜ ๊ทœ์น™์„ ํ‘œํ˜„ํ•˜๋Š” ๊ฒ€์ƒ‰ ํŒจํ„ด์œผ๋กœ ์ฃผ๋กœ ๋ฌธ์ž์—ด ๊ฒ€์ƒ‰๊ณผ ์น˜ํ™˜์— ์‚ฌ์šฉ๋œ๋‹ค. REGEXP_LIKE ์กฐ๊ฑด๊ณผ REGEXP_REPLACE ํ•จ์ˆ˜, REGEXP_SUBSTR ํ•จ์ˆ˜, REGEXP_INSTR ํ•จ์ˆ˜, REGEXP_COUNT ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ถœ์ฒ˜ : SQL ์ „๋ฌธ๊ฐ€ ๊ฐ€์ด๋“œ

300x250

์ฝ”๋“œ