rollup是对group by的扩展,会进行小计和合计,而cube包含rollup,是粒度更精细的小计和合计。当只有一个字段时,rollup和cube是一样的。
可用grouping对rollup和cube进行测试:0看;1不看[看表示列有显示,不看表示没有显示]
① rollup
㈠ 什么都不看
㈡ 从左往右 ┌ 先看第1个
│ 再看前2个
└ 后看前3个
② cube:0、1全排列
rollup
rollup后面指定的列以逗号分隔,rollup的计算结果和其后面指定的列的顺序有关,因为rollup的分组具有方向性。若指定n列,则有n+1种分组方式。可改变列的顺序,达到不同的业务需求。一定要牢记列的顺序对结果的影响!
例子:
20:32:51 scott@ORCL (^ω^) select a.dname,b.job,sum(b.sal) sum_sal,grouping(a.dname),grouping(b.job)
20:33:06 2 from dept a,emp b
20:33:06 3 where a.deptno=b.deptno
20:33:06 4 group by rollup(a.dname,b.job)
20:33:08 5 /
DNAME JOB SUM_SAL GROUPING(A.DNAME) GROUPING(B.JOB)
---------- ---------- ---------- ----------------- ---------------
SALES CLERK 950 0 0
SALES MANAGER 2850 0 0
SALES SALESMAN 5600 0 0
SALES 9400 0 1
RESEARCH CLERK 1200 0 0
RESEARCH ANALYST 7000 0 0
RESEARCH MANAGER 2975 0 0
RESEARCH 11175 0 1
ACCOUNTING CLERK 1300 0 0
ACCOUNTING MANAGER 2450 0 0
ACCOUNTING PRESIDENT 5000 0 0
ACCOUNTING 8750 0 1
29325 1 1
已选择13行。
解释:什么都不看(1,1);从左往右,先看第一个(0,1)
可以将不需要进行小计和合计的列移出rollup,要小计的留在rollup里面。
比如:
20:48:37 scott@ORCL (^ω^) select a.dname,b.job,sum(b.sal) sum_sal,grouping(a.dname),grouping(b.job)
20:57:43 2 from dept a,emp b
20:57:43 3 where a.deptno=b.deptno
20:57:43 4 group by a.dname,rollup(b.job)
20:57:45 5 /
DNAME JOB SUM_SAL GROUPING(A.DNAME) GROUPING(B.JOB)
---------------------------- ------------------ ---------- ----------------- ---------------
SALES CLERK 950 0 0
SALES MANAGER 2850 0 0
SALES SALESMAN 5600 0 0
SALES 9400 0 1
RESEARCH CLERK 1200 0 0
RESEARCH ANALYST 7000 0 0
RESEARCH MANAGER 2975 0 0
RESEARCH 11175 0 1
ACCOUNTING CLERK 1300 0 0
ACCOUNTING MANAGER 2450 0 0
ACCOUNTING PRESIDENT 5000 0 0
ACCOUNTING 8750 0 1
已选择12行。
cube
和rollup不同,cube的计算结果和顺序无关。若n列,则分组方式有2的n次方种。
20:57:46 scott@ORCL (^ω^) select a.dname,b.job,sum(b.sal) sum_sal,grouping(a.dname),grouping(b.job)
21:27:03 2 from dept a,emp b
21:27:03 3 where a.deptno=b.deptno
21:27:03 4 group by cube(a.dname,b.job)
21:27:04 5 /
DNAME JOB SUM_SAL GROUPING(A.DNAME) GROUPING(B.JOB)
---------------------------- ------------------ ---------- ----------------- ---------------
29325 1 1
CLERK 3450 1 0
ANALYST 7000 1 0
MANAGER 8275 1 0
SALESMAN 5600 1 0
PRESIDENT 5000 1 0
SALES 9400 0 1
SALES CLERK 950 0 0
SALES MANAGER 2850 0 0
SALES SALESMAN 5600 0 0
RESEARCH 11175 0 1
RESEARCH CLERK 1200 0 0
RESEARCH ANALYST 7000 0 0
RESEARCH MANAGER 2975 0 0
ACCOUNTING 8750 0 1
ACCOUNTING CLERK 1300 0 0
ACCOUNTING MANAGER 2450 0 0
ACCOUNTING PRESIDENT 5000 0 0
已选择18行。
注释:和rollup的结果相比,cube的所有可能的分组都走一遍。
可以去掉合计和某些不需要的小计,通过部分cube实现。部分cube比部分rollup来得有用多了。
21:27:06 scott@ORCL (^ω^) select a.dname,b.job,sum(b.sal) sum_sal,grouping(a.dname),grouping(b.job)
21:32:20 2 from dept a,emp b
21:32:20 3 where a.deptno=b.deptno
21:32:20 4 group by a.dname,cube(b.job)
21:32:21 5 /
DNAME JOB SUM_SAL GROUPING(A.DNAME) GROUPING(B.JOB)
---------------------------- ------------------ ---------- ----------------- ---------------
SALES 9400 0 1
SALES CLERK 950 0 0
SALES MANAGER 2850 0 0
SALES SALESMAN 5600 0 0
RESEARCH 11175 0 1
RESEARCH CLERK 1200 0 0
RESEARCH ANALYST 7000 0 0
RESEARCH MANAGER 2975 0 0
ACCOUNTING 8750 0 1
ACCOUNTING CLERK 1300 0 0
ACCOUNTING MANAGER 2450 0 0
ACCOUNTING PRESIDENT 5000 0 0
已选择12行。