SQL -- GROUP BY 基本语法
SELECT column1, column2, aggregate_function(column3)
FROM table_name
WHERE condition
GROUP BY column1, column2
ORDER BY column1, column2;
主要用途
GROUP BY用于将结果集按照一个或多个列进行分组,通常与聚合函数(如COUNT、SUM、AVG、MAX、MIN)一起使用。
常用聚合函数
- COUNT() - 计算行数
- SUM() - 求和
- AVG() - 平均值
- MAX() - 最大值
- MIN() - 最小值
- GROUP_CONCAT() - 连接字符串(MySQL)
实际示例
1. 基本分组统计
-- 按部门统计员工数量
SELECT department, COUNT(*) as employee_count
FROM employees
GROUP BY department;
2. 多列分组
-- 按部门和职位统计
SELECT department, position, COUNT(*) as count
FROM employees
GROUP BY department, position;
3. 使用聚合函数
-- 按部门统计平均工资
SELECT department, COUNT(*) as employee_count,AVG(salary) as avg_salary,MAX(salary) as max_salary,MIN(salary) as min_salary
FROM employees
GROUP BY department;
4. 带WHERE条件的分组
-- 统计2023年各月份的销售总额
SELECT MONTH(order_date) as month,SUM(amount) as total_sales
FROM orders
WHERE YEAR(order_date) = 2023
GROUP BY MONTH(order_date)
ORDER BY month;
5. 使用HAVING过滤分组结果
-- 找出平均工资超过50000的部门
SELECT department, AVG(salary) as avg_salary
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000;
6. 复杂分组示例
-- 按年份和季度统计销售数据
SELECT YEAR(order_date) as year,QUARTER(order_date) as quarter,COUNT(*) as order_count,SUM(amount) as total_amount,AVG(amount) as avg_amount
FROM orders
WHERE order_date >= '2020-01-01'
GROUP BY YEAR(order_date), QUARTER(order_date)
ORDER BY year, quarter;
重要注意事项
1. SELECT子句规则
-- ✅ 正确:SELECT中的非聚合列必须在GROUP BY中
SELECT department, COUNT(*)
FROM employees
GROUP BY department;-- ❌ 错误:name不在GROUP BY中
SELECT department, name, COUNT(*)
FROM employees
GROUP BY department;
2. WHERE vs HAVING (?)
-- WHERE:过滤行(在分组前)
SELECT department, COUNT(*)
FROM employees
WHERE salary > 30000
GROUP BY department;-- HAVING:过滤分组(在分组后)
SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
3. 分组排序
-- 按分组结果排序
SELECT department, COUNT(*) as emp_count
FROM employees
GROUP BY department
ORDER BY emp_count DESC;
实际应用场景
1. 数据分析
-- 用户行为分析:按日期统计访问量
SELECT DATE(visit_time) as date,COUNT(DISTINCT user_id) as unique_visitors,COUNT(*) as total_visits
FROM user_visits
GROUP BY DATE(visit_time)
ORDER BY date;
2. 报表生成
-- 销售报表:按产品类别统计
SELECT category,COUNT(*) as product_count,SUM(price * quantity) as total_revenue
FROM sales
GROUP BY category
ORDER BY total_revenue DESC;
3. 数据清理
-- 查找重复数据
SELECT email, COUNT(*) as count
FROM users
GROUP BY email
HAVING COUNT(*) > 1;
GROUP BY是SQL中非常重要的功能,特别适用于数据分析和报表生成。记住要正确使用WHERE和HAVING,以及确保SELECT子句中的非聚合列都在GROUP BY中。
