rollup与cube函数
1、为什么oracle 9i中使用rollup得到的结果中总计那个值在最开始的一条记录,cube得到的结果中总计那个值在最后一条记录?这个有什么规律么?
2、另外,我用group by与grouping sets得到的结果好像一样啊?
谢谢! 展开
ROLLUP和CUBE 用法
Oracle的GROUP BY语句除了最基本的语法外,还支持ROLLUP和CUBE语句。如果是Group by ROLLUP(A, B, C)的话,首先会对(A、B、C)进行GROUP BY,然后对(A、B)进行GROUP BY,然后是(A)进行GROUP BY,最后对全表进行GROUP BY操作。
如果是GROUP BY CUBE(A, B, C),则首先会对(A、B、C)进行GROUP BY,然后依次是(A、B),(A、C),(A),(B、C),(B),(C),最后对全表进行GROUP BY操作。 grouping_id()可以美化效果。除了使用GROUPING函数,还可以使用GROUPING_ID来标识GROUP BY的结果。
也可以 Group by Rollup(A,(B,C)) ,Group by A Rollup(B,C),…… 这样任意按自己想要的形式结合统计数据,非常方便。
Rollup():分组函数可以理解为group by的精简模式,具体分组模式如下:
Rollup(a,b,c): (a,b,c),(a,b),(a),(全表)
Cube():分组函数也是以group by为基础,具体分组模式如下:
cube(a,b,c):(a,b,c),(a,b),(a,c),(b,c),(a),(b),(c),(全表)
下面准备数据比较一下两个函数的不同:
1、准备数据:
2、使用rollup函数查询
select group_id,job,name,sum(salary) from GROUP_TEST group by rollup(group_id,job,name);
3、使用cube函数:
select group_id,job,name,sum(salary) from GROUP_TEST group by cube(group_id,job,name)
4、对比:从最后查询出来的数据条数就差了好多,下面看一下将两个函数从转化成对应的group函数语句:
rollup函数:
select group_id,job,name,sum(salary) from GROUP_TEST group by rollup(group_id,job,name);
等价于:
select group_id,job,name,sum(salary) from GROUP_TEST group by group_id,job,name
union all
select group_id,job,null,sum(salary) from GROUP_TEST group by group_id,job
union all
select group_id,null,null,sum(salary) from GROUP_TEST group by group_id
union all
select null,null,null,sum(salary) from GROUP_TEST
cube函数:
select group_id,job,name,sum(salary) from GROUP_TEST group by cube(group_id,job,name) ;
等价于:
select group_id,job,name,sum(salary) from GROUP_TEST group by group_id,job,name
union all
select group_id,job,null,sum(salary) from GROUP_TEST group by group_id,job
union all
select group_id,null,name,sum(salary) from GROUP_TEST group by group_id,name
union all
select group_id,null,null,sum(salary) from GROUP_TEST group by group_id
union all
select null,job,name,sum(salary) from GROUP_TEST group by job,name
union all
select null,job,null,sum(salary) from GROUP_TEST group by job
union all
select null,null,name,sum(salary) from GROUP_TEST group by name
union all
select null,null,null,sum(salary) from GROUP_TEST
5、由此可见两个函数对于汇总统计来说要比普通函数好用的多,另外还有一个配套使用的函数
grouping(**):当**字段为null的时候值为1,当字段**非null的时候值为0;
select grouping(group_id),job,name,sum(salary) from GROUP_TEST group by rollup(group_id,job,name);
6、添加一列用来直观的显示所有的汇总字段:
select group_id,job,name,
case when name is null and nvl(group_id,0)=0 and job is null then '全表聚合'
when name is null and nvl(group_id,0)=0 and job is not null then 'JOB聚合'
when name is null and grouping(group_id)=0 and job is null then 'GROUPID聚合'
when name is not null and nvl(group_id,0)=0 and job is null then 'Name聚合'
when name is not null and grouping(group_id)=0 and job is null then 'GROPName聚合'
when name is not null and grouping(group_id)=1 and job is not null then 'JOBName聚合'
when name is null and grouping(group_id)=0 and job is not null then 'GROUPJOB聚合'
else
'三列汇总' end ,
sum(salary) from GROUP_TEST group by cube(group_id,job,name) ;
2 用GROUP BY GROUPING SETS来代替GROUP BY CUBE。你可以应用来指定你感兴趣的总数组合。因为它不必计算它不需要集合(也不会产生太多结果),所以对SQL引擎来说更为高效。
其格式为:
GROUP BY GROUPING SETS ((list), (list) ... )
这里(list)是圆括号中的一个列序列,这个组合生成一个总数。要增加一个总和,必须增加一个(NUlL)分组集。