๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป Computer Science/๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

[๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค] ๊ทธ๋ฃนํ•จ์ˆ˜ (ROLLUP, CUBE, GROUPING SETS)๋ž€? ์˜ˆ์ œ ํฌํ•จ

by Jay Din 2024. 8. 22.
728x90
๋ฐ˜์‘ํ˜•

ROLLUP, CUBE, GROUPING SETS๋Š” SQL์—์„œ ์ง‘๊ณ„ ๊ฒฐ๊ณผ๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

์ด๋“ค์€ ๋ฐ์ดํ„ฐ์˜ ์—ฌ๋Ÿฌ ์ง‘๊ณ„ ์ˆ˜์ค€์„ ๊ณ„์‚ฐํ•˜๊ฑฐ๋‚˜ ๋ณด๊ณ ์„œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

ROLLUP, CUBE, GROUPING SETS ์š”์•ฝ ์ •๋ฆฌ

ROLLUP ์ง€์ •๋œ ์—ด ์ˆœ์„œ์— ๋”ฐ๋ผ ๊ณ„์ธต์  ์ง‘๊ณ„๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์†Œ๊ณ„์™€ ์ด๊ณ„๋ฅผ ํฌํ•จ
CUBE ์ง€์ •๋œ ์—ด์˜ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ์— ๋Œ€ํ•ด ์ง‘๊ณ„๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์ฐจ์› ๋ถ„์„์— ์œ ์šฉ
GROUPING SETS ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•œ ํŠน์ • ๊ทธ๋ฃนํ™” ์ง‘ํ•ฉ์— ๋Œ€ํ•ด ์ง‘๊ณ„๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์œ ์—ฐํ•˜๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

 

ํ•œ ์ค„ ์š”์•ฝ์œผ๋กœ ์™ธ์šฐ๊ธฐ

  • ROLLUP = "์ญ‰ ๋ง์•„๊ฐ€๋Š” ์†Œ๊ณ„์™€ ์ด๊ณ„"
    • ๊ทธ๋ฃนํ•‘ ์—ด์ด ์ˆœ์ฐจ์ ์œผ๋กœ ์†Œ๊ณ„๋ฅผ ํฌํ•จํ•˜๋ฉด์„œ ์ ์  ๋” ํฐ ์ง‘๊ณ„๋ฅผ ๋งŒ๋“ ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  • CUBE = "๋ชจ๋“  ๋ฐฉํ–ฅ์—์„œ ์™„์ „ํ•œ ์ง‘๊ณ„"
    • ํ๋ธŒ๋Š” ๋ชจ๋“  ๋ฉด์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ž…์ฒด ๋„ํ˜•์ด์ฃ . ๋ชจ๋“  ๋ฐฉํ–ฅ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.
  • GROUPING SETS = "ํ•„์š”ํ•œ ์ง‘ํ•ฉ๋งŒ ๋งž์ถค ์ง‘๊ณ„"
    • ๋งž์ถคํ˜•"์ฒ˜๋Ÿผ ์›ํ•˜๋Š” ์กฐํ•ฉ๋งŒ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

 


 

1. ROLLUP

ROLLUP์€ ๊ณ„์ธต์  ์ง‘๊ณ„๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฃนํ™”๋œ ์—ด์˜ ์ˆœ์„œ์— ๋”ฐ๋ผ ์ ์ง„์ ์œผ๋กœ ์ง‘๊ณ„๋ฅผ ๊ณ„์‚ฐํ•˜๋ฉฐ, ์ตœ์ข…์ ์œผ๋กœ ์ดํ•ฉ๊ณ„๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ๋ณด๊ณ ์„œ์˜ ์†Œ๊ณ„์™€ ์ด๊ณ„๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ:

๋งค์ถœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” Sales ํ…Œ์ด๋ธ”์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ด…์‹œ๋‹ค.

Year Quarter Revenue
2022 Q1 100
2022 Q2 150
2022 Q3 200
2022 Q4 250
2023 Q1 300
2023 Q2 350

 

ROLLUP์„ ์‚ฌ์šฉํ•œ ์ฟผ๋ฆฌ:

SELECT Year, Quarter, SUM(Revenue) AS TotalRevenue
FROM Sales
GROUP BY ROLLUP(Year, Quarter);

 

Year Quarter TotalRevenue
2022 Q1 100
2022 Q2 150
2022 Q3 200
2022 Q4 250
2022 NULL 700
2023 Q1 300
2023 Q2 350
2023 NULL 650
NULL NULL 1350

 

  • ROLLUP์€ Year์™€ Quarter์— ๋”ฐ๋ผ ์ง‘๊ณ„๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉฐ, Year๋ณ„๋กœ ์†Œ๊ณ„, ์ „์ฒด ์ด๊ณ„๊นŒ์ง€ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
  • NULL ๊ฐ’์€ ์†Œ๊ณ„ ๋ฐ ์ด๊ณ„๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

 

 

2. CUBE

CUBE๋Š” ์ฃผ์–ด์ง„ ์—ด์— ๋Œ€ํ•ด ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์กฐํ•ฉ์œผ๋กœ ์ง‘๊ณ„๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

์ด ๊ธฐ๋Šฅ์€ ๋‹ค์ฐจ์› ๋ถ„์„์— ์œ ์šฉํ•˜๋ฉฐ, ๋ณด๊ณ ์„œ์˜ ๊ฐ ์ฐจ์›์— ๋Œ€ํ•œ ๋ชจ๋“  ๊ต์ฐจ ์ง‘๊ณ„๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ:

๊ฐ™์€ Sales ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

CUBE๋ฅผ ์‚ฌ์šฉํ•œ ์ฟผ๋ฆฌ:

SELECT Year, Quarter, SUM(Revenue) AS TotalRevenue
FROM Sales
GROUP BY CUBE(Year, Quarter);

 

Year Quarter TotalRevenue
2022 Q1 100
2022 Q2 150
2022 Q3 200
2022 Q4 250
2022 NULL 700
2023 Q1 300
2023 Q2 350
2023 NULL 650
NULL Q1 400
NULL Q2 500
NULL NULL 1350

 

  • CUBE๋Š” ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ์— ๋Œ€ํ•ด ์ง‘๊ณ„๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
  • Year์™€ Quarter์— ๋Œ€ํ•œ ๋ชจ๋“  ์กฐํ•ฉ, ๊ฐ ์—ด์— ๋Œ€ํ•œ ์†Œ๊ณ„, ๊ทธ๋ฆฌ๊ณ  ์ด๊ณ„๊นŒ์ง€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.

 

3. GROUPING SETS

 

GROUPING SETS๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ๊ทธ๋ฃน ์ง‘ํ•ฉ์„ ์ •์˜ํ•˜์—ฌ ์ง‘๊ณ„๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

ํŠน์ • ๊ทธ๋ฃนํ™”๋ฅผ ์ง์ ‘ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์–ด, ROLLUP์ด๋‚˜ CUBE๋ณด๋‹ค ๋” ์œ ์—ฐํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ:

Sales ํ…Œ์ด๋ธ”์—์„œ ํŠน์ • ๊ทธ๋ฃนํ™” ์ง‘ํ•ฉ๋งŒ ๊ณ„์‚ฐํ•˜๊ณ  ์‹ถ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

GROUPING SETS๋ฅผ ์‚ฌ์šฉํ•œ ์ฟผ๋ฆฌ:

SELECT Year, Quarter, SUM(Revenue) AS TotalRevenue
FROM Sales
GROUP BY GROUPING SETS ((Year, Quarter), (Year), (Quarter));

 

Year Quarter TotalRevenue
2022 Q1 100
2022 Q2 150
2022 Q3 200
2022 Q4 250
2023 Q1 300
2023 Q2 350
2022 NULL 700
2023 NULL 650
NULL Q1 400
NULL Q2 500

 

  • GROUPING SETS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ์ง‘ํ•ฉ์— ๋Œ€ํ•ด์„œ๋งŒ ์ง‘๊ณ„๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  • ์—ฌ๊ธฐ์„œ๋Š” Year์™€ Quarter๋กœ ๊ทธ๋ฃนํ™”, Year๋กœ ๊ทธ๋ฃนํ™”, ๊ทธ๋ฆฌ๊ณ  Quarter๋กœ ๊ทธ๋ฃนํ™”๋œ ์ง‘๊ณ„๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ํ•„์š”ํ•œ ์กฐํ•ฉ๋งŒ ์ง‘๊ณ„ํ•  ์ˆ˜ ์žˆ์–ด, ๋ถˆํ•„์š”ํ•œ ์กฐํ•ฉ์— ๋Œ€ํ•œ ์ง‘๊ณ„๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

728x90
๋ฐ˜์‘ํ˜•