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));

 

 

你可能感兴趣的:(group by)