1.常见的几个聚合函数
#1.1 AVG/SUM :只适用于数值类型的字段(或变量)
SELECT AVG(salary),SUM(salary),AVG(salary)*107
FROM employees;
#如下的操作没有意义
SELECT SUM(last_name),AVG(last_name),SUM(hire_date)
FROM employees;
#1.2 MAX/MIN:适用于数值类型、字符串类型、日期时间类型的字段(或变量)
SELECT MAX(salary),MIN(salary)
FROM employees;
SELECT MAX(last_name),MIN(last_name),MAX(hire_date),MIN(hire_date)
FROM employees;
#1.3 COUNT:
#①作用:计算指定字段在查询结果中出现的个数(不包含null值)
SELECT COUNT(employee_id),COUNT(salary),COUNT(2*107),COUNT(1),COUNT(2),COUNT(*)
FROM employees;
SELECT*
FROM employees;
#如果计算表中有多少条记录,如何实现?
#方式1:COUNT(*)
#方式2:COUNT(1)
#方式3: COUNT(具体字段):不一定对!
#②注意:计算指定字段出现的个数时,是不计算null值的
SELECT COUNT(commission_pct)#只有不是null的数据被算进去了
FROM employees;
#③ 公式:AVG=SUM/COUNT
SELECT AVG(salary),SUM(salary)/COUNT(salary),
AVG(commission_pct),SUM(commission_pct)/COUNT(commission_pct),
SUM(commission_pct)/107
FROM employees;
#需求:查询公司中平均奖金率
#错误的!没有奖金的人,他没有算进去,只除了有奖金的人数
SELECT AVG(commission_pct)
FROM employees;
#正确的
SELECT SUM(commission_pct)/COUNT(IFNULL(commission_pct,0)),
AVG(IFNULL(commission_pct,0))
FROM employees;