聚合函数对一组值执行计算,并返回单个值, 除了 COUNT(*)
外,聚合函数都会忽略 Null 值,聚合函数经常与 SELECT 语句的 GROUP BY、PARTITION BY 子句一起使用。
1、 AVG() 此函数返回组中各值的平均值,将忽略 null 值
2、MIN() 在表达式中返回最小值
3、MAX() 在表达式中返回最大值
4、COUNT() 此函数返回组中找到的项数量
5、SUM() 返回表达式中所有值的和,只能用于数字列,Null 值会被忽略
select count(t.employee_id) as 员工总人数,max(t.salary) 员工最高工资,min(t.salary) 员工最低工资,avg(t.salary) 员工平均工资,sum(t.salary) 员工总工资 from hr.employees t
select t2.department_name, count(t.employee_id) over (partition by t.department_id) as 部门总人数,max(t.salary) over (partition by t.department_id) 部门最高工资,min(t.salary) over (partition by t.department_id) 部门最低工资,avg(t.salary) over (partition by t.department_id) 部门平均工资,sum(t.salary) over (partition by t.department_id) 部门总工资 from hr.employees t,hr.departments t2 where t.department_id=t2.department_id;