๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
DBMS/Mariadb

[DBMS]MariaDB์˜ GROUP BY ๊ตฌ๋ฌธ: ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ํšจ์œจ์ ์œผ๋กœ ๋ถ„์„ํ•˜๊ธฐ

by YJ Dev 2024. 6. 3.
728x90
๋ฐ˜์‘ํ˜•
SMALL

GROUP BY ๊ตฌ๋ฌธ์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ํŠน์ • ๊ธฐ์ค€์— ๋”ฐ๋ผ ์ง‘๊ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” ๋งˆ๋ฆฌ์•„ DB์˜ GROUP BY ๊ตฌ๋ฌธ์„ ์†Œ๊ฐœํ•˜๊ณ , ๊ทธ ํ™œ์šฉ๋ฒ•๊ณผ ์ค‘์š”์„ฑ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

MARIADB GROUP BY

GROUP BY ๊ตฌ๋ฌธ์˜ ๊ธฐ๋ณธ ๊ฐœ๋…๐Ÿ“˜

GROUP BY ๊ตฌ๋ฌธ์€ ๋ฐ์ดํ„ฐ๋ฅผ ํŠน์ • ์—ด ๋˜๋Š” ์—ด์˜ ์กฐํ•ฉ์— ๋”ฐ๋ผ ๊ทธ๋ฃนํ™”ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์„ธ๋ถ„ํ™”ํ•˜๊ณ  ์ง‘๊ณ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ฃผ๋ฌธ ๋ฐ์ดํ„ฐ์—์„œ ์ง€์—ญ๋ณ„ ํŒ๋งค๋Ÿ‰์„ ๊ตฌํ•˜๊ฑฐ๋‚˜, ๊ณ ๊ฐ๋ณ„๋กœ ๊ตฌ๋งคํ•œ ์ƒํ’ˆ ์ˆ˜๋ฅผ ํŒŒ์•…ํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

GROUP BY ๊ตฌ๋ฌธ์˜ ํ™œ์šฉ๐Ÿ’ก

๊ธฐ๋ณธ์ ์ธ GROUP BY ๊ตฌ๋ฌธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑ๋ฉ๋‹ˆ๋‹ค.

SELECT column_name, aggregate_function(column_name)
FROM table_name
GROUP BY column_name;

์—ฌ๊ธฐ์„œ column_name์€ ๊ทธ๋ฃนํ™”ํ•  ์—ด์„, aggregate_function์€ ๊ทธ๋ฃนํ™”๋œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์ ์šฉํ•  ์ง‘๊ณ„ ํ•จ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜๊ณ  ์ง‘๊ณ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

GROUP BY์™€ ์ง‘๊ณ„ ํ•จ์ˆ˜๐Ÿ“ˆ

GROUP BY์™€ ์ง‘๊ณ„ ํ•จ์ˆ˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋“ค์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜๊ณ  ํ•„์š”ํ•œ ํ†ต๊ณ„๋Ÿ‰์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์š”ํ•œ ์ง‘๊ณ„ ํ•จ์ˆ˜์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒƒ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค.

    1. COUNT: ๊ทธ๋ฃน ๋‚ด์˜ ํ–‰ ์ˆ˜๋ฅผ ์„ธ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ๋กœ ํŠน์ • ์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
    2. SUM: ๊ทธ๋ฃน ๋‚ด์˜ ์—ด์˜ ํ•ฉ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋งค์ถœ ๋ฐ์ดํ„ฐ์—์„œ ๋งค์ถœ์•ก์˜ ์ด ํ•ฉ์„ ๊ตฌํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
    3. AVG: ๊ทธ๋ฃน ๋‚ด์˜ ์—ด์˜ ํ‰๊ท ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ํ‰๊ท  ์ฃผ๋ฌธ ๊ฐ€๊ฒฉ์ด๋‚˜ ํ‰๊ท  ๊ณ ๊ฐ ๋งŒ์กฑ๋„ ๋“ฑ์„ ๊ณ„์‚ฐํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
    4. MIN/MAX: ๊ทธ๋ฃน ๋‚ด์˜ ์—ด์—์„œ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’ ๋˜๋Š” ๊ฐ€์žฅ ํฐ ๊ฐ’์ด ๋ฌด์—‡์ธ์ง€๋ฅผ ์ฐพ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ตœ์†Œ ์ฃผ๋ฌธ ๊ธˆ์•ก์ด๋‚˜ ์ตœ๊ณ  ๋งค์ถœ์•ก ๋“ฑ์„ ํŒŒ์•…ํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

์‚ฌ์šฉ ์˜ˆ์‹œ

์•„๋ž˜๋Š” ๊ฐ„๋‹จํ•œ GROUP BY์™€ ์ง‘๊ณ„ ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

-- ์ฃผ๋ฌธ ๋ฐ์ดํ„ฐ์—์„œ ์ง€์—ญ๋ณ„ ์ฃผ๋ฌธ ์ˆ˜ ์„ธ๊ธฐ
SELECT `region`, COUNT(*) AS `order_count`
FROM `orders`
GROUP BY `region`;

-- ์ œํ’ˆ ๋ฐ์ดํ„ฐ์—์„œ ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ํ‰๊ท  ๊ฐ€๊ฒฉ ๊ณ„์‚ฐ
SELECT `category`, AVG(`price`) AS `avg_price`
FROM `products`
GROUP BY `category`;

GROUP BY์˜ ํ™œ์šฉ ์‚ฌ๋ก€๐Ÿ’ผ

์˜ˆ์ œ ๋ฐ์ดํ„ฐ

sales ํ…Œ์ด๋ธ”

region sales_amount
East 1500
West 2200
North 1800
South 2000
East 1200
West 2500
North 1600
South 1900


products ํ…Œ์ด๋ธ”

category price
Electronics 1000
Electronics 1200
Clothing 50
Clothing 80
Books 20
Books 25


orders ํ…Œ์ด๋ธ”

order_date total_amount
2024-01-05 300
2024-01-10 500
2024-02-15 700
2024-02-20 450
2024-03-03 600
2024-03-08 350


customers ํ…Œ์ด๋ธ”

customer_id age
1 28
2 35
3 19
4 42
5 50
6 22

์ฟผ๋ฆฌ ๋ฐ ๊ฒฐ๊ณผ

1. ์ง€์—ญ๋ณ„ ํŒ๋งค๋Ÿ‰ ๋ถ„์„

SELECT `region`, SUM(`sales_amount`) AS `total_sales`
FROM `sales`
GROUP BY `region`;
region total_sales
East 2700
West 4700
North 3400
South 3900


2. ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ํ‰๊ท  ๊ฐ€๊ฒฉ ๋ถ„์„

SELECT `category`, AVG(`price`) AS `avg_price`
FROM `products`
GROUP BY `category`;
category avg_price
Electronics 1100
Clothing 65
Books 22.5


3. ์›”๋ณ„ ์ฃผ๋ฌธ๋Ÿ‰ ๋ถ„์„

SELECT MONTH(`order_date`) AS `month`, COUNT(*) AS `total_orders`
FROM `orders`
GROUP BY MONTH(`order_date`);
month total_orders
1 2
2 2
3 2


4. ๊ณ ๊ฐ ์„ธ๋ถ„ํ™”

SELECT CASE 
            WHEN `age` <= 25 THEN 'Youth'
            WHEN `age` BETWEEN 26 AND 40 THEN 'Adult'
            ELSE 'Senior'
        END AS `customer_segment`,
        COUNT(*) AS `total_customers`
FROM `customers`
GROUP BY `customer_segment`;
customer_segment total_customers
Youth 2
Adult 3
Senior 1

์ฃผ์˜ํ•  ์ ๊ณผ ์ตœ์ ํ™” ํŒโš ๏ธ

์ฃผ์˜ํ•  ์ 

  1. ์ธ๋ฑ์‹ฑ: GROUP BY๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์—ด์— ์ ์ ˆํ•œ ์ธ๋ฑ์Šค๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ๊ทธ๋ฃนํ™”๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ์–ด ์„ฑ๋Šฅ์„ ํ–ฅ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  2. GROUP BY์˜ ์ˆœ์„œ: GROUP BY ๊ตฌ๋ฌธ์€ ์ผ๋ฐ˜์ ์œผ๋กœ WHERE ์ ˆ ๋‹ค์Œ์— ์˜ค๋ฉฐ, ORDER BY ์ ˆ ์ด์ „์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ˆœ์„œ๋ฅผ ์ง€ํ‚ค์ง€ ์•Š์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  3. ์ง‘๊ณ„ ํ•จ์ˆ˜ ์‚ฌ์šฉ: GROUP BY์™€ ํ•จ๊ป˜ ์ง‘๊ณ„ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ง‘๊ณ„ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์—ด์ด๋‚˜ ๊ทธ๋ฃนํ™”ํ•  ์—ด์„ ๋ช…ํ™•ํ•˜๊ฒŒ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  4. NULL ๊ฐ’ ์ฒ˜๋ฆฌ: GROUP BY๋Š” NULL ๊ฐ’์„ ๊ทธ๋ฃนํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ NULL ๊ฐ’์„ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ์ง€์— ๋Œ€ํ•œ ์ „๋žต์„ ์„ธ์›Œ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ตœ์ ํ™” ํŒ

  1. ๋ฐ์ดํ„ฐ ์–‘ ์ œํ•œ: ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ GROUP BY๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ๋ฐ์ดํ„ฐ ์–‘์„ ์ œํ•œํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. WHERE ์ ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋จผ์ € ํ•„ํ„ฐ๋งํ•˜๊ณ  ๊ทธ ๋‹ค์Œ์— GROUP BY๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.
  2. ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ ํ™œ์šฉ: ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‹œ์Šคํ…œ์ด ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๋ฅผ ์ง€์›ํ•˜๋Š” ๊ฒฝ์šฐ, GROUP BY ์—ฐ์‚ฐ์„ ๋ณ‘๋ ฌ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ฟผ๋ฆฌ์˜ ์ฒ˜๋ฆฌ ์†๋„๋ฅผ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  3. ์ ์ ˆํ•œ ๋ฐ์ดํ„ฐ ์œ ํ˜• ์‚ฌ์šฉ: ๊ทธ๋ฃนํ™”ํ•  ์—ด์˜ ๋ฐ์ดํ„ฐ ์œ ํ˜•์„ ์ ์ ˆํ•˜๊ฒŒ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ๋ฌธ์ž์—ด๋ณด๋‹ค๋Š” ์ˆซ์žํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋ฃนํ™”ํ•˜๋Š” ๊ฒƒ์ด ์„ฑ๋Šฅ์— ๋” ์œ ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  4. ํ•˜์œ„ ์ฟผ๋ฆฌ ์‚ฌ์šฉ ์ตœ์†Œํ™”: GROUP BY๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ํ•˜์œ„ ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๊ฐ€๋Šฅํ•œ ํ•œ ํ•˜๋‚˜์˜ ์ฟผ๋ฆฌ๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์„ฑ๋Šฅ์— ๋” ์œ ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

ํ•ต์‹ฌ ๋‚ด์šฉ๐Ÿ‘€

MARIADB GROUP BY

728x90
๋ฐ˜์‘ํ˜•