rollup function
of this blog brief usage rollup grouping function of the oracle, rollup functions commonly used in statistical grouping is contained within one oracle analysis functions
Preparing the Environment
create table dept as select * from scott.dept;
create table emp as select * from scott.emp;
Business scenario: Find the sum of the sum of wages and salaries in all sectors of the various departments
Here you can do with the union, wages statistics and press department, then in the statistics of all the wage sector and
select a.dname, sum(b.sal)
from scott.dept a, scott.emp b
where a.deptno = b.deptno
group by a.dname
union all
select null, sum(b.sal)
from scott.dept a, scott.emp b
where a.deptno = b.deptno;
The above is a union to do, and then do with rollup, grammar is simpler and better performance
select a.dname, sum(b.sal)
from scott.dept a, scott.emp b
where a.deptno = b.deptno
group by rollup(a.dname);
Business scenario: Based on the above statistics, coupled with the demand, and now you want to take a look at wages and job corresponding to each department
select a.dname, b.job, sum(b.sal)
from scott.dept a, scott.emp b
where a.deptno = b.deptno
group by a.dname, b.job
union all//各部门的工资之和
select a.dname, null, sum(b.sal)
from scott.dept a, scott.emp b
where a.deptno = b.deptno
group by a.dname
union all//所有部门工资之和
select null, null, sum(b.sal)
from scott.dept a, scott.emp b
where a.deptno = b.deptno;
With a rollup to achieve, simpler syntax
select a.dname, b.job, sum(b.sal)
from scott.dept a, scott.emp b
where a.deptno = b.deptno
group by rollup(a.dname, b.job);
If coupled with a time statistics, you can use the following sql:
select to_char(b.hiredate, 'yyyy') hiredate, a.dname, b.job, sum(b.sal)
from scott.dept a, scott.emp b
where a.deptno = b.deptno
group by rollup(to_char(b.hiredate, 'yyyy'), a.dname, b.job);
cube function
select a.dname, b.job, sum(b.sal)
from scott.dept a, scott.emp b
where a.deptno = b.deptno
group by cube(a.dname, b.job);
cube function is the dimension finer statistics, syntax, and similar rollup
Suppose there are n dimensions, then there will be n rollup polymerized, polymerizable Cube have 2n
rollup statistics from
rollup (a, b) Statistics column comprising: (A, B), (A), ()
rollup (A, B, C) Statistics column comprising: (a, b, c) , (a, b) , (A), ()
....cube statistics from
cube (a, b) Statistics column comprising: (A, B), (A), (B), ()
cube (A, B, C) Statistics column comprising: (a, b, c) , ( A, B), (A, C), (B, C), (A), (B), (C), ()
....