zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

oracle数据库GROUP BY 子句

Oracle数据库 by group 子句
2023-09-11 14:14:08 时间

1.GROUP BY子句

在SELECT 列表中所有未包含在组函数中的列都应该包含在GROUP BY 子句中.

如下:

SELECT deptno,AVG(sal) from emp GROUP BY deptno;(deptno为没有包含在组函数的列)

以下查询是错误的:

SELECT a,b,c,AVG(sal) from emp GROUP BY a,b;(c没有写在group by 后面)

===========================================================================================

多个列的分组:先按照第一个列分组,如果相同,再第二个列分组,以此类推

===========================================================================================

2.过滤分组:HAVING 子句

使用HAVING过滤分组

1.行已经被分组;

2.使用了组函数;

如:

select deptno,avg(sal) from emp group by deptno having avg(sal)>2000;

==========================================================================================

where 后面不能使用多行函数;

==========================================================================================

GROUP BY语句的增强(主要作用:用来做报表)

select deptno,job,sum(sal)  from emp group by deptno,job
+

select deptno,sum(sal) from emp group by deptno

+

selelct sum(sal) from emp

====

select deptno,job,sum(sal) from emp group by rollup(deptno,job)

 

 查询出来类似于报表形式

===========================================================================================

抽象:

group by rollup(a,b)

=

group by a,b

+

group by a

+

没有group by