[갖고 노는 MySQL] 1-5. 조건에 따라 그룹으로 묶기

1. GROUP BY - 조건에 따라 집계된 값을 가져옵니다.

SELECT Country FROM Customers
GROUP BY Country;
SELECT CategoryID FROM Products
GROUP BY CategoryID;

💡 여러 컬럼을 기준으로 그룹화할 수도 있습니다.

SELECT 
  Country, City,
  CONCAT_WS(', ', City, Country)
FROM Customers
GROUP BY Country, City;

📚 그룹 함수 활용하기

SELECT
  COUNT(*), OrderDate
FROM Orders
GROUP BY OrderDate;
SELECT
  ProductID,
  SUM(Quantity) AS QuantitySum
FROM OrderDetails
GROUP BY ProductID
ORDER BY QuantitySum DESC;
SELECT
  CategoryID,
  MAX(Price) AS MaxPrice, 
  MIN(Price) AS MinPrice,
  TRUNCATE((MAX(Price) + MIN(Price)) / 2, 2) AS MedianPrice,
  TRUNCATE(AVG(Price), 2) AS AveragePrice
FROM Products
GROUP BY CategoryID;
SELECT 
  CONCAT_WS(', ', City, Country) AS Location,
  COUNT(CustomerID)
FROM Customers
GROUP BY Country, City;

💡 WITH ROLLUP - 전체의 집계값

SELECT
  Country, COUNT(*)
FROM Suppliers
GROUP BY Country
WITH ROLLUP;
  • 위의 각 집계함수 쿼리 끝에 WITH ROLLUP 을 추가해보세요.
  • ⚠️ ORDER BY 와는 함께 사용될 수 없습니다.

HAVING - 그룹화된 데이터 걸러내기

SELECT
  Country, COUNT(*) AS Count
FROM Suppliers
GROUP BY Country
HAVING Count >= 3;

💡 WHERE는 그룹하기 전 데이터, HAVING은 그룹 후 집계에 사용합니다.

SELECT
  COUNT(*) AS Count, OrderDate
FROM Orders
WHERE OrderDate > DATE('1996-12-31')
GROUP BY OrderDate
HAVING Count > 2;
SELECT
  CategoryID,
  MAX(Price) AS MaxPrice, 
  MIN(Price) AS MinPrice,
  TRUNCATE((MAX(Price) + MIN(Price)) / 2, 2) AS MedianPrice,
  TRUNCATE(AVG(Price), 2) AS AveragePrice
FROM Products
WHERE CategoryID > 2
GROUP BY CategoryID
HAVING
  AveragePrice BETWEEN 20 AND 30
  AND MedianPrice < 40;

2. DISTINCT - 중복된 값들을 제거합니다.

  • GROUP BY 와 달리 집계함수가 사용되지 않습니다.
  • GROUP BY 와 달리 정렬하지 않으므로 더 빠릅니다.
SELECT DISTINCT CategoryID
FROM Products;
-- 위의 GROUP BY를 사용한 쿼리와 결과 비교
SELECT COUNT DISTINCT CategoryID
FROM Products;
-- 오류 발생
SELECT DISTINCT Country
FROM Customers
ORDER BY Country;
SELECT DISTINCT Country, City
FROM Customers
ORDER BY Country, City;

💡 GROUP BYDISTINCT 함께 활용하기

SELECT
  Country,
  COUNT(DISTINCT CITY)
FROM Customers
GROUP BY Country;

좋은 웹페이지 즐겨찾기