ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/DataBase

[SQL] GROUP BY ์ ˆ

NaNaRin๐Ÿ™ƒ 2021. 3. 3. 21:18

1. GROUP BY ์ ˆ

- ๊ทธ๋ฃนํ™” : ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ์—์„œ ์˜๋ฏธ ์žˆ๋Š” ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋ฅผ ํŠน์ • ์นผ๋Ÿผ ๊ฐ’ ๋ณ„๋กœ ๋ฌถ์–ด์„œ ์ถœ๋ ฅ

  ex) DEPTNO๊ฐ€ 10์ผ ๋•Œ, 20์ผ ๋•Œ, 30์ผ ๋•Œ ๊ธ‰์—ฌ์˜ ํ‰๊ท ๊ฐ’์„ ๊ตฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ ํ•˜๋‚˜์”ฉ SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์•ผํ•˜์ง€๋งŒ GROUP BY ์ ˆ์„ ์‚ฌ์šฉํ•˜๋ฉด DEPTNO ๋ณ„๋กœ ๋ฌถ์–ด์„œ ํ‰๊ท ๊ฐ’์„ ํ•œ๋ฒˆ์— ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค

SELECT 		[์กฐํšŒํ•  ์นผ๋Ÿผ 1 ์ด๋ฆ„], [์นผ๋Ÿผ 2 ์ด๋ฆ„], … , [์นผ๋Ÿผ N ์ด๋ฆ„]
FROM 		[์กฐํšŒํ•  ํ…Œ์ด๋ธ” ์ด๋ฆ„]
WHERE		[์กฐํšŒํ•  ์นผ๋Ÿผ์„ ์„ ๋ณ„ํ•˜๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด์‹]
GROUP BY	[๊ทธ๋ฃนํ™”ํ•  ์นผ๋Ÿผ์„ ์ง€์ •(์—ฌ๋Ÿฌ๊ฐœ ๊ฐ€๋Šฅ)]
ORDER BY	[์ •๋ ฌํ•˜๋ ค๋Š” ์นผ๋Ÿผ ์ง€์ •] [์ •๋ ฌ ์˜ต์…˜];

-- 1. DEPTNO ๋ณ„๋กœ ๊ทธ๋ฃนํ™”
SELECT		AVG(SAL), DEPTNO
FROM		EMP
GROUP BY	DEPTNO;

-- 2. DEPTNO ๋ณ„๋กœ ๊ทธ๋ฃนํ™”
SELECT		AVG(SAL), DEPTNO, JOB
FROM		EMP
GROUP BY	DEPTNO, JOB
ORDER BY	DEPTNO, JOB;

  (1) DEPTNO ๋ณ„๋กœ SAL ์นผ๋Ÿผ์˜ ํ‰๊ท ์„ ๊ตฌํ•ด ์ถœ๋ ฅ

  (2) DEPTNO, JOB ๋ณ„๋กœ SAL ์นผ๋Ÿผ์˜ ํ‰๊ท ์„ ๊ตฌํ•ด DEPTNO, JOB ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅ

        -> DEPTNO๊ฐ€ 10์ด๊ณ  JOB์ด MANAGER์ธ ์‚ฌ๋žŒ๋“ค์˜ SAL ํ‰๊ท , DEPTNO๊ฐ€ 20์ด๊ณ  JOB์ด SALESMAN์ธ ์‚ฌ๋žŒ๋“ค์˜ SAL ํ‰๊ท , …

 

2. GROUP BY ์ ˆ ์‚ฌ์šฉ ์‹œ ์œ ์˜์ 

- ๋‹ค์ค‘ํ–‰ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ์ผ๋ฐ˜ ์นผ๋Ÿผ์€ GROUP BY ์ ˆ์— ๋ช…์‹œํ•˜์ง€ ์•Š์œผ๋ฉด SELECT ์ ˆ์—์„œ ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅ

- ๋‹ค์ค‘ํ–‰ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•œ ํ–‰์œผ๋กœ ์ถœ๋ ฅ๋˜์ง€๋งŒ ์ผ๋ฐ˜ ์นผ๋Ÿผ์€ ์—ฌ๋Ÿฌ ํ–‰์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ๊ฐ ์นผ๋Ÿผ๋ณ„ ๋ฐ์ดํ„ฐ ์ˆ˜๊ฐ€ ๋‹ฌ๋ผ์ ธ ์ถœ๋ ฅ์ด ๋ถˆ๊ฐ€๋Šฅํ•จ

 

3. HAVING ์ ˆ

- GROUP BY ์ ˆ์— ์กฐ๊ฑด์„ ์ค„ ๋•Œ ์‚ฌ์šฉ. ๋”ฐ๋ผ์„œ GROUP BY ์ ˆ์ด ์กด์žฌํ•  ๋•Œ๋งŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅ

- GROUP BY ์ ˆ์„ ํ†ตํ•ด ๊ทธ๋ฃนํ™”๋œ ๊ฒฐ๊ณผ ๊ฐ’์˜ ๋ฒ”์œ„๋ฅผ ์ œํ•œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ

SELECT 		[์กฐํšŒํ•  ์นผ๋Ÿผ 1 ์ด๋ฆ„], [์นผ๋Ÿผ 2 ์ด๋ฆ„], … , [์นผ๋Ÿผ N ์ด๋ฆ„]
FROM 		[์กฐํšŒํ•  ํ…Œ์ด๋ธ” ์ด๋ฆ„]
WHERE		[์กฐํšŒํ•  ์นผ๋Ÿผ์„ ์„ ๋ณ„ํ•˜๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด์‹]
GROUP BY	[๊ทธ๋ฃนํ™”ํ•  ์นผ๋Ÿผ์„ ์ง€์ •(์—ฌ๋Ÿฌ๊ฐœ ๊ฐ€๋Šฅ)]
HAVING		[์ถœ๋ ฅ ๊ทธ๋ฃน์„ ์ œํ•œํ•˜๋Š” ์กฐ๊ฑด์‹]
ORDER BY	[์ •๋ ฌํ•˜๋ ค๋Š” ์นผ๋Ÿผ ์ง€์ •] [์ •๋ ฌ ์˜ต์…˜];

-- 1. 
SELECT		AVG(SAL), DEPTNO, JOB
FROM		EMP
GROUP BY	DEPTNO, JOB
HAVING		AVG(SAL) >= 2000
ORDER BY	DEPTNO, JOB;

  (1) DEPTNO, JOB ๋ณ„๋กœ SAL ์นผ๋Ÿผ์˜ ํ‰๊ท ์„ ๊ตฌํ•ด DEPTNO, JOB ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋˜ AVG(SAL)์ด 2000 ์ด์ƒ์ธ ๋•Œ๋งŒ ์ถœ๋ ฅ

 

4. HAVING ์ ˆ ์‚ฌ์šฉ ์‹œ ์œ ์˜์ 

- WHERE ์ ˆ :  ์ถœ๋ ฅ ๋Œ€์ƒ ํ–‰์„ ์ œํ•œ / HAVING ์ ˆ : ๊ทธ๋ฃนํ™” ๋œ ๋Œ€์ƒ์„ ์ถœ๋ ฅ์—์„œ ์ œํ•œ

- ์กฐ๊ฑด์‹์„ ์ง€์ •ํ•˜์—ฌ ์กฐ๊ฑด์ด ์ฐธ์ธ ๊ฒฐ๊ณผ๋งŒ ์ถœ๋ ฅํ•œ๋‹ค๋Š” ์ ์—์„œ HAVING ์ ˆ์ด WHERE ์ ˆ๊ณผ ๋น„์Šทํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ ๋‹ค๋ฅด๋‹ค

-- 1.
SELECT		AVG(SAL), DEPTNO, JOB
FROM		EMP
WHERE		AVG(SAL) >= 2000
GROUP BY	DEPTNO, JOB
ORDER BY	DEPTNO, JOB;

-- 2.
SELECT		AVG(SAL), DEPTNO, JOB
FROM		EMP
WHERE		SAL >= 2000
GROUP BY	DEPTNO, JOB
HAVING		AVG(SAL) >= 2000
ORDER BY	DEPTNO, JOB;

  (1) ์˜ค๋ฅ˜ ๋ฐœ์ƒ

  (2) SAL ์ด 2000 ์ด์ƒ์ธ ํ–‰๋งŒ (WHERE ์ ˆ) DEPTNO, JOB ๋ณ„๋กœ SAL ์นผ๋Ÿผ์˜ ํ‰๊ท ์„ ๊ตฌํ•ด 

        DEPTNO, JOB ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋˜ AVG(SAL)์ด 2000 ์ด์ƒ์ธ ๋•Œ๋งŒ ์ถœ๋ ฅ (HAVING ์ ˆ)