PLSQL-高级分组

group by rollup(a,b)

说明:先做一次a和b的分组,再做一次a的分组,最后做一次no group

示例:统计公司每个部门的部门编号和总薪水,以及公司的总薪水

1
2
3
4
5
6
7
8
9
SQL> select deptno,sum(sal) from emp group by rollup(deptno);
  
DEPTNO   SUM(SAL)
---------- ----------
    10      8750
    20      10875
    25      10000
    30      9400
            39025  ---公司总薪水 

示例:统计公司每个部门的每个职位的总薪水,每个部门的总薪水,公司的总薪水

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
SQL> 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  --部门编号为10的总薪水
    20       CLERK      1900
    20       ANALYST    6000
    20       MANAGER    2975
    20                  10875  --部门编号为20的总薪水
    25                  10000
    25                  10000  --部门编号为25的总薪水
    30       CLERK      950
  
   DEPTNO     JOB      SUM(SAL)
---------- --------- ----------
    30       MANAGER    2850
    30       SALESMAN   5600
    30                  9400  --部门编号为30的总薪水
                        39025  

group by cube(a,b)

说明:分组采用a,b分组一次,a单独分组一次,b单独分组一次,no group分组一次

示例:统计公司每个部门的每个职位的薪水总和,每个部门的薪水总和,每个职位的薪水总和,公司总薪水

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
SQL> select deptno,job,sum(sal) from emp group by cube(deptno,job);
  
   DEPTNO     JOB      SUM(SAL)
---------- --------- ----------
                       10000  ---职业为空的总薪水
                       39025  ---公司总薪水
             CLERK     4150  ---职业为CLERK的总薪水
             ANALYST   6000  ---职业为ANALYST的总薪水
             MANAGER   8275  ---职业为MANAGER的总薪水
             SALESMAN  5600  ---职业为SALESMAN的总薪水
             PRESIDENT 5000  ---职业为PRESIDENT的总薪水
   10                  8750  ---部门编号为10的总薪水
   10        CLERK     1300
   10        MANAGER   2450
   10        PRESIDENT 5000
  
   DEPTNO     JOB      SUM(SAL)
---------- --------- ----------
   20                  10875  ---部门编号为20的总薪水
   20        CLERK     900
   20        ANALYST   6000
   20        MANAGER   2975
   25                  10000  ---部门编号为25的总薪水
   25                  10000
   30                  9400  ---部门编号为30的总薪水
   30        CLERK     950
   30        MANAGER   2850

grouping sets

说明:grouping sets相当于group by a union group by b

示例:统计公司每个职位的薪水总和,每个部门的薪水总和

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
SQL> select deptno,job,sum(sal) from emp group by grouping sets(deptno,job);
  
        DEPTNO JOB         SUM(SAL)
---------- --------- ----------
                                                    10000
                      CLERK           4150
                      SALESMAN        5600
                      PRESIDENT       5000
                      MANAGER         8275
                      ANALYST         6000
                30                 9400
                25                10000
                20                10875
                10                 8750

grouping

说明:分组报告函数,显示字段是否被用于分组,1表示是,0表示否

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
SQL> select deptno,job,grouping(deptno) from emp group by grouping sets(deptno,job);
  
  DEPTNO      JOB       GROUPING(DEPTNO)
---------- --------- ----------------
                          1
              CLERK       1
              SALESMAN    1
              PRESIDENT   1
              MANAGER     1
              ANALYST     1
   30                     0
   25                     0
   20                     0
   10                     0
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus