--group by grouping sets的使用
/*可以用group by grouping sets来进行分组自定义汇总,可以用它来指定你需要的总数组合
其格式为 group by grouping sets ((list),(list)...) 这里的(list) 是圆括号中的一个列序列,
这个组合生成一个总数。要增加一个总和,必须增加一个(null)分组集。
*/
--查询scott.emp表,如果要查询:各部门sal大于2000的员工,进行汇总,得到各部门的sal总和、以及总共的sal总和。
select * from emp;
select case
when a.deptno is null then
'合计'
when a.deptno is not null and a.empno is null then
'小计'
else
''||a.deptno
end deptno,a.empno, a.ename,sum(a.sal) total_sal
from scott.emp a where a.sal > 2000 group by grouping sets((a.deptno),(a.deptno,a.empno,a.ename),());
--(大于2000分成deptno部门汇总)
如果能帮到你请感谢我的老婆“一行琉璃”
今天累了,改天详细讲讲