๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
DB/MySQL

[MySQL] ๋ฐ์ดํ„ฐ ๊ทธ๋ฃนํ™” - GROUP BY

by yunamom 2022. 3. 28.
๋ฐ˜์‘ํ˜•

GROUP BY ๋ž€?

๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜๋Š” ๊ฒƒ, ์ฆ‰ ๋ฐ์ดํ„ฐ๋ฅผ ์ง‘๊ณ„ํ•˜๋Š” ๊ฒƒ์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์—์„œ ์ƒ๋‹นํžˆ ์ค‘์š”ํ•œ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค.

GROUP BY๋ฅผ ์–ด๋–ค ์‹์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํฌ์ŠคํŒ… ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค :D 


๐Ÿ’ก์™œ ํ–‰์„ ๊ทธ๋ฃนํ™”ํ•ด์•ผ ํ• ๊นŒ์š”?

์šฐ๋ฆฌ๋Š” SQL์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋ฃนํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์„ ์ธ์ง€ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทผ๋ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์™œ ๊ทธ๋ฃนํ™”ํ•ด์•ผ ํ• ๊นŒ์š”? ์ฐธ๊ณ ๋กœ, GROUP BY ์ ˆ์€ ์ฃผ๋กœ ์ง‘๊ณ„ ํ•จ์ˆ˜์™€ ๊ฐ™์ด ์‚ฌ์šฉ๋˜๊ณค ํ•ฉ๋‹ˆ๋‹ค.

์ง‘๊ณ„ ํ•จ์ˆ˜๋Š” ์—ฌ๋Ÿฌ ํ–‰์˜ ๊ฐ’์„ ๋”ํ•˜๊ฑฐ๋‚˜, ํ‰๊ท ๊ฐ’์„ ๋‚ด๊ฑฐ๋‚˜, ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ๋“ฑ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ์— ๊ด€ํ•œ ๊ณ„์‚ฐ์„ ํ•ฉ๋‹ˆ๋‹ค.

๊ฐ€์žฅ ๋Œ€ํ‘œ์ ์ธ ์ง‘๊ณ„ ํ•จ์ˆ˜์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

1. COUNT() : ํ–‰์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์–ด์คŒ
2. AVG() : ํ–‰ ์•ˆ์— ์žˆ๋Š” ๊ฐ’์˜ ํ‰๊ท ์„ ๋‚ด์–ด์คŒ
3. MIN() : ํ–‰ ์•ˆ์— ์žˆ๋Š” ๊ฐ’์˜ ์ตœ์†Ÿ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์คŒ
4. MAX() : ํ–‰ ์•ˆ์— ์žˆ๋Š” ๊ฐ’์˜ ์ตœ๋Œ“๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์คŒ
5. SUM() : ํ–‰ ์•ˆ์— ์žˆ๋Š” ๊ฐ’์˜ ํ•ฉ์„ ๋‚ด์–ด์คŒ


# ๊ทธ๋ฃนํ•จ์ˆ˜ 
 
 # COUNT()
 # SUM()
 # AVG()
 # MAX()
 # MIN()
 
 select * from member;
 # ๋ฌธ์ œ) memberํ…Œ์ด๋ธ”์—์„œ ์ „์ฒด ํšŒ์› ์ˆ˜ ๊ฐ€์ ธ์˜ค๊ธฐ
 SELECT COUNT(*) FROM member;
 
 # ๋ฌธ์ œ) memberํ…Œ์ด๋ธ”์—์„œ ๋‚จ์ž ํšŒ์›์˜ ์ˆ˜ ๊ฐ€์ ธ์˜ค๊ธฐ
 SELECT COUNT(*) FROM member WHERE member_gender='m';
 
 # ๋ฌธ์ œ) bookํ…Œ์ด๋ธ”์—์„œ ์ „์ฒด ๋„์„œ ๊ฐ€๊ฒฉ์˜ ์ดํ•ฉ ๊ฐ€์ ธ์˜ค๊ธฐ
 SELECT SUM(book_price) FROM book;
 
 # ๋ฌธ์ œ) bookํ…Œ์ด๋ธ”์—์„œ ์ „์ฒด ๋„์„œ ๊ฐ€๊ฒฉ์˜ ํ‰๊ท  ๊ฐ€์ ธ์˜ค๊ธฐ
 # ์กฐ๊ฑด) ์†Œ์ˆ˜์  ์ดํ•˜ 2๊ฐ€์ง€๊นŒ์ง€ ์ถœ๋ ฅ
 SELECT AVG(book_price) FROM book;
 SELECT ROUND(AVG(book_price), 2) FROM book;
 
 # ๋ฌธ์ œ) bookํ…Œ์ด๋ธ”์—์„œ ๋„์„œ ๊ฐ€๊ฒฉ์„ 10%๋กœ ์ธ์ƒํ–ˆ์„ ๋•Œ์˜ ์ดํ•ฉ ๊ฐ€์ ธ์˜ค๊ธฐ
 SELECT SUM(book_price), SUM(book_price * 1.1) FROM book;
 
# ๋ฌธ์ œ) bookํ…Œ์ด๋ธ”์—์„œ ๋„์„œ ๊ฐ€๊ฒฉ์ด ๊ฐ€์žฅ ๋†’์€ ๋„์„œ๊ฐ€๊ฒฉ ๊ฐ€์ ธ์˜ค๊ธฐ
SELECT MAX(book_price) FROM book;

#--------------------------------------------------------------

# GROUP BY(๊ทธ๋ฃนํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ)
-- GROUP BY ๋  ๋•Œ์— ๋‚ด๋ถ€์ ์œผ๋กœ ์ž„์‹œ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ ๋‹ค.
-- ์ด๋•Œ ์ž„์‹œ ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ์€ GROUP BY์— ์‚ฌ์šฉ๋œ ์ปฌ๋Ÿผ๊ณผ
-- SELECT์—์„œ ์‚ฌ์šฉ๋œ ๊ทธ๋ฃนํ•จ์ˆ˜๊ฐ€ ๋œ๋‹ค.

# ๋ฌธ์ œ) memberํ…Œ์ด๋ธ”์—์„œ ํšŒ์›์˜ ์ˆ˜๋ฅผ ์„ฑ๋ณ„๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ
SELECT member_gender, COUNT(*) FROM member;  -- ์ž˜๋ชป๋œ ๊ฒฐ๊ณผ ์ถœ๋ ฅ
SELECT member_gender, COUNT(*) FROM member GROUP BY member_gender;

 # ๋ฌธ์ œ) bookํ…Œ์ด๋ธ”์—์„œ ๋„์„œ ์ข…๋ฅ˜๋ณ„ ๋„์„œ ์ˆ˜๋Ÿ‰ ๊ฐ€์ ธ์˜ค๊ธฐ
 SELECT book_kind, COUNT(*) FROM book GROUP BY book_kind;

# ๋ฌธ์ œ) cartํ…Œ์ด๋ธ”์—์„œ ํšŒ์›๋ณ„๋กœ ๊ตฌ์ž…ํ•œ ์ด ์ˆ˜๋Ÿ‰ ๊ฐ€์ ธ์˜ค๊ธฐ
# ์กฐ๊ฑด) ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ธฐ
SELECT buyer, SUM(buy_count) FROM cart GROUP BY buyer 
ORDER BY SUM(buy_count) DESC;

#--------------------------------------------------------------

# GROUP BY์—์„œ WHERE ๋Œ€์‹  HAVING์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

# ๋ฌธ์ œ) cartํ…Œ์ด๋ธ”์—์„œ ํšŒ์›์ด ๊ตฌ์ž…ํ•œ ๋„์„œ์˜ ์ˆ˜๋Ÿ‰์ด 2๊ฐœ ์ด์ƒ์ธ ํšŒ์›์˜ id ๊ฐ€์ ธ์˜ค๊ธฐ
SELECT buyer, SUM(buy_count) FROM cart GROUP BY buyer;
SELECT buyer, SUM(buy_count) FROM cart GROUP BY buyer HAVING SUM(buy_count) >= 2;

# ๋ฌธ์ œ) bookํ…Œ์ด๋ธ”์—์„œ 2015๋…„์—์„œ 2019๋…„ ์‚ฌ์ด์— ์ถœ๊ฐ„ํ•œ ๋„์„œ ์ค‘์— ๊ฐœ์ˆ˜๊ฐ€ 3๊ฐœ ์ดํ•˜์ธ ๋„์„œ๋ฅผ ์ข…๋ฅ˜๋ณ„๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ
SELECT COUNT(*) FROM book 
WHERE '2015-01-01' <= book_publishing_date  
AND book_publishing_date < '2020-01-01';

SELECT book_kind, COUNT(*) FROM book 
WHERE '2015-01-01' <= book_publishing_date 
AND book_publishing_date < '2020-01-01'
GROUP BY book_kind;

SELECT book_kind, COUNT(*) FROM book 
WHERE '2015-01-01' <= book_publishing_date 
AND book_publishing_date < '2020-01-01' and book_count <=3
GROUP BY book_kind;
300x250

์ฝ”๋“œ