Oracle #8 - ๊ทธ๋ฃนํ•จ์ˆ˜

ennakoidaยท2023๋…„ 6์›” 25์ผ
0

Oracle

๋ชฉ๋ก ๋ณด๊ธฐ
12/13

๐Ÿ“ ๊ทธ๋ฃนํ•จ์ˆ˜

๊ทธ๋ฃนํ•จ์ˆ˜(์ง‘๊ณ„ํ•จ์ˆ˜)๋Š” ํŠน์ •ํ•œ ํ–‰๋“ค์˜ ์ง‘ํ•ฉ์œผ๋กœ ๊ทธ๋ฃน์ด ํ˜•์„ฑ๋˜์–ด ์ ์šฉ ๊ทธ๋ฃน๋‹น 1๊ฐœ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋กœ, ๊ฒฐ๊ณผ๊ฐ€ ๋”ฑ 1ํ–‰๋งŒ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.

์ง‘๊ณ„ํ•จ์ˆ˜

SELECT SUM(SALARY) FROM EMPLOYEE; -- ์ „์ฒด ํ•ฉ
SELECT AVG(SALARY) FROM EMPLOYEE; -- ์ „์ฒด ํ‰๊ท 
SELECT COUNT(SALARY) FROM EMPLOYEE; -- ์ „์ฒด ๋ ˆ์ฝ”๋“œ ๊ฐฏ์ˆ˜
SELECT MAX(SALARY), MIN(SALARY) FROM EMPLOYEE; -- ์ตœ๋Œ€, ์ตœ์†Œ

GROUP BY

SELECT DEPT_CODE, SUM(SALARY) FROM EMPLOYEE GROUP BY DEPT_CODE ORDER BY 1 ASC;

GROUP BY ์ ˆ์€, ๋ณ„๋„์˜ ๊ทธ๋ฃน์ง€์ •์—†์ด ์‚ฌ์šฉํ•œ ๊ทธ๋ฃนํ•จ์ˆ˜๋Š” ๋‹จ ํ•œ๊ฐœ์˜ ๊ฒฐ๊ณผ๊ฐ’๋งŒ ์‚ฐ์ถœํ•˜๊ธฐ ๋•Œ๋ฌธ์—,
๊ทธ๋ฃนํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ์‚ฐ์ถœํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ทธ๋ฃนํ•จ์ˆ˜๊ฐ€ ์ ์šฉ๋  ๊ทธ๋ฃน์˜ ๊ธฐ์ค€์„ GROUP BY์ ˆ์— ๊ธฐ์ˆ ํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.


HAVING

-- ๋ถ€์„œ๋ณ„ ์ธ์›์ˆ˜๋ฅผ ๊ตฌํ•ด๋ณด์‹œ์˜ค.

SELECT DEPT_CODE, COUNT(*) FROM EMPLOYEE GROUP BY DEPT_CODE HAVING COUNT(*) <= 5 ORDER BY 1 DESC;

HAVING์ ˆ์€, ๊ทธ๋ฃนํ•จ์ˆ˜๋กœ ๊ฐ’์„ ๊ตฌํ•ด์˜ฌ ๊ทธ๋ฃน์— ๋Œ€ํ•ด ์กฐ๊ฑด์„ ์„ค์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
WHERE์ ˆ๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ, WHERE์ ˆ์€ SELECT์— ๋Œ€ํ•œ ์กฐ๊ฑด์„ ๋‚˜ํƒ€๋‚ด๋ฏ€๋กœ, ๊ตฌ๋ณ„ํ•ด์„œ ์‚ฌ์šฉํ•  ์ค„ ์•Œ์•„์•ผ ํ•œ๋‹ค.


ROLLUP

-- ๋ถ€์„œ๋‚ด ์ง๊ธ‰๋ณ„ ๊ธ‰์—ฌ์˜ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•˜์‹œ์˜ค

SELECT DEPT_CODE, JOB_CODE, SUM(SALARY) FROM EMPLOYEE GROUP BY ROLLUP(DEPT_CODE, JOB_CODE) ORDER BY 1;

ROLLUP์€, ์ธ์ž๋กœ ์ „๋‹ฌ๋ฐ›์€ ๊ทธ๋ฃน ์ค‘์— ๊ฐ€์žฅ ๋จผ์ € ์ง€์ •ํ•œ ๊ทธ๋ฃน๋ณ„ ํ•ฉ๊ณ„์™€ ์ด ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•œ๋‹ค.


CUBE

-- ๋ถ€์„œ๋‚ด ์ง๊ธ‰๋ณ„ ๊ธ‰์—ฌ์˜ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•˜์‹œ์˜ค

SELECT DEPT_CODE, JOB_CODE, SUM(SALARY) FROM EMPLOYEE GROUP BY CUBE(DEPT_CODE, JOB_CODE) ORDER BY 1;

CUBE๋Š” ๊ทธ๋ฃน์œผ๋กœ ์ง€์ •๋œ ๋ชจ๋“  ๊ทธ๋ฃน์— ๋Œ€ํ•œ ํ•ฉ๊ณ„์™€ ์ด ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•œ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€