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

4. ๊ทธ๋ฃนํ•จ์ˆ˜ Cube, Roll up, Grouping set, Window function, CTE, Partition by, SUBQUERY ์™€ JOIN ์˜ ์ฐจ์ด

by Hagrid 2022. 11. 14.
๋ฐ˜์‘ํ˜•

๐Ÿ“Œ ๊ทธ๋ฃนํ•จ์ˆ˜์˜ ํ•„์š”์„ฑ

๊ทธ๋ฃน ํ•จ์ˆ˜(GROUP FUNCTION)์˜ ํ•„์š”์„ฑ

๊ฒฐ์‚ฐ ๊ฐœ๋…์˜ ์—…๋ฌด๋ฅผ ๊ฐ€์ง€๋Š” ์›๊ฐ€๋‚˜ ํŒ๋งค ์‹œ์Šคํ…œ์˜ ๊ฒฝ์šฐ๋Š” ์†Œ๊ณ„, ์ค‘๊ณ„, ํ•ฉ๊ณ„, ์ด ํ•ฉ๊ณ„ ๋“ฑ ์—ฌ๋Ÿฌ ๋ ˆ๋ฒจ์˜ ๊ฒฐ์‚ฐ ๋ณด๊ณ ์„œ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ค‘์š” ์—…๋ฌด ์ค‘์˜ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ์ž๋“ค์ด ์ด๋Ÿฐ ๋ณด๊ณ ์„œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ ˆ๋ฒจ๋ณ„ ์ง‘๊ณ„๋ฅผ ์œ„ํ•œ ์—ฌ๋Ÿฌ ๋‹จ๊ณ„์˜ SQL์„ UNION, UNION ALL๋กœ ๋ฌถ์€ ํ›„ ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ฝ์–ด ๋‹ค์‹œ ์žฌ์ •๋ ฌํ•˜๋Š” ๋ณต์žกํ•œ ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์ณ์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ, ๊ทธ๋ฃน ํ•จ์ˆ˜(GROUP FUNCTION)์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, ํ•˜๋‚˜์˜ SQL๋กœ ํ…Œ์ด๋ธ”์„ ํ•œ ๋ฒˆ๋งŒ ์ฝ์–ด์„œ ๋น ๋ฅด๊ฒŒ ์›ํ•˜๋Š” ๋ฆฌํฌํŠธ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, GROUPING ํ•จ์ˆ˜์™€ CASE ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ์›ํ•˜๋Š” ํฌ๋งท์˜ ๋ณด๊ณ ์„œ ์ž‘์„ฑ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ ์š”์•ฝ

  • ROLLUP ํ•จ์ˆ˜ : ์†Œ๊ทธ๋ฃน ๊ฐ„์˜ ์†Œ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•จ
  • CUBE ํ•จ์ˆ˜ : GROUP BY ํ•ญ๋ชฉ๋“ค๊ฐ„ ๋‹ค์ฐจ์›์ ์ธ ์†Œ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Œ (๋ชจ๋“  ๊ทธ๋ฃน์ผ€์ด์Šค๋ฅผ ๊ณ„์‚ฐํ•จ)
  • GROUPING SETS ํ•จ์ˆ˜ : ํŠน์ • ํ•ญ๋ชฉ์— ๋Œ€ํ•œ ์†Œ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Œ (GROUPING SETS ์€ ํŠน์ •ํ•ญ๋ชฉ์„ ๊ทธ๋ฃนํ•œ ๋‹ค์ˆ˜์˜ SQL๋“ค์„ UNIONํ•œ ๊ฒƒ๊ณผ ๊ฐ™์Œ)

๐Ÿ“ŒGROUPING SETS์ ˆ

  • Grouping sets์€ ์™œ ํ•„์š”ํ•œ๊ฐ€?

    • ๋งŒ์•ฝ subject ๋ณ„ ํ‰๊ท ๊ณผ name ๋ณ„ ํ‰๊ท , All ํ‰๊ท  ์ •๋ณด๋ฅผ ํ•œ๋ฒˆ์— ๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด...

    • โ‡’ ํ•˜๋‚˜์”ฉ Group by ํ•œ ๊ฒฐ๊ณผ๋ฅผ Union All ํ•ด์„œ ์ถœ๋ ฅ ๊ฐ€๋Šฅ

      • ๊ทผ๋ฐ ๋„ˆ๋ฌด ์ฟผ๋ฆฌ๋ฌธ์ด ๊ธธ์–ด์ง„๋‹ค.
    • Union all ๋Œ€์‹ ์— ์“ธ ์ˆ˜ ์žˆ๋Š”๊ฒŒ ์žˆ๋‹ค๋ฉด !?

      ```sql

        1. ์ „์ฒด ํ‰๊ท  ๊ณ„์‚ฐ
          SELECT NULL,
          NULL,
          Avg(score)
          FROM scorelist
        1. ๊ณผ๋ชฉ, ์ด๋ฆ„๋ณ„ ํ‰๊ท  ๊ณ„์‚ฐ
          UNION ALL
          SELECT subject,
          name,
          Avg(score)
          FROM scorelist
          GROUP BY subject,
          name
        1. ๊ณผ๋ชฉ๋ณ„ ํ‰๊ท  ๊ณ„์‚ฐ
          UNION ALL
          SELECT subject,
          NULL,
          Avg(score)
          FROM scorelist
          GROUP BY subject
        1. ์ด๋ฆ„๋ณ„ ํ‰๊ท  ๊ณ„์‚ฐ
          UNION ALL
          SELECT NULL,
          name,
          Avg(score)
          FROM scorelist
          GROUP BY name;
    • ์ด๋ ‡๊ฒŒ ๊ธด ๊ตฌ๋ฌธ์„

    • ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณ€ํ™˜์‹œํ‚จ๋‹ค.

      SELECT subject,
           NAME,
           Avg(score)
      FROM   scorelist
      GROUP  BY grouping sets( ( subject, NAME ), -- 2. ๊ณผ๋ชฉ, ์ด๋ฆ„๋ณ„ ํ‰๊ท  ๊ณ„์‚ฐ
                                                                         subject, -- 3. ๊ณผ๋ชฉ๋ณ„ ํ‰๊ท  ๊ณ„์‚ฐ
                                                                                NAME, -- 4. ์ด๋ฆ„๋ณ„ ํ‰๊ท  ๊ณ„์‚ฐ
                                                                                ( ) ) -- 1. ์ „์ฒด ํ‰๊ท  ๊ณ„์‚ฐ
    • ์žฅ์ ์€ ์ˆ˜ํ–‰ ์†๋„๋„ ๋” ๋น ๋ฅด๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ณด์ด๋Š” ์ 

๐Ÿ“Œ Roll up

  • ROLL UP์ด ๋ญ”๊ฐ€?

    • ์ง€์ •๋œ GROUPING ์ปฌ๋Ÿผ์˜ ์†Œ๊ณ„๋ฅผ ์ƒ์„ฑํ•จ
    • ์†Œ๊ทธ๋ฃน ๊ฐ„์˜ ์†Œ๊ณ„๋ฅผ ๊ณ„์‚ฐ
  • ์ธ์ž๋กœ ๋“ค์–ด์˜จ ์ปฌ๋Ÿผ์„ ์˜ค๋ฅธ์ชฝ(โ†’)๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ๋นผ๋ฉด์„œ Group ์ƒ์„ฑ

      Rollup(์›”,๋ถ€์„œ)
      -- ๋ถ€์„œ ๋นผ๊ณ  ์›”๋ณ„๋กœ ๊ทธ๋ฃนํ™”ํ•œ agg ํ•จ์ˆ˜๊ฐ’๊ณผ 
      -- ๋งˆ์ง€๋ง‰์—๋Š” ๋‘˜๋‹ค ๋นผ๊ณ  ์ดํ•ฉ์œผ๋กœ ๊ทธ๋ฃนํ™”ํ•œ ๊ฐ’์ด ๋‚˜์˜จ๋‹ค.

๐Ÿ“Œ Cube

  • ROLL UP๊ณผ์˜ ์ฐจ์ด
    • ROLL UP์€ ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ํ•˜๋‚˜์”ฉ ๋บŒ
    • CUBE๋Š” ๋ฐฉํ–ฅ ์ƒ๊ด€์—†์ด ํ•˜๋‚˜์”ฉ ๋นผ๋ฉฐ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ์˜ Group ์ƒ์„ฑ
    | CUBE(C1, C2) | ROLLUP(C1,C2) |
    | --- | --- |
    | C1, C2 | C1, C2 |
    | C1 | C1 |
    | C2 | X |
    | () | () |
GROUP BY CUBE((SUBJECT, NAME))

CUBE์— SUBJECT, NAME์ด ๊ด„ํ˜ธ์•ˆ์— ํ•˜๋‚˜์˜ ๊ทธ๋ฃน์ด๊ณ , ์ œ์™ธ ๋˜๋ฉด ์•„๋ฌด ๊ฒƒ๋„ ์—†์œผ๋ฏ€๋กœ ์ „์ฒด ํ‰๊ท  ์ถœ๋ ฅ

๐Ÿ“Œ WINDOW FUNCTION ๊ฐœ์š” ๋ฐ ์„ค๋ช…

์œˆ๋„์šฐ ํ•จ์ˆ˜ (WINDOW FUNCTION)

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

    โ‡’ ๋ถ„์„ ํ•จ์ˆ˜๋‚˜ ์ˆœ์œ„ ํ•จ์ˆ˜๋ž‘ ๋™์˜์–ด

WINDOW FUNCTION ๋ฌธ๋ฒ•

SELECT **WINDOW_FUNCTION** (**ARGUMENTS**) OVER 
( [PARTITION BY **์ปฌ๋Ÿผ**] [ORDER BY **์ปฌ๋Ÿผ**] [WINDOWING ์ ˆ] )
FROM ํ…Œ์ด๋ธ”๋ช… ;
  • OVER ๋ฌธ๊ตฌ๊ฐ€ ํ‚ค์›Œ๋“œ๋กœ ํ•„์ˆ˜ ํฌํ•จ
  • WINDOW_FUNCTION : ์œˆ๋„์šฐ ํ•จ์ˆ˜
  • ARGUMENTS(= ์ธ์ˆ˜) : ํ•จ์ˆ˜์— ๋”ฐ๋ผ 0 ~ N๊ฐœ ์ธ์ˆ˜๊ฐ€ ์ง€์ •๋  ์ˆ˜ ์žˆ๋‹ค.
  • PARTITION BY ์ ˆ : ์ „์ฒด ์ง‘ํ•ฉ์„ ๊ธฐ์ค€์— ์˜ํ•ด ์†Œ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.
  • ORDER BY ์ ˆ : ์–ด๋–ค ํ•ญ๋ชฉ์— ๋Œ€ํ•ด ์ˆœ์œ„๋ฅผ ์ง€์ •ํ•  ์ง€ order by ์ ˆ์„ ๊ธฐ์ˆ ํ•œ๋‹ค.

๐Ÿ“Œ WINDOW FUNCTION ์ข…๋ฅ˜

WINDOW FUNCTION ๋Š” ํฌ๊ฒŒ 5๊ฐ€์ง€ ๊ทธ๋ฃน์œผ๋กœ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ๋‹ค. (๋ฒค๋”๋ณ„๋กœ ์ง€์›ํ•˜๋Š” ํ•จ์ˆ˜ ์ฐจ์ด๊ฐ€ ์žˆ์Œ)

  1. ๐Ÿ“Œ ๊ทธ๋ฃน ๋‚ด ์ˆœ์œ„(RANK) ๊ด€๋ จ ํ•จ์ˆ˜: RANK, DENSE_RANK, ROW_NUMBER
  2. ๐Ÿ“Œ ๊ทธ๋ฃน ๋‚ด ์ง‘๊ณ„(AGGREGATE) ๊ด€๋ จ ํ•จ์ˆ˜ : SUM, MAX, MIN, AVG, COUNT
  3. ๐Ÿ“Œ ๊ทธ๋ฃน ๋‚ด ํ–‰ ์ˆœ์„œ ๊ด€๋ จ ํ•จ์ˆ˜ : FIRST_VALUE, LAST_VALUE, LAG, LEAD
  4. ๐Ÿ“Œ ๊ทธ๋ฃน ๋‚ด ๋น„์œจ ๊ด€๋ จ ํ•จ์ˆ˜ : CUME_DIST, PERCENT_RANK, NTILE, RATIO_TO_REPORT
  5. ์„ ํ˜• ๋ถ„์„์„ ํฌํ•จํ•œ ํ†ต๊ณ„ ๋ถ„์„ ํ•จ์ˆ˜

๐Ÿ“Œ Rank ๊ด€๋ จํ•จ์ˆ˜

  1. ํŠน์ • ํ•ญ๋ชฉ๊ณผ ํŒŒํ‹ฐ์…˜์— ๋Œ€ํ•ด ์ˆœ์œ„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜

    1. ๐Ÿ“Œ RANK โ€” ์ˆœ์œ„๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ๋™์ผํ•œ ์ˆœ์œ„๋Š” ๋™์ผํ•œ ๊ฐ’์ด ๋ถ€์—ฌ๋ฉ๋‹ˆ๋‹ค.

    2. ๐Ÿ“Œ DENSE_RANK โ€” ๋™์ผํ•œ ์ˆœ์œ„๋ฅผ ํ•˜๋‚˜์˜ ๊ฑด์ˆ˜๋กœ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค

      • DENSE = ๋ฐ€์ง‘ํ•œ
        • RANK()๋Š” ๊ฐ™์€ ์ˆœ์œ„๊ฐ€ ์žˆ์œผ๋ฉด(2์œ„) ๊ทธ ๋‹ค์Œ ์ˆœ์œ„(3์œ„)๊ฐ€ ์—†์–ด์ง‘๋‹ˆ๋‹ค.
        • DENSE_RANK()๋Š” ๊ฐ™์€ ์ˆœ์œ„๊ฐ€ ์žˆ์–ด๋„ ๊ฑด์ˆ˜๋กœ ๊ณ„์‚ฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— 3์œ„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
          • ๋นฝ๋นฝํ•˜๊ฒŒ, ๋ฐ€์ ‘ํ•˜๊ฒŒ ๋‹ค ์ฑ„์šด๋‹ค๊ณ  ์ƒ๊ฐ!
    3. ๐Ÿ“Œ ROW_NUMBER โ€” ๋™์ผํ•œ ์ˆœ์œ„์— ๋Œ€ํ•ด ๊ณ ์œ ์˜ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค.

      • ROW_NUMBER๋Š” ๋™์ผํ•œ ์ˆœ์œ„๊ฐ€ ์žˆ๋”๋ผ๋„ ๊ณ ์œ ์˜ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค.

      • RANK, DENSE_RANK ๊ฐ€ ๋™์ผํ•œ ๊ฐ’์— ๋Œ€ํ•ด์„œ๋Š” ๋™์ผํ•œ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•˜๋Š”๋ฐ ๋ฐ˜ํ•ด,

        ROW_NUMBER ๋Š” ๋™์ผํ•œ ๊ฐ’์ด๋ผ๋„ ๊ณ ์œ ํ•œ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌ

๐Ÿ“Œ AGGREGATE ์ง‘๊ณ„ ๊ด€๋ จํ•จ์ˆ˜

  • ์ง‘๊ณ„ ํ•จ์ˆ˜(AGGREGATE Function)
    • SUM, MAX, MIN, AVG, COUNT ๋“ฑ๋“ฑ
    • Group by ๋•Œ ์จ์ฃผ์—ˆ๋˜ ๊ธฐ๋ณธ ์ง‘๊ณ„ํ•จ์ˆ˜

๐Ÿ“Œ ๊ทธ๋ฃน ๋‚ด ํ–‰ ์ˆœ์„œ ๊ด€๋ จ ํ•จ์ˆ˜

  • ํ–‰ ์ˆœ์„œ ๊ด€๋ จ ํ•จ์ˆ˜๋Š” ์ƒ์œ„ ํ–‰์˜ ๊ฐ’์„ ํ•˜์œ„์— ์ถœ๋ ฅํ•˜๊ฑฐ๋‚˜ ํ•˜์œ„ ํ–‰์˜ ๊ฐ’์„ ์ƒ์œ„ ํ–‰์— ์ถœ๋ ฅํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๋˜๋Š” ํŠน์ • ์œ„์น˜์˜ ํ–‰์„ ๊ฐ–๊ณ ์™€ ์ถœ๋ ฅ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • FIRST_VALUE ํ•จ์ˆ˜

    • ํŒŒํ‹ฐ์…˜์—์„œ ๊ฐ€์žฅ ์ฒ˜์Œ์— ๋‚˜์˜ค๋Š” ๊ฐ’
    • MIN ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๊ฒƒ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ
  • LAST_VALUE ํ•จ์ˆ˜

    • ํŒŒํ‹ฐ์…˜์—์„œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋‚˜์˜ค๋Š” ๊ฐ’
    • MIN ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๊ฒƒ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ
  • LAG ํ•จ์ˆ˜

    • ์ด์ „ ํ–‰(๊ฐ’)์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
    • Lag(Sum(amount))
                 OVER (ORDER BY **Extract(month FROM Date(p.payment_date))**) as pre_amt,
  • LEAD ํ•จ์ˆ˜

    • ์ง€์ •๋œ ํ–‰(๊ฐ’)์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ 1์ž…๋‹ˆ๋‹ค.
    • LEAD(sal, 1, 0) 1์„ ์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”๋กœ ๋‹ค์Œ ํ–‰๊ฐ’ ๊ฐ€์ ธ์˜ด
      • 2๋ฅผ ์ฃผ๋ฉด Lead ์—ด์˜ ์ฒซ๋ฒˆ์งธ ๊ฐ’์— sal์˜ ์„ธ๋ฒˆ์งธ ๊ฐ’์„ ๊ฐ€์ ธ์˜ด
      • ์„ธ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ 0์€ null๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ

๐Ÿ“Œ ๊ทธ๋ฃน ๋‚ด ํ–‰ ๋น„์œจ ๊ด€๋ จ ํ•จ์ˆ˜

  • ๋น„์œจ ๊ด€๋ จ ํ•จ์ˆ˜๋Š” ๋ˆ„์  ๋ฐฑ๋ถ„์œจ, ์ˆœ์„œ๋ณ„ ๋ฐฑ๋ถ„์œจ, ํŒŒํ‹ฐ์…˜์„ N๋“ฑ๋ถ„์œผ๋กœ ๋ถ„ํ• ํ•œ ๊ฒฐ๊ณผ ๋“ฑ์„ ์กฐํšŒ

    1. CUME_DIST

      • CUME_DIST -- ํŒŒํ‹ฐ์…˜ ์ „์ฒด์—์„œ ํ˜„์žฌ ํ–‰๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฑด์ˆ˜์— ๋Œ€ํ•œ ๋ˆ„์  ๋ฐฑ๋ถ„์œจ์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
      • ๋ˆ„์  ๋ถ„ํฌ์ƒ ์œ„์น˜๋Š” 0~1 ์‚ฌ์ด์— ์œ„์น˜
      • ์ฆ‰ ๊ทธ๋ƒฅ ๋ˆ„์  ๋ฐฑ๋ถ„์œจ์ด ์–ผ๋งˆ์ธ์ง€ ์กฐํšŒํ•˜๋Š”๊ฒƒ
    2. PERCENT_RANK

      • ํŒŒํ‹ฐ์…˜์—์„œ ์ œ์ผ ๋จผ์ € ๋‚˜์˜จ ๊ฒƒ์„ 0, ์ œ์ผ ๋Šฆ๊ฒŒ ๋‚˜์˜จ ๊ฒƒ์„ 1๋กœ ํ•˜์—ฌ ๊ฐ’์ด ์•„๋‹Œ ํ–‰์˜ ์ˆœ์„œ๋ณ„ ๋ฐฑ๋ถ„์œจ์„ ์กฐํšŒ
      • ํŒŒํ‹ฐ์…˜์„ ๋‚˜๋ˆ„์–ด Partition by ๊ฐ€์žฅ ๋จผ์ € ๋‚˜์˜จ ๊ฒƒ์„ 0 ๋งˆ์ง€๋ง‰์„ 1๋กœํ•˜์—ฌ ๋“ฑ์ˆ˜์˜ ํผ์„ผํŠธ๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

      Untitled

    3. NTILE

      • -- ํŒŒํ‹ฐ์…˜๋ณ„๋กœ ์ „์ฒด ๊ฑด์ˆ˜๋ฅผ ARGUMENT ๊ฐ’์œผ๋กœ N๋“ฑ๋ถ„ํ•œ ๊ฒฐ๊ณผ
      • Ntile(4) over (partition by ~~) โ†’ 4๋“ฑ๋ถ„ํ•œ ๊ฒƒ
        SELECT ENAME, SAL, NTILE(4) OVER (ORDER BY SAL DESC ) AS QUAR_TILE 
        FROM EMP ;
    4. RATIO_TO_REPORT

      • -- ํŒŒํ‹ฐ์…˜ ๋‚ด ์ „์ฒด SUM(์ปฌ๋Ÿผ)์— ๋Œ€ํ•œ ํ–‰ ๋ณ„ ์ปฌ๋Ÿผ ๊ฐ’์˜ ๋ฐฑ๋ถ„์œจ์„ ์†Œ์ˆ˜์ ๊นŒ์ง€ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
        โ‡’ (์ ์œ ์œจ ์กฐํšŒ)
      • ํŒŒํ‹ฐ์…˜์„ ๋‚˜๋ˆ„์–ด ๊ฐ sal์ด ๊ฐ deptno์—์„œ ์–ด๋Š์ •๋„ ์ ์œ ์œจ์„ ๊ฐ–๊ณ  ์žˆ๋Š”์ง€ ์กฐํšŒ

      )์ถœ์ฒ˜

๐Ÿ“Œ ์„ ํ˜• ๋ถ„์„์„ ํฌํ•จํ•œ ํ†ต๊ณ„ ๋ถ„์„ ํ•จ์ˆ˜

  • ์„ ํ˜•๋ถ„์„์„ ํฌํ•จํ•œ ํ†ต๊ณ„๋ถ„์„ ํ•จ์ˆ˜
  • 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

CTE (Common Table Expressions)

  • ๋‹จ์ผ ๋ฌธ์˜ ๋ฒ”์œ„ ๋‚ด์— ์กด์žฌ
    • ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ์“ฐ์ด๋Š” ํŒŒ์ƒํ…Œ์ด๋ธ”(derived table)๊ณผ ๋น„์Šทํ•œ ๊ฐœ๋…
    • ๋ณต์žกํ•œ ์ฟผ๋ฆฌ๋ฌธ์—์„œ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๊ณผ ์žฌ์‚ฌ์šฉ์„ฑ์„ ์œ„ํ•ด ํŒŒ์ƒํ…Œ์ด๋ธ” ๋Œ€์‹  ์‚ฌ์šฉํ•˜๊ธฐ์— ์œ ์šฉ
    • ์ฆ‰, ์„œ๋ธŒ์ฟผ๋ฆฌ๋ณด๋‹ค ๊น”๋”ํ• ์ˆ˜๋„ ?
  • ํ•ด๋‹น ๋ฌธ ๋‚ด์—์„œ ์—ฌ๋Ÿฌ ๋ฒˆ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ช…๋ช…๋œ ์ž„์‹œ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ = ํ…Œ์ด๋ธ”
  • ๐Ÿ“Œ ์ฆ‰, ๊ณ„์†ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์ƒ์˜ ํ…Œ์ด๋ธ”

CTE ๋งŒ๋“ค๊ธฐ

  • ์˜ˆ์‹œ
- `WITH`๋กœ ์‹œ์ž‘ํ•˜์—ฌ *`CTE Body`*๊นŒ์ง€๊ฐ€ CTE์ž…๋‹ˆ๋‹ค.
- `AS()` ์•ˆ์˜ ์ฟผ๋ฆฌ์— ๋”ฐ๋ผ `engineers`๋ผ๋Š” ์ด๋ฆ„์˜ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์ด ์ผ์‹œ์ ์œผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.
- CTE๋กœ ๋งŒ๋“ค์–ด์ง„ ๊ฒฐ๊ณผ `engineers`๋Š” *CTE Usage* ๋ถ€๋ถ„์˜ `FROM` ์ ˆ ๋“ฑ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
- CTE์˜ ๊ฒฐ๊ณผ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅ๋˜์ง€ ์•Š๊ณ , **์ฟผ๋ฆฌ๊ฐ€ ์ˆ˜ํ–‰๋˜๋Š” ๋™์•ˆ์—๋งŒ ์กด์žฌ**ํ•ฉ๋‹ˆ๋‹ค.
  • ๋‹จ์ผ ํ…Œ์ด๋ธ”

WITH ๊ฐ€์ƒํ…Œ์ด๋ธ”๋ช… 
    AS ( SELECT ์ฟผ๋ฆฌ 
                UNION ALL 
                SELECT ์ฟผ๋ฆฌ )
-- ์˜ˆ์‹œ

WITH cte_film
     AS (SELECT film_id,
                title,
                ( CASE
                    WHEN length < 30 THEN 'Short'
                    WHEN length < 90 THEN 'Medium'
                    ELSE 'Long'
                  END ) length
         FROM   film)
SELECT film_id,
       title,
       length
FROM   cte_film
WHERE  length = 'Long'
ORDER  BY title;
  • 2๊ฐœ ์ด์ƒ์˜ ๊ฐ€์ƒ ํ…Œ์ด๋ธ”
  • , ๋กœ ์ถ”๊ฐ€
WITH 
        ๊ฐ€์ƒ1 AS ( ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฌธ ), 
        ๊ฐ€์ƒ2 AS ( ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฌธ )
-- ์‹ค์ œ ์‚ฌ์šฉ 

SELECT ์ปฌ๋Ÿผ, [์ปฌ๋Ÿผ, ...] FROM ๊ฐ€์ƒ1, ๊ฐ€์ƒ2

์„œ๋ธŒ์ฟผ๋ฆฌ ์“ฐ๋ฉด ์•ˆ๋˜๋‚˜ ? ๊ตณ์ด with ์จ์•ผํ•˜๋‚˜ ?

  • ๊ตณ์ด ๊ฐ€์ƒ ํ…Œ์ด๋ธ”์„ With๋ฌธ์œผ๋กœ ๋งŒ๋“ค ํ•„์š”๋Š” ์—†์Œ
  • ํ•˜์ง€๋งŒ With๋ฌธ์„ ํ™œ์šฉํ•˜์˜€์„ ๋•Œ ํ•ด๋‹น SQL ํ™˜๊ฒฝ์—์„œ ์ž„์‹œ๋กœ ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์žฅ์ 
    • ์ฆ‰ CREATE TABLE์„ ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ
    • ์ž„์‹œ๋กœ ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ• ๋•Œ ์œ ์šฉํ•จ

VIEW vs CTE

  • VIEW๋Š” ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ๊ถŒํ•œ์ด ํ•„์š”ํ•˜๊ณ  ์‚ฌ์ „์— ์ •์˜๋ฅผ ํ•ด์•ผํ•œ๋‹ค.
  • ๋ฐ˜๋ฉด, CTE๋Š” ๊ถŒํ•œ์ด ํ•„์š” ์—†๊ณ  ํ•˜๋‚˜์˜ ์ฟผ๋ฆฌ๋ฌธ์ด ๋๋‚ ๋•Œ๊นŒ์ง€๋งŒ ์ง€์†๋˜๋Š” ์ผํšŒ์„ฑ ํ…Œ์ด๋ธ”

์ฃผ์˜ํ• ์ 

  • MySQL 8๊ณผ PostgreSQL์—์„œ๋Š” CTE๋ฅผ Materializingํ•œ๋‹ค
  • CTE๋ฅผ ๋ฌด๋ถ„๋ณ„ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, Query performance๊ฐ€ ์˜คํžˆ๋ ค ๋” ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค
  • Query Optimizer๊ฐ€ ์‹คํ–‰๋ ฅ์„ ๊ณ„์‚ฐํ•  ๋•Œ CTE์˜ ์ตœ์ ํ™”๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๋Š”๋‹ค
    • ์˜ˆ๋กœ, ์„ค์ •ํ•œ index๊ฐ€ CTE์—์„œ๋Š” ์ ์šฉ๋˜์ง€ ์•Š๊ฒŒ ๋œ๋‹ค

window func์˜ PARTITION BY ๋ณต์Šต

๋ถ„์„ํ•จ์ˆ˜([์นผ๋Ÿผ]) OVER(PARTITION BY ์นผ๋Ÿผ1, ์นผ๋Ÿผ2... [ORDER BY ์ ˆ] [WINDOWING ์ ˆ])
  • OVER ์ ˆ ๋‚ด๋ถ€์— PATITION BY ์ ˆ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ์ „์ฒด๋ฅผ ์ง‘๊ณ„
  • PARTITION BY ์ ˆ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์—์„œ ํ•ด๋‹น ์นผ๋Ÿผ์„ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์–ด์„œ ๊ฒฐ๊ณผ๋ฅผ ํ‘œ์‹œ
    • ์ฆ‰, ๊ตฌ๋ฌธ ๋งˆ๋‹ค GROUP BY ํ•ด์„œ ์ปฌ๋Ÿผ์— ๊ฐ’์„ ๋‹ด๋Š”๋‹ค.

๐Ÿ“Œ JOIN ์˜ ์ข…๋ฅ˜

  1. ๋ฌต์‹œ์  ์กฐ์ธ

    SELECT ์†์„ฑ๋ช…

    FROM ํ…Œ์ด๋ธ”1, ํ…Œ์ด๋ธ”2

    WHERE ํ…Œ์ด๋ธ”1.์†์„ฑ1 = ํ…Œ์ด๋ธ”2.์†์„ฑ1

  1. ๋ช…์‹œ์  ์กฐ์ธ

    SELECT ์†์„ฑ๋ช…

    FROM ํ…Œ์ด๋ธ”1 JOIN ํ…Œ์ด๋ธ”2

    ON ํ…Œ์ด๋ธ”1.์†์„ฑ1 = ํ…Œ์ด๋ธ”2.์†์„ฑ1

๐Ÿ“Œ SUBQUERY ์™€ JOIN ์˜ ์ฐจ์ด

์„œ๋ธŒ ์ฟผ๋ฆฌ๋Š” ๋ณต์žกํ•œ SQL ์ฟผ๋ฆฌ๋ฌธ์— ๋งŽ์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

๋ฐ˜๋ฉด์—, ์กฐ์ธ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ฟผ๋ฆฌ๋ฅผ ํ•„์š”๋กœ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์กฐ์ธ์€ ์ฟผ๋ฆฌ๋ฌธ์ด ๋ณต์žกํ•ด์ง€๋”๋ผ๋„ ์„œ๋ธŒ ์ฟผ๋ฆฌ์— ๋น„ํ•ด ์ฝ์–ด๋‚ด๊ธฐ ์ˆ˜์›”ํ•ฉ๋‹ˆ๋‹ค.

  • ์„œ๋ธŒ ์ฟผ๋ฆฌ๋ฅผ ์กฐ์ธ์œผ๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ

    • ๋‚ด๋ถ€ ์ฟผ๋ฆฌ๊ฐ€ ๋‹จ์ผํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋ฉด ์ด๋Š” ์กฐ์ธ์œผ๋กœ๋„ ์ถฉ๋ถ„ํžˆ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค
      • ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ
    • IN ์—ฐ์‚ฐ์ž ์•ˆ์— ์„œ๋ธŒ ์ฟผ๋ฆฌ๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ์„œ๋ธŒ ์ฟผ๋ฆฌ๋ฅผ ์กฐ์ธ์œผ๋กœ ๋ฐ”๊ฟ” ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ๋Š” ๋‚ด๋ถ€ ์ฟผ๋ฆฌ, ์ฆ‰, ์„œ๋ธŒ ์ฟผ๋ฆฌ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
      • NOT IN ์—ฐ์‚ฐ์ž ์•ˆ์— ์žˆ๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌํฌํ•จ
    • EXISTS์™€ NOT EXISTS ์—ฐ์‚ฐ์ž ์•ˆ์— ์žˆ๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ๋„ ์กฐ์ธ์œผ๋กœ ๋Œ€์ฒด
  • ํ•˜์œ„ ์ฟผ๋ฆฌ๋ฅผ JOIN์œผ๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ

    • GROUP BY๊ฐ€ ์žˆ๋Š” FROM์˜ ํ•˜์œ„ ์ฟผ๋ฆฌ

        SELECT city,
               sum_price
        FROM   (SELECT city,
                       Sum(price) AS sum_price
                FROM   sale
                GROUP  BY city) AS s
        WHERE  sum_price < 2100;
    • WHERE ์ ˆ์—์„œ ์ง‘๊ณ„ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•˜์œ„ ์ฟผ๋ฆฌ

        SELECT name FROM product
        WHERE cost<(SELECT AVG(price) from sale);
    • ALL ์ ˆ์˜ ํ•˜์œ„ ์ฟผ๋ฆฌ

      ๐Ÿ“Œ ๊ฒฐ๋ก 

    • ์›ฌ๋งŒํ•˜๋ฉด JOIN ์‚ฌ์šฉํ•˜์ž


์ด๋ฏธ์ง€ ์ถœ์ฒ˜ : https://seohc.tistory.com/87
| ๊ตฌ๋ถ„ | ๋ถ„์„ํ•จ์ˆ˜ |
| --- | --- |
| ์ง‘๊ณ„ | COUNT, MAX, MIN, SUM, AVG |
| ์ˆœ์œ„ | ROW_NUMBER, RANK, DENSE_RANK |
| ์ˆœ์„œ | FIRST_VALUE, LAST_VALUE, LAG, LEAD |
| ํ†ต๊ณ„ | STDDEV, VARIANCE |
| ๋น„์œจ | RATIO_TO_REPORT, CUME_DIST, PERCENT_RANK, NTITLE |

๐Ÿ“Œ JOIN ์˜ ์ข…๋ฅ˜

  1. ๋ฌต์‹œ์  ์กฐ์ธ

    SELECT ์†์„ฑ๋ช…

    FROM ํ…Œ์ด๋ธ”1, ํ…Œ์ด๋ธ”2

    WHERE ํ…Œ์ด๋ธ”1.์†์„ฑ1 = ํ…Œ์ด๋ธ”2.์†์„ฑ1

  1. ๋ช…์‹œ์  ์กฐ์ธ

    SELECT ์†์„ฑ๋ช…

    FROM ํ…Œ์ด๋ธ”1 JOIN ํ…Œ์ด๋ธ”2

    ON ํ…Œ์ด๋ธ”1.์†์„ฑ1 = ํ…Œ์ด๋ธ”2.์†์„ฑ1

๐Ÿ“Œ SUBQUERY ์™€ JOIN ์˜ ์ฐจ์ด

์„œ๋ธŒ ์ฟผ๋ฆฌ๋Š” ๋ณต์žกํ•œ SQL ์ฟผ๋ฆฌ๋ฌธ์— ๋งŽ์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

๋ฐ˜๋ฉด์—, ์กฐ์ธ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ฟผ๋ฆฌ๋ฅผ ํ•„์š”๋กœ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์กฐ์ธ์€ ์ฟผ๋ฆฌ๋ฌธ์ด ๋ณต์žกํ•ด์ง€๋”๋ผ๋„ ์„œ๋ธŒ ์ฟผ๋ฆฌ์— ๋น„ํ•ด ์ฝ์–ด๋‚ด๊ธฐ ์ˆ˜์›”ํ•ฉ๋‹ˆ๋‹ค.

  • ์„œ๋ธŒ ์ฟผ๋ฆฌ๋ฅผ ์กฐ์ธ์œผ๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ

    • ๋‚ด๋ถ€ ์ฟผ๋ฆฌ๊ฐ€ ๋‹จ์ผํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋ฉด ์ด๋Š” ์กฐ์ธ์œผ๋กœ๋„ ์ถฉ๋ถ„ํžˆ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค
      • ์Šค์นผ๋ผ ์„œ๋ธŒ์ฟผ๋ฆฌ
    • IN ์—ฐ์‚ฐ์ž ์•ˆ์— ์„œ๋ธŒ ์ฟผ๋ฆฌ๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ์„œ๋ธŒ ์ฟผ๋ฆฌ๋ฅผ ์กฐ์ธ์œผ๋กœ ๋ฐ”๊ฟ” ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ๋Š” ๋‚ด๋ถ€ ์ฟผ๋ฆฌ, ์ฆ‰, ์„œ๋ธŒ ์ฟผ๋ฆฌ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
      • NOT IN ์—ฐ์‚ฐ์ž ์•ˆ์— ์žˆ๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌํฌํ•จ
    • EXISTS์™€ NOT EXISTS ์—ฐ์‚ฐ์ž ์•ˆ์— ์žˆ๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ๋„ ์กฐ์ธ์œผ๋กœ ๋Œ€์ฒด
  • ํ•˜์œ„ ์ฟผ๋ฆฌ๋ฅผ JOIN์œผ๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ

    • GROUP BY๊ฐ€ ์žˆ๋Š” FROM์˜ ํ•˜์œ„ ์ฟผ๋ฆฌ

        SELECT city,
               sum_price
        FROM   (SELECT city,
                       Sum(price) AS sum_price
                FROM   sale
                GROUP  BY city) AS s
        WHERE  sum_price < 2100;
    • WHERE ์ ˆ์—์„œ ์ง‘๊ณ„ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•˜์œ„ ์ฟผ๋ฆฌ

        SELECT name FROM product
        WHERE cost<(SELECT AVG(price) from sale);
    • ALL ์ ˆ์˜ ํ•˜์œ„ ์ฟผ๋ฆฌ

      ๐Ÿ“Œ ๊ฒฐ๋ก 

    • ์›ฌ๋งŒํ•˜๋ฉด JOIN ์‚ฌ์šฉํ•˜์ž


์ด๋ฏธ์ง€ ์ถœ์ฒ˜ : https://seohc.tistory.com/87

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€