Oracle 聚集函数

(1)avg(x):返回x的平均值

select avg(grade) from sc;

  (2)count(x):返回统计的行数

select count(name) from sc;

  (3)max(x):返回x的最大值

select max(grade) from sc;

  (4)min(x):返回x的最小值

select min(grade) from sc;

  (5)sum(x):返回x的总计值

select sum(grade) from sc;

2.对分组行使用聚集函数

  对分组后的行使用聚集函数,聚集函数会统计每组中的值,对于每组分别统计后返回一个值。

  示例

--按照职位分组,求出每个职位的最高和最低工资
select job ,max(sal),min(sal) from emp 
group by job 
order by job;

  注意:1.分组时select子句后边的列名必须与group by子句后的列名一致,除非是聚合函数

select deptno,avg(sal) from EMP;--错误,因为deptno不是聚集函数,也不是group by后面跟的列名

       2.不能使用聚集函数作为WHERE子句的筛选条件

select deptno from emp where avg(sal)>1000;--错误

     3.分组后,需要使用条件进行筛选,则使用having过滤分组后的行,不能使用where,where只能放在group by前面。

select deptno, avg(sal) from emp where deptno<>10 
group by deptno  
having avg(sal) > 900;
原文地址:https://www.cnblogs.com/cb1186512739/p/9889555.html