首页 > 数据库 > 数据库开发技术 > 《MySQL 入门教程》第 12 篇 分组统计
COUNT,返回查询结果的行数;
AVG,计算一组数值的平均值;
SUM,计算一组数值的总和;
MAX ,计算一组数据中的最大值;
MIN,计算一组数据中的最小值;
GROUP_CONCAT,连接一组字符串。
select count(*) "员工数量",
avg(salary) "平均月薪",
max(salary) "最高月薪",
min(salary) "最低月薪",
sum(salary) "月薪总和"
from employee;
员工数量|平均月薪 |最高月薪 |最低月薪 |月薪总和 |
-------|-----------|--------|-------|---------|
25|9912.000000|30000.00|4000.00|247800.00|
select group_concat(emp_name) "所有员工",
group_concat(emp_name order by salary separator ':') "所有员工"
from employee
where dept_id = 1;
所有员工 |所有员工 |
------------|------------|
刘备,关羽,张飞|张飞:关羽:刘备|
在聚合函数的参数中加上 DISTINCT 关键字,可以在计算之前排除重复值。例如,当 AVG 函数中包含 DISTINCT 参数时,在计算平均值之前会排除掉重复值。因此,(1、1、2)的平均值为 (1 + 2) / 2 = 1.5,而不是 (1 + 1 + 2) / 3 = 1.33。
聚合函数在计算时,忽略输入值为 NULL 的数据行;COUNT(*) 除外。例如,当 AVG 函数中存在空值时,计算之前会忽略这些空值。因此,(1,2,NULL)的平均值为 (1 + 2) / 2 = 1.5,而不是 (1 + 2) / 3 = 1。
select count(*),
count(distinct sex),
count(bonus)
from employee;
count(*)|count(distinct sex)|count(bonus)|
--------|-------------------|------------|
25| 2| 9|
aggregate_function( [ALL | DISTINCT] expression)
SELECT col1,
col2,
aggregate_function(expression)
FROM table_name
[WHERE conditions]
GROUP BY col1, col2;
select dept_id, count(*), sum(salary)
from employee
group by dept_id;
dept_id|count(*)|sum(salary)|
-------|--------|-----------|
1| 3| 80000.00|
2| 3| 41500.00|
3| 2| 18000.00|
4| 9| 68200.00|
5| 8| 40100.00|
select dept_id, sex, count(*)
from employee
group by dept_id, sex;
dept_id|sex |count(*)|
-------|----|--------|
1|男 | 3|
2|男 | 3|
3|女 | 2|
4|男 | 8|
4|女 | 1|
5|男 | 8|
select extract(year from hire_date) as "入职年份",
count(*) as "员工数量"
from employee
group by extract(year from hire_date);
入职年份|员工数量|
----|----|
2000| 3|
2006| 1|
2008| 1|
2007| 1|
2002| 2|
2005| 1|
2009| 1|
2011| 3|
2012| 2|
2010| 1|
2014| 1|
2017| 2|
2018| 5|
2019| 1|
select extract(year from hire_date) as "入职年份",
count(*) as "员工数量"
from employee
group by 1;
SELECT bonus, COUNT(*)
FROM employee
GROUP BY bonus;
bonus |COUNT(*)|
--------|--------|
10000.00| 3|
8000.00| 1|
[NULL]| 16|
5000.00| 2|
6000.00| 1|
2000.00| 1|
1500.00| 1|
-- GROUP BY 错误示例
select dept_id, emp_name, avg(salary)
from employee
group by dept_id;
ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'hrdb.employee.emp_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
select dept_id, any_value(emp_name), avg(salary)
from employee
group by dept_id;
dept_id|any_value(emp_name)|avg(salary) |
-------|-------------------|------------|
1|刘备 |26666.666667|
2|诸葛亮 |13833.333333|
3|孙尚香 | 9000.000000|
4|赵云 | 7577.777778|
5|法正 | 5012.500000|
select dept_id, count(*)
from employee
where count(*) > 5
group by dept_id;
ERROR 1111 (HY000): Invalid use of group function
select dept_id, count(*)
from employee
group by dept_id
having count(*) > 5;
dept_id|count(*)|
-------|--------|
4| 9|
5| 8|
select dept_id, count(*) cnt
from employee
where sex = '女'
group by dept_id
having cnt >= 2;
dept_id|cnt|
-------|---|
3| 2|
SELECT col1,
col2,
aggregate_function(expression)
FROM table_name
WHERE conditions
GROUP BY col1, col2
HAVING conditions
ORDER BY col1 [ASC | DESC], col2 [ASC | DESC], ...
LIMIT [off_set,] row_count;
CREATE TABLE sales (
item VARCHAR(10),
year VARCHAR(4),
quantity INT
);
INSERT INTO sales VALUES('apple', '2018', 800);
INSERT INTO sales VALUES('apple', '2018', 1000);
INSERT INTO sales VALUES('banana', '2018', 500);
INSERT INTO sales VALUES('banana', '2018', 600);
INSERT INTO sales VALUES('apple', '2019', 1200);
INSERT INTO sales VALUES('banana', '2019', 1800);
select item, year, sum(quantity)
from sales
group by item, year with rollup;
item |year|sum(quantity)|
------|----|-------------|
apple |2018| 1800|
apple |2019| 1200|
apple | | 3000|
banana|2018| 1100|
banana|2019| 1800|
banana| | 2900|
| | 5900|
GROUP BY col1, col2 WITH ROLLUP
GROUP BY col1, col2
GROUP BY col1
GROUP BY null
select item, year, sum(quantity), grouping(item), grouping(year), grouping(item, year)
from sales
group by item, year with rollup;
item |year|sum(quantity)|grouping(item)|grouping(year)|grouping(item, year)|
------|----|-------------|--------------|--------------|--------------------|
apple |2018| 1800| 0| 0| 0|
apple |2019| 1200| 0| 0| 0|
apple | | 3000| 0| 1| 1|
banana|2018| 1100| 0| 0| 0|
banana|2019| 1800| 0| 0| 0|
banana| | 2900| 0| 1| 1|
| | 5900| 1| 1| 3|
grouping(col1) * 4 + grouping(col2) * 2 + grouping(col3)
select if(grouping(item) = 1, '所有产品', item) as "产品",
if(grouping(year) = 1, '所有年度', item) as "年度",
sum(quantity) as "销量"
from sales
group by item, year with rollup;
产品 |年度 |销量 |
---------|---------|----|
apple |apple |1800|
apple |apple |1200|
apple |所有年度 |3000|
banana |banana |1100|
banana |banana |1800|
banana |所有年度 |2900|
所有产品 |所有年度 |5900|
原文作者:SQL编程思想;如有侵权,请联系管理员删除。