oracle rollup,cube函数

以前一直都分不太清楚rollup和cube的区别,今天练习了一下,顿然明白了!!

rollup和cube是同group by 函数进行连接的!如果是rollup(A,B,C,)的话,首先会对(A,B,C)列进行group by,然后对(A,B)列进行group by,对(A)进行group by,最后对全表进行group by。

下面以两列来进行分析

SQL> select job,deptno,sum(sal) total_sal from emp group by rollup(job,deptno);

JOB           DEPTNO  TOTAL_SAL
--------- ---------- ----------
CLERK1            20       1100
CLERK1                     1100
CL_ERK            20        800
CL_ERK                      800
ANALYST           20       6000
ANALYST                    6000
CL43ERK           30        950
CL43ERK                     950
MANAGER           10       2450
MANAGER           20       2975
MANAGER           30       2850

JOB           DEPTNO  TOTAL_SAL
--------- ---------- ----------
MANAGER                    8275
CLfdERK2                   1300
CLfdERK2                   1300
SALESMAN          30       5600
SALESMAN                   5600
PRESIDENT         10       5000
PRESIDENT                  5000
                          29025

19 rows selected.

对上面的结论进行验证:

SQL> select job,deptno,sum(sal) total_sal from emp group by job,deptno  --对job,deptno进行group by
  2  union all
  3  select job,null,sum(sal) total_sal from emp group by job  --对job列进行group by
  4  union all
  5  select null,null,sum(sal) from emp;--对全部列进行group by

JOB           DEPTNO  TOTAL_SAL
--------- ---------- ----------
MANAGER           20       2975
PRESIDENT         10       5000
SALESMAN          30       5600
CL43ERK           30        950
CLfdERK2                   1300
ANALYST           20       6000
CL_ERK            20        800
MANAGER           30       2850
MANAGER           10       2450
CLERK1            20       1100
CLERK1                     1100

JOB           DEPTNO  TOTAL_SAL
--------- ---------- ----------
CL43ERK                     950
CLfdERK2                   1300
SALESMAN                   5600
PRESIDENT                  5000
MANAGER                    8275
ANALYST                    6000
CL_ERK                      800
                          29025

19 rows selected.

 

如果是GROUP BY CUBE(A, B, C),则首先会对(A、B、C)进行GROUP BY,然后依次是(A、B),(A、C),(A),(B、C),(B),(C),最后对全表进行GROUP BY操作。

SQL> select job,deptno,sum(sal) total_sal from emp group by cube(job,deptno);

JOB           DEPTNO  TOTAL_SAL
--------- ---------- ----------
                           1300
                          29025
                  10       7450
                  20      10875
                  30       9400
CLERK1                     1100
CLERK1            20       1100
CL_ERK                      800
CL_ERK            20        800
ANALYST                    6000
ANALYST           20       6000

JOB           DEPTNO  TOTAL_SAL
--------- ---------- ----------
CL43ERK                     950
CL43ERK           30        950
MANAGER                    8275
MANAGER           10       2450
MANAGER           20       2975
MANAGER           30       2850
CLfdERK2                   1300
CLfdERK2                   1300
SALESMAN                   5600
SALESMAN          30       5600
PRESIDENT                  5000

JOB           DEPTNO  TOTAL_SAL
--------- ---------- ----------
PRESIDENT         10       5000

23 rows selected.

 

对上面的结论进行验证:

SQL> select job,deptno,sum(sal) total_sal from emp group by job,deptno
  2  union all
  3  select job,null,sum(sal) total_sal from emp group by job
  4  union all
  5  select null,deptno,sum(sal) total_sal from emp group by deptno
  6  union all
  7  select null,null,sum(sal) from emp;

JOB           DEPTNO  TOTAL_SAL
--------- ---------- ----------
MANAGER           20       2975
PRESIDENT         10       5000
SALESMAN          30       5600
CL43ERK           30        950
CLfdERK2                   1300
ANALYST           20       6000
CL_ERK            20        800
MANAGER           30       2850
MANAGER           10       2450
CLERK1            20       1100
CLERK1                     1100

JOB           DEPTNO  TOTAL_SAL
--------- ---------- ----------
CL43ERK                     950
CLfdERK2                   1300
SALESMAN                   5600
PRESIDENT                  5000
MANAGER                    8275
ANALYST                    6000
CL_ERK                      800
                  30       9400
                           1300
                  20      10875
                  10       7450

JOB           DEPTNO  TOTAL_SAL
--------- ---------- ----------
                          29025

23 rows selected.

 

 

 

你可能感兴趣的:(oracle,sql,c,manager,null)