1、语法
在select列表中所有未包含在组函数中的列都应该包含在group by字句中
包含在group by字句中的列不必包含在select列表中
- 正确:
1 | select deptno avy(sal) from emp group by deptno;(每个部门的平均工资) |
- 错误:
1 | select deptno, count (ename) from emp; |
2、多个列分组
1 2 3 4 | select deptno,job sum (sal) from emp group by deptno,job order by 1; |
先按照第一个列分组,如果相同,再按第二个分组,以此类推
3、过滤分组数据
where和having的区别
where后面不能使用多行行数
当既可以使用where和having的时候,尽量使用where
4、group by语句增强
举例说明
1 2 3 4 | (1) select deptno,job sum (sal) from emp group by deptno,job; (2) select deptno sum (sal) from emp group by deptno; (3) select sum (sal) from emp; (4) select deptno,job sum (sal) from emp group by rollup ( deptno,job); |
(1)+(2)+(3)==(4)
rollup()函数
1 2 3 4 5 6 7 8 9 10 11 12 13 | group by rollup (a,b) == group by a,b + group by a + 没有 group by |
- 作用:做报表
- 设置格式:break on deptno skip 2
- 取消格式:break on null
select语句可以做加减运算,通过集合运算
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持IT俱乐部。