聚合函數對一組值執行計算,並返回單個值, 除了 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;