oracle group by

 

扩展的group by的函数还是要符合group by的相关语法语义规则,比如select中不能直接显示非分组列

 

 

rollup

oracle使用rollup对group by进行扩展,他允许计算标准分组及相应维度的小计合计

 

select  group by rollup(grouping_column_reference_list)

 

先计算标准分组,然后列从右到左递减计算更高一级的小计,一直到列全部被选完,最后计算合计

 

 

 

select  a.dname,b.job,sum(b.sal)

from dept a,emp b 

where a.deptno=b.deptno

group by a.dname,b.job

 

union all

 

select a.dname,null,sum(b.sal) 

from dept a,emp b

where a.deptno=b.deptno

group by a.dname

 

union all

select null,null,sum(b.sal)

from dept a,emp b

where a.deptno=b.deptno;

-------------------------------------

 

 

select a.dname,b.job,sum(b.sal) sum_sal

from dept a,emp b

where a.deptno=b.deptno

group by rollup(a.dname,b.job)

 

 

 

select to_char(b.hiredate,'yyyy') hire_year,a.dname,b.job,sum(b.sal) sum_sal

from dept a,emp b

where a.deptno=b.deptno

group by rollup(to_char(b.hiredate,'yyyy'),a.dname,b.job);

 

 

部分rollup分组

 

 

 

select to_char(b.hiredate,'yyyy') hire_year,a.dname,b.job,sum(b.sal) sum_sal

from dept a,emp b

where a.deptno=b.deptno

group by to_char(b.hiredate,'yyyy'),a.dname, rollup(b.job);

 

rollup中指定n列,分组方式就有n+1种,如果部分rollup可以剔除某些不需要的小计和合计,只要根据需求将相关的列从rollup中移出,放到group by 中即可

 

 

CUBE

可以对不同维度的所有可能分组进行统计,从而生成交叉报表

 

select ----  group by cube(grouping_column_reference_list)

 

select b.job,a.dname,sum(b.sal)

from dept a,emp b

where a.deptno=b.deptno

group by cube(a.dname,b.job)

 

部分cube

 

select a.dname,b.job,sum(b.sal)

from dept a,emp b

where a.deptno=b.deptno

group by a.dname,cube(b.job);

 

cube实现了多维组的分析统计工作

 

grouping sets

只有指定某些维度的小计,没有常规分组结果及合计结果,只对关注某些维度的小计分析很有用

select .... group by grouping sets(grouping_column_reference_list)

 

 

select to_char(b.hiredate,'yyyy') hie_year,a.dname,b.job,sum(b.sal) sum_sal

from dept a,emp b

where a.deptno=b.deptno

group by grouping sets(to_char(b.hiredate,'yyyy'),a.dname,b.job);

 

部分grouping sets

 

select to_char(b.hiredate,'yyyy') hie_year,a.dname,b.job,sum(b.sal) sum_sal

from dept a,emp b

where a.deptno=b.deptno

group by a.dname, grouping sets(to_char(b.hiredate,'yyyy'),b.job);

 

cube ,rollup 作为grouping sets的参数

grouping sets 只对单列进行分组,而不提供合计的功能,如果需要提供合计的功能,那么就可以使用rollup或者cube作为grouping sets的参数

 

select a.dname,b.job,sum(b.sal)

from dept a,emp b

where a.deptno=b.deptno

group by grouping sets(rollup( a.dname),rollup(b.job));

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值