Enhancements to the Group By clause:

The ROLLUP operator creates subtotals that roll up from the most detailed level to a grand total, following
the grouping list specified in the GROUP BY clause
The Rollup operation to produce cumulative aggregates such as subtotals:
SQL> select deptno,job,sum(sal) from emp
2 where deptno<40
3 group by deptno,job;

DEPTNO JOB SUM(SAL)
---------- --------- ----------
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
20 CLERK 1900
20 ANALYST 6000
20 MANAGER 5385
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600

已选择9行。


SQL> select deptno,job,sum(sal) from emp
2 where deptno<40
3 group by rollup(deptno,job);

DEPTNO JOB SUM(SAL)
---------- --------- ----------
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
10 8750
20 CLERK 1900
20 ANALYST 6000
20 MANAGER 5385
20 13285
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600

DEPTNO JOB SUM(SAL)
---------- --------- ----------
30 9400
31435

已选择13行。
Then it creates progressively higher-level subtotals, moving from right to left through the
list of grouping columns. (In the preceding example, the sum of salaries for each department is calculated,
followed by the sum of salaries for all departments.):

SQL> select deptno,job,sum(sal) from emp
2 where deptno<40
3 group by rollup(job,deptno);

DEPTNO JOB SUM(SAL)
---------- --------- ----------
10 CLERK 1300
20 CLERK 1900
30 CLERK 950
CLERK 4150
20 ANALYST 6000
ANALYST 6000
10 MANAGER 2450
20 MANAGER 5385
30 MANAGER 2850
MANAGER 10685
30 SALESMAN 5600

DEPTNO JOB SUM(SAL)
---------- --------- ----------
SALESMAN 5600
10 PRESIDENT 5000
PRESIDENT 5000
31435

已选择15行。


While ROLLUP produces only a
fraction of possible subtotal combinations, CUBE produces subtotals for all possible combinations of
groupings specified in the GROUP BY clause, and a grand total.

SQL> select deptno,job,sum(sal) from emp
2 where deptno<40
3 group by cube(deptno,job);

DEPTNO JOB SUM(SAL)
---------- --------- ----------
31435
CLERK 4150
ANALYST 6000
MANAGER 10685
SALESMAN 5600
PRESIDENT 5000
10 8750
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
20 13285

DEPTNO JOB SUM(SAL)
---------- --------- ----------
20 CLERK 1900
20 ANALYST 6000
20 MANAGER 5385
30 9400
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600

已选择18行。
可以明显看到cube首先按一个部门分组,求针对每个job的sum(sal);其次按deptno分组求那些部门的sum(sal);再次按每一种job来求total

sum(sal);最后按所有部门求total sum(sal)。
(CLERK 4150)this rows is cross-tabulation-rows.

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8382469/viewspace-259840/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8382469/viewspace-259840/

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值