[MySQL] 3. SQL - GROUP BY

sorzzzzyยท2021๋…„ 8์›” 13์ผ
0

MySQL

๋ชฉ๋ก ๋ณด๊ธฐ
3/8
post-thumbnail

๐Ÿท GROUP BY

MySQL์—์„œ ์œ ํ˜•๋ณ„๋กœ ๊ฐฏ์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์€๋ฐ, ๋‹จ์ˆœํžˆ COUNT ํ•จ์ˆ˜๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋ฉด ์ „์ฒด ๊ฐฏ์ˆ˜๋งŒ์„ ๊ฐ€์ ธ์˜ค๊ฒŒ ๋œ๋‹ค
์ด๋ ‡๊ฒŒ ์œ ํ˜•๋ณ„๋กœ ๊ฐฏ์ˆ˜๋ฅผ ์•Œ๊ณ  ์‹ถ์„ ๋•Œ๋Š” ์ปฌ๋Ÿผ์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋ฃนํ™” ํ•  ์ˆ˜ ์žˆ๋Š” GROUP BY๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ ์ ˆํ•˜๋‹ค ๐Ÿ˜ƒ

GROUP BY๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋‘ ๊ฐ€์ง€๋ฅผ ๊ผญ ๊ธฐ์–ตํ•˜๊ธฐ!

  • ํŠน์ • ์ปฌ๋Ÿผ์„ ๊ทธ๋ฃนํ™” ํ•˜๋Š” GROUP BY
  • ํŠน์ • ์ปฌ๋Ÿผ์„ ๊ทธ๋ฃนํ™”ํ•œ ํ›„, ๊ทธ ๊ฒฐ๊ณผ์— ์กฐ๊ฑด์„ ๊ฑฐ๋Š” HAVING
    (WHERE๋Š” ๊ทธ๋ฃนํ™” ํ•˜๊ธฐ ์ „ ์— ์‚ฌ์šฉํ•˜๊ณ , HAVING์€ ๊ทธ๋ฃนํ™” ํ›„์— ์‚ฌ์šฉํ•œ๋‹ค)

1. ์ปฌ๋Ÿผ ๊ทธ๋ฃนํ™”

SELECT ์ปฌ๋Ÿผ FROM ํ…Œ์ด๋ธ” GROUP BY ๊ทธ๋ฃนํ™”ํ•  ์ปฌ๋Ÿผ;

2. ์กฐ๊ฑด ์ฒ˜๋ฆฌ ํ›„์— ์ปฌ๋Ÿผ ๊ทธ๋ฃนํ™”

SELECT ์ปฌ๋Ÿผ FROM ํ…Œ์ด๋ธ” WHERE ์กฐ๊ฑด์‹ GROUP BY ๊ทธ๋ฃนํ™”ํ•  ์ปฌ๋Ÿผ;

3. ์ปฌ๋Ÿผ ๊ทธ๋ฃนํ™” ํ›„์— ์กฐ๊ฑด ์ฒ˜๋ฆฌ

SELECT ์ปฌ๋Ÿผ FROM ํ…Œ์ด๋ธ” GROUP BY ๊ทธ๋ฃนํ™”ํ•  ์ปฌ๋Ÿผ HAVING ์กฐ๊ฑด์‹;

4. ์กฐ๊ฑด ์ฒ˜๋ฆฌ ํ›„์— ์ปฌ๋Ÿผ ๊ทธ๋ฃนํ™” ํ›„์— ์กฐ๊ฑด ์ฒ˜๋ฆฌ

SELECT ์ปฌ๋Ÿผ FROM ํ…Œ์ด๋ธ” WHERE ์กฐ๊ฑด์‹ GROUP BY ๊ทธ๋ฃนํ™”ํ•  ์ปฌ๋Ÿผ HAVING ์กฐ๊ฑด์‹;

5.ORDER BY๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ

SELECT ์ปฌ๋Ÿผ FROM ํ…Œ์ด๋ธ” (WHERE ์กฐ๊ฑด์‹)
GROUP BY ๊ทธ๋ฃนํ™”ํ•  ์ปฌ๋Ÿผ (HAVING ์กฐ๊ฑด์‹) ORDER BY ์ปฌ๋Ÿผ1...;

์ฐธ๊ณ  ์ž๋ฃŒ 1

๐Ÿ“Œ DISTINCT & GROUP BY
DISTINCT : ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ์ปฌ๋Ÿผ์ด๋‚˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ์กฐํšŒํ•  ๋•Œ ์‚ฌ์šฉ
GROUP BY : ๊ทธ๋ฃนํ•‘ํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์‚ฌ์šฉ
๋‘ ์ž‘์—…์€ ๋™์ผํ•œ ํ˜•ํƒœ์˜ ์ž‘์—…์ธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๊ณ , ์ผ๋ถ€ ์ž‘์—…์˜ ๊ฒฝ์šฐ DISTINCT๋กœ ๋™์‹œ์— GROUP BY๋กœ๋„ ์ฒ˜๋ฆฌ๋  ์ˆ˜ ์žˆ๋Š” ์ฟผ๋ฆฌ๋“ค์ด ์žˆ๋‹ค!
์ฃผ๋กœ, ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ๊ทธ๋ฃน์œผ๋กœ ๊ตฌ๋ถ„ ํ•  ๋•Œ๋Š” GROUP BY ์ ˆ์„ ์‚ฌ์šฉํ•˜๋ฉฐ, ํŠน์ • ๊ทธ๋ฃน ๊ตฌ๋ถ„์—†์ด ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ฑฐํ•  ๊ฒฝ์šฐ์—๋Š” DISTINCT ์ ˆ์„ ์‚ฌ์šฉ ํ•˜์ž !!
์ฐธ๊ณ  ์ž๋ฃŒ 2



๐Ÿท ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(SQL-Kit GROUP BY)๋ฌธ์ œ ํ’€์ด


1. ๊ณ ์–‘์ด์™€ ๊ฐœ๋Š” ๋ช‡ ๋งˆ๋ฆฌ ์žˆ์„๊นŒ

SELECT ANIMAL_TYPE, COUNT(*) FROM ANIMAL_INS GROUP BY ANIMAL_TYPE ORDER BY ANIMAL_TYPE;

2. ๋™๋ช… ๋™๋ฌผ ์ˆ˜ ์ฐพ๊ธฐ

SELECT NAME, COUNT(*) FROM ANIMAL_INS GROUP BY NAME HAVING COUNT(NAME)>1 ORDER BY NAME;
  • ํŠน์ • ๊ทธ๋ฃน์„ ๊ทธ๋ฃนํ™” ํ•œ ํ›„์— ์กฐ๊ฑด์„ ๊ฑธ๊ณ  ์‹ถ์„ ๋•Œ HAVING ์„ ์‚ฌ์šฉํ•จ
  • NULL ๊ฐ’์€ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ–ˆ์œผ๋ฏ€๋กœ, COUNT(*) ๋Œ€์‹  COUNT(NAME) ์ด๋ผ๊ณ  ์จ์•ผ ํ•œ๋‹ค!

3. ์ž…์–‘ ์‹œ๊ฐ ๊ตฌํ•˜๊ธฐ(1)

SELECT hour(datetime) as HOUR, count(datetime) as 'COUNT'
FROM ANIMAL_OUTS where hour(datetime) between 9 and 19 group by HOUR order by HOUR;

๋‚ ์งœ ๋ฐ์ดํ„ฐ์—์„œ ์ผ๋ถ€๋งŒ ์ถ”์ถœํ•˜๊ธฐ

  • hour(์ปฌ๋Ÿผ๋ช…) : ์‹œ๊ฐ„๋งŒ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋‹ค

4. ์ž…์–‘ ์‹œ๊ฐ ๊ตฌํ•˜๊ธฐ(2)

SET @HOUR = -1;
SELECT 
(@HOUR := @HOUR + 1) As HOUR, 
(SELECT COUNT(*) FROM ANIMAL_OUTS WHERE HOUR(DATETIME) = @HOUR) As COUNT 
FROM ANIMAL_OUTS 
WHERE @HOUR < 23 GROUP BY HOUR ORDER BY HOUR;

๐Ÿ“Œ ๋ณ€์ˆ˜ ์„ค์ • : SET @๋ณ€์ˆ˜๋ช… = ๊ฐ’;
๐Ÿ“Œ ๋Œ€์ž… ์—ฐ์‚ฐ์ž : :=

profile
Backend Developer

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