songining
article thumbnail

๋ฐ์ดํ„ฐ ๋ถ„์„ ํ•จ์ˆ˜ 

(AGGREGATE FUNCTIONS) 

- GROUP FUNCTION์˜ ํ•œ ๋ถ€๋ถ„์ด๋ฉฐ, GROUP AGGREGATE FUNCTION์ด๋ผ๊ณ ๋„ ๋ถ€๋ฅผ ์ˆ˜ ์žˆ๋‹ค. 

COUNT,SUM,AVG,MAX,MIN์™ธ ๊ฐ์ข… ์ง‘๊ณ„ ํ•จ์ˆ˜๋“ค์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค. 

 

(GROUP FUNCTIONS)

- ๊ทธ๋ฃนํ•จ์ˆ˜๋กœ๋Š” ์ง‘๊ณ„ ํ•จ์ˆ˜ ์ œ์™ธํ•˜๋ฉด, ROLLUPํ•จ์ˆ˜, CUBE ํ•จ์ˆ˜, GROUPING SETS ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. 

- ROLLUP์€ GROUP BY์˜ ํ™•์žฅ๋œ ํ˜•ํƒœ๋กœ ๋ณ‘๋ ฌ๋กœ ์ˆ˜ํ–‰์ด ๊ฐ€๋Šฅ. (๊ณ„์ธต์  ๋ถ„๋ฅ˜๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ ์ง‘๊ณ„์— ์ ํ•ฉ)

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

- GROUPING SETS ๋Š” ์›ํ•˜๋Š” ๋ถ€๋ถ„์˜ ์†Œ๊ณ„๋งŒ ์†์‰ฝ๊ฒŒ ์ถ”์ถœ๊ฐ€๋Šฅ. 

- ROLLUP, CUBE, GROUPING SETS ์‚ฌ์šฉ์‹œ ์ •๋ ฌ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ๋Š” ORDER BY ์ ˆ์— ์ •๋ ฌ ์นผ๋Ÿผ์„ ๋ช…์‹œํ•ด์•ผ ํ•œ๋‹ค .

 

(WINDOW FUNCTIONS)

- ๋ถ„์„ํ•จ์ˆ˜(ANALYTIC FUNCTION)๋‚˜ ์ˆœ์œ„ํ•จ์ˆ˜(RANK FUCTION)๋กœ๋„ ์•Œ๋ ค์ ธ ์žˆ์Œ. 

 

๊ทธ๋ฃนํ•จ์ˆ˜(GROUP FUNCTIONS )

1) ROLLUP ํ•จ์ˆ˜ 

- GROUP BY ์ ˆ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉ ๋˜๋ฉฐ, GROUP BY์ ˆ์— ์˜ํ•ด์„œ ๊ทธ๋ฃน ์ง€์–ด์ง„ ์ง‘ํ•ฉ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด์„œ ์ข€ ๋” ์ƒ์„ธํ•œ ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ ํ•œ๋‹ค.

- ์ธ์ˆ˜๊ฐ€ ๊ณ„์ธต๊ตฌ์กฐ์ด๋ฏ€๋กœ ์ธ์ˆ˜ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ๋„ ๋ฐ”๋€Œ๊ฒŒ ๋˜๋ฏ€๋กœ ์ธ์ˆ˜์˜ ์ˆœ์„œ์— ์ฃผ์˜ํ•ด์•ผํ•จ. 

์˜ˆ์ œ1 ) SELECT DNAME, JOB, COUNT(*) "Total Empl", SUM(SAL) "Total Sal"
          FROM EMP, DEPT
          WHERE DEPT.DEPTNO = EMP.DEPTNO
          GROUP BY ROLLUP (DNAME, JOB)

DNAME๊ณผ JOB์— ๋Œ€ํ•œ ์†Œ๊ณ„๊ฐ€ ๊ณ„์‚ฐ๋จ 

์˜ˆ์ œ2 ) GROUPING ํ•จ์ˆ˜ ์‚ฌ์šฉ 

ROLLUP ์ด๋‚˜ CUBE์— ์˜ํ•œ ์†Œ๊ณ„๊ฐ€ ๊ณ„์‚ฐ๋œ ๊ฒฐ๊ณผ์—๋Š” GROUPING(EXPR) = 1์ด ํ‘œ์‹œ๋˜๊ณ , 

๊ทธ ์™ธ์˜ ๊ฒฐ๊ณผ์—๋Š” GROUPING(EXPR) = 0 ์ด ํ‘œ์‹œ๋œ๋‹ค. 

SELECT DNAME, GROUPING(DNAME), JOB, GROUPING(JOB), COUNT(*) "Total  Empl", SUM(SAL) "Total Sal"

FROM EMP,DEPT

WHERE DEPT.DEPTNO = EMP.DEPTNO

GROUP BY ROLLUP (DNAME, JOB); 

๊ฐ ์†Œ๊ณ„ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด 1 ํ‘œ์‹œ 

+) ์ด์™ธ์—๋„ CASEํ•จ์ˆ˜๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ GROUP BY์ ˆ์—์„œ ROLLUPํ•จ์ˆ˜๋ฅผ ํŠน์ • ์†์„ฑ์—๋งŒ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์—ฌ๋Ÿฌ ์†์„ฑ์„ ๊ด„ํ˜ธ๋กœ ๋ฌถ์–ด ํ•˜๋‚˜์˜ ์ง‘ํ•ฉ์œผ๋กœ ๊ฐ„์ฃผํ•˜๋Š” ๋“ฑ์˜ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

2) CUBE ํ•จ์ˆ˜  

- ROLLUP์—์„œ๋Š” ๋‹จ์ง€ ๊ฐ€๋Šฅํ•œ Subtotal๋งŒ์„ ์ƒ์„ฑํ•˜์˜€์ง€๋งŒ, CUBE๋Š” ๊ฒฐํ•ฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฐ’์— ๋Œ€ํ•˜์—ฌ ๋‹ค์ฐจ์› ์ง‘๊ณ„๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. 

- CUBE๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ๋Š” Grouping Columns์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊พธ์–ด์„œ ๋˜ ํ•œ๋ฒˆ์˜ Query๋ฅผ ์ถ”๊ฐ€ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค. 

- ROLLUP์— ๋น„ํ•ด ์‹œ์Šคํ…œ์— ๋งŽ์€ ๋ถ€๋‹ด์„ ์ฃผ๋ฏ€๋กœ ์‚ฌ์šฉ์— ์ฃผ์˜ํ•ด์•ผ ํ•จ. 

- ROLLUP๊ณผ๋Š” ๋‹ฌ๋ฆฌ ํ‰๋“ฑํ•œ ๊ด€๊ณ„์ด๋ฏ€๋กœ ์ธ์ˆ˜์˜ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋Š” ๊ฒฝ์šฐ ํ–‰๊ฐ„์— ์ •๋ ฌ ์ˆœ์„œ๋Š” ๋ฐ”๋€” ์ˆ˜ ์žˆ์–ด๋„ ๋ฐ์ดํ„ฐ ๊ฒฐ๊ณผ๋Š” ๊ฐ™๋‹ค. 

 

์˜ˆ์ œ1) CUBE ํ•จ์ˆ˜ ์ด์šฉ 

SELECT

CASE GROUPING(DNAME)
  WHEN 1 THEN 'All Departmets'
  ELSE DNAME END AS DNAME,
CASE GROUPING(JOB)
  WHEN 1 THEN 'All Jobs'
  ELSE JOB END AS JOB,
COUNT(*) "Total Empl",
SUM(SAL) "Total Sal",
FROM EMP, DEPT
WHERE DEPT.DEPTNO = EMP.DEPTNO
GROUP BY CUBE (DNAME, JOB);

 

๊ฒฐํ•ฉ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฐ’์— ๋Œ€ํ•œ ์ง‘๊ณ„

3) GROUPING SETS ํ•จ์ˆ˜  

๊ณ„์ธต ๊ตฌ์กฐ์ธ ROLLUP๊ณผ๋Š” ๋‹ฌ๋ฆฌ ํ‰๋“ฑํ•œ ๊ด€๊ณ„์ด๋ฏ€๋กœ ์ธ์ˆ˜์˜ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ์–ด๋„ ๊ฒฐ๊ณผ๋Š” ๊ฐ™๋‹ค. 

GROUP BY SQL ๋ฌธ์žฅ์„ ์—ฌ๋Ÿฌ๋ฒˆ ๋ฐ˜๋ณตํ•˜์ง€ ์•Š์•„๋„ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์‰ฝ๊ฒŒ ์–ป์„ ์ˆ˜ ์žˆ๊ฒŒ ๋จ. 

- GROUPING SETS(์ธ์ˆ˜1, ์ธ์ˆ˜2)๋Š” (์ธ์ˆ˜1 ๊ทธ๋ฃนํ•‘ ๊ฒฐ๊ณผ)  + (์ธ์ˆ˜2 ๊ทธ๋ฃนํ•‘ ๊ฒฐ๊ณผ)๋ฅผ ํ•ฉ์นœ(UNION ALL) ๊ฒฐ๊ณผ์ž„.

 

์˜ˆ์ œ 1) 

SELECT
DECODE(GROUPING(DNAME),1,
'All Departments', DNAME) AS DNAME,
DECODE(GROUPING(JOB), 1,
'All Jobs', JOB) AS JOB,
           COUNT(*) "Total Empl ",
           SUM(SAL) "Total Sal"
FROM EMP, DEPT
WHERE DEPT.DEPTNO = EMP.DEPTNO
GROUP BY GROUPING SETS (DNAME, JOB);

GROUPING SETS ๊ฒฐ๊ณผ