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

[Oracle] ์˜ค๋ผํด GROUP BY ROLLUP, CUBE, GROUPING SETS ์ •๋ฆฌ :: ๋งˆ์ด์ž๋ชฝ

by ๐ŸŒปโ™š 2020. 3. 18.

GROUP BY

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

 

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

 

์˜ค๋ผํด HR ๊ณ„์ •์œผ๋กœ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

1
2
3
4
5
6
7
8
9
10
11
SELECT 
    DEPARTMENT_ID
    ,JOB_ID
    ,SUM(SALARY)
FROM
    EMPLOYEES
WHERE DEPARTMENT_ID > 80
GROUP BY 
    DEPARTMENT_ID
    ,JOB_ID
ORDER BY JOB_ID;

์œ„์˜ ์ฟผ๋ฆฌ์—์„œ๋Š” DEPARTMENT_ID์™€ JOB_ID ๋ณ„๋กœ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‘๊ฐ€์ง€ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆด๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅธ์ชฝ ํ‘œ์™€ ๊ฐ™์ด ๊ฐ€๋กœ์—๋Š” DEPARTMENT_ID, ์„ธ๋กœ๋Š” JOB_ID๊ฐ’๊ณผ ๊ฐ ์นธ์—๋Š” ํ•ด๋‹น ๊ธฐ์ค€์— ๋งž๋Š” ์—ฐ๋ด‰์˜ ํ•ฉ์‚ฐ ๊ฐ’์ด ๋“ค์–ด์žˆ์Šต๋‹ˆ๋‹ค.

 

 

1 : ๊ฐ๊ฐ DEPARTMENT_ID์˜ ์—ฐ๋ด‰ ํ•ฉ์‚ฐ ๊ฐ’

2 : ์ „์ฒด ์—ฐ๋ด‰ ํ•ฉ์‚ฐ ๊ฐ’

3 : ๊ฐ๊ฐ JOB_ID์˜ ์—ฐ๋ด‰ ํ•ฉ์‚ฐ ๊ฐ’

 

ORACLE์—์„œ๋Š” GROUPING BY ์ ˆ๊ณผ ํ•จ๊ป˜ ROLLUP, CUBE, GROUPING SETS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ„ ์ด๋ฏธ์ง€์˜ 1, 2, 3๋ฒˆ ๊ฐ’๋“ค๋„ ๊ฐ™์ด ์ถœ๋ ฅํ•ด ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

ROLLUP

1 : ๊ฐ๊ฐ DEPARTMENT_ID์˜ ์—ฐ๋ด‰ ํ•ฉ์‚ฐ ๊ฐ’

2 : ์ „์ฒด ์—ฐ๋ด‰ ํ•ฉ์‚ฐ ๊ฐ’

ROLLUP์„ ์‚ฌ์šฉํ•˜๋ฉด 1, 2๋ฒˆ์— ํ•ด๋‹นํ•˜๋Š” ๋‚ด์šฉ์„ ํ•จ๊ป˜ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ DEPARTMENT_ID์˜ ์—ฐ๋ด‰ ํ•ฉ์‚ฐ ๊ฐ’๊ณผ ์ „์ฒด ์—ฐ๋ด‰ ํ•ฉ์‚ฐ ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

1
2
3
4
5
6
7
8
9
SELECT
    DEPARTMENT_ID
    ,JOB_ID
    ,SUM(SALARY)
FROM 
    EMPLOYEES
WHERE DEPARTMENT_ID > 80
GROUP BY ROLLUP(DEPARTMENT_ID, JOB_ID)
ORDER BY DEPARTMENT_ID;

GROUP BY ์ ˆ ๋’ค์— ROLLUP์„ ์‚ฌ์šฉํ•ด์ฃผ๊ณ  ์ธ์ž๋กœ ๊ทธ๋ฃนํ™”ํ•˜๋Š” ์นผ๋Ÿผ์„ ์„ ํƒํ•ด์ค๋‹ˆ๋‹ค. ์ธ์ž์˜ ์ฒซ๋ฒˆ์งธ ์ปฌ๋Ÿผ์˜ ํ•ฉ์‚ฐ ๋‚ด์šฉ๊ณผ ์ „์ฒด ํ•ฉ์‚ฐ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•ด์ค๋‹ˆ๋‹ค. ๋งŒ์•ฝ DEPARTMENT_ID์™€ JOB_ID์˜ ์ธ์ž ์œ„์น˜๋ฅผ ๋ฐ”๊พธ๊ฒŒ ๋˜๋ฉด, JOB_ID์— ๋Œ€ํ•œ ํ•ฉ์‚ฐ ๊ธˆ์•ก๊ณผ ์ „์ฒด ํ•ฉ์‚ฐ ๋‚ด์šฉ์ด ์ถœ๋ ฅ๋˜์—ˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ROLLUP์„ ์‚ฌ์šฉํ•˜๋ฉด ์ธ์ž๋กœ ๋„ฃ์–ด์ค€ ๋งจ ์•ž ์นผ๋Ÿผ์„ ๊ธฐ์ค€๊ณผ ์ „์ฒด ๋ฐ์ดํ„ฐ์˜ ๊ทธ๋ฃนํ•จ์ˆ˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.

 

 

CUBE

1 : ๊ฐ๊ฐ DEPARTMENT_ID์˜ ์—ฐ๋ด‰ ํ•ฉ์‚ฐ ๊ฐ’

2 : ์ „์ฒด ์—ฐ๋ด‰ ํ•ฉ์‚ฐ ๊ฐ’

3 : ๊ฐ๊ฐ JOB_ID์˜ ์—ฐ๋ด‰ ํ•ฉ์‚ฐ ๊ฐ’

 

1
2
3
4
5
6
7
8
9
SELECT
    DEPARTMENT_ID
    ,JOB_ID
    ,SUM(SALARY)
FROM
    EMPLOYEES
WHERE DEPARTMENT_ID > 80
GROUP BY CUBE(JOB_ID, DEPARTMENT_ID)
ORDER BY DEPARTMENT_ID;

CUBE๋ฅผ GROUP BY์ ˆ์—์„œ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ด์ค๋‹ˆ๋‹ค.

 

 

GROUPING SETS

 GROUP BY์ ˆ์„ ROLLUP๊ณผ CUBE๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ด ๋ดค์Šต๋‹ˆ๋‹ค. ROLLUP์€ ๋งจ์ฒ˜์Œ์— ๋‚˜์˜ค๋Š” ์ธ์ž ์นผ๋Ÿผ๊ณผ ์ „์ฒด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•œ ๊ทธ๋ฃนํ•จ์ˆ˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์คฌ๊ณ , CUBE๋Š” ์ „์ฒด ๊ฒฝ์šฐ์˜ ์ˆ˜์˜ ๊ทธ๋ฃนํ•จ์ˆ˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ด ์คฌ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ ์ „์ฒด๊ฐ’์˜ ๊ฒฐ๊ณผ ์ฆ‰ 3๋ฒˆ๋งŒ ๋ณด๊ณ  ์‹ถ๋‹ค๊ฑฐ๋‚˜, 3๋ฒˆ์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€๋ฅผ ๋ณด๊ณ ์‹ถ๋‹ค๊ฑฐ๋‚˜, 1,2,3๋ฒˆ์˜ ๊ฒฐ๊ณผ๋ฅผ ์„ ํƒํ•ด์„œ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด GROUPING SETS๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

GROUPING SETS์˜ ์ธ์ž๋กœ ๊ฐ๊ฐ ์›ํ•˜๋Š” ๊ทธ๋ฃน ๋ฐ์ดํ„ฐ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์†Œ๊ด„ํ˜ธ ์•ˆ์— ๊ทธ๋ฃนํ™”ํ•  ์นผ๋Ÿผ์„ ๋จผ์ € ๋„ฃ์–ด์„œ ํ•˜๋‚˜์˜ ๊ทธ๋ฃน์„ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ํ•ด๋‹น ๊ทธ๋ฃน์˜ ๊ฐ๊ฐ ์นผ๋Ÿผ์„ ์ด์šฉํ•ด์„œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ ์„ ํƒํ•ด์„œ ์ถœ๋ ฅ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. "()" ๊ด„ํ˜ธ ๋ฌธ์ž๋ฅผ ์ด์šฉํ•˜๋ฉด ์ „์ฒด ํ•จ์‚ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

GROUPING SETS ์˜ˆ์‹œ

1
2
3
4
5
6
7
8
SELECT
    DEPARTMENT_ID
    ,JOB_ID
    ,SUM(SALARY)
FROM
    EMPLOYEES
WHERE DEPARTMENT_ID > 80
GROUP BY GROUPING SETS((DEPARTMENT_ID, JOB_ID), ());

๊ทธ๋ฃนํ™”ํ•œ ์นผ๋Ÿผ์— ๋Œ€ํ•œ ์ „์ฒด ํ•ฉ์‚ฐ ๊ฒฐ๊ณผ๋งŒ ๋ฐ›์•„์˜ค๋„๋ก ํ•ด๋ดค์Šต๋‹ˆ๋‹ค.

 

๋Œ“๊ธ€