Oracle之Group by 扩展分组函数
Oracle之group by 扩展分组函数
测试学习过程如下:-
--rollup:以每个部门为参考对象,每个部门下员工薪水的明细和汇总
select t.dept_id,t.dept_name,sum(t.salary)
from qcfang.test1 t
group by rollup(t.dept_id,t.dept_name)
--cube:信息量最大的交叉报表,汇总,明细均有
select t.dept_id,t.dept_name,sum(t.salary)
from qcfang.test1 t
group by cube(t.dept_id,t.dept_name)
--rollup 取消最后的总计,其实是过滤掉了dept_id 为null的记录
select t.dept_id,t.dept_name,sum(t.salary)
from qcfang.test1 t
group by t.dept_id, rollup(t.dept_name)
order by t.dept_id
--cube取消总计,其实是过滤掉了dept_id为null的记录
select t.dept_id,t.dept_name,sum(t.salary)
from qcfang.test1 t
group by t.dept_id,cube(t.dept_name)
--grouping settings:每个维度不同值的汇总,相当于多个union all
select t.dept_id,t.dept_name,sum(t.salary)
from qcfang.test1 t
group by grouping sets(t.dept_id,t.dept_name)
--利用grouping 函数,找出哪些是合计:flag=1为合计.
select t.dept_id,t.dept_name,sum(t.salary),grouping(t.dept_name)flag
from qcfang.test1 t
group by rollup(t.dept_id,t.dept_name)
order by t.dept_id
--利用grouping函数过滤分组
select t.dept_id,t.dept_name,sum(t.salary),grouping(t.dept_name)flag
from qcfang.test1 t
group by rollup(t.dept_id,t.dept_name)
having grouping(t.dept_name) =0
--利用grouping_id 进行排序
select t.dept_id,t.dept_name,sum(t.salary),grouping(t.dept_name)flag
from qcfang.test1 t
group by rollup(t.dept_id,t.dept_name)
order by grouping_id(t.dept_id,t.dept_name),t.dept_id
推荐阅读: