Chinaunix首页 | 论坛 | 博客
  • 博客访问: 364293
  • 博文数量: 80
  • 博客积分: 2056
  • 博客等级: 大尉
  • 技术积分: 961
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-19 12:17
文章分类

全部博文(80)

文章存档

2011年(9)

2010年(13)

2009年(41)

2008年(17)

我的朋友

分类: Oracle

2010-12-27 17:32:46

Group by Rollup(a,b,c,d)
的结果集为,共n+1个集
Group by a,b,c,d
Union all
Group by a,b,c
Union all
Group by a,b
Union all
Group by a
Union all
Group by null
select deptno,job,sum(sal) from emp group by rollup(deptno,job);
DEPTNO JOB SUM(SAL)
------ --------- ----------
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
10 8750
20 CLERK 800
20 ANALYST 3000
20 MANAGER 2975
20 6775
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600
30 9400
24925
结果为
select deptno,job,sum(sal) from emp group by deptno,job
union all
select deptno,null,sum(sal) from emp group by deptno
union all
select null,null,sum(sal) from emp;
Grouping(列名称)的使用,为了表达该列是否参加了分组活动。
0为该列参加了分组,1为该列未参加分组操作
select deptno,job,grouping(deptno),grouping(job),sum(sal)
from emp group by rollup(deptno,job);
DEPTNO JOB GROUPING(DEPTNO) GROUPING(JOB) SUM(SAL)
------ --------- ---------------- ------------- ----------
10 CLERK 0 0 1300
10 MANAGER 0 0 2450
10 PRESIDENT 0 0 5000
10 0 1 8750
20 CLERK 0 0 800
20 ANALYST 0 0 3000
20 MANAGER 0 0 2975
20 0 1 6775
30 CLERK 0 0 950
30 MANAGER 0 0 2850
30 SALESMAN 0 0 5600
30 0 1 9400
1 1 24925
 Cube分组
select deptno,job,grouping(deptno),
grouping(job) ,sum(sal) from emp group by cube(deptno,job);
结果集为,2**n个结果集
select deptno,job,sum(sal) from emp group by deptno,job
union all
select deptno,null,sum(sal) from emp group by deptno
union all
select null,job,sum(sal) from emp group by job
union all
select null,null,sum(sal) from emp;
阅读(1919) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-01-01 19:53:01

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com