Hive_Hive ROLLUP, GROUPING SETS, CUBE 聚合函数 与 GROUPING 函数

在使用Hive 的时候,我们常常进行聚合统计运算。

聚合统计的运算函数有很多,比如 我们最常用的 GROUP BY 函数。但是常常我们需要多维度统计数据,这个时候我们就会用到Hive 的聚合统计函数

这里我们讲解下  ROLLUP,  GROUPING SETS, CUBE 的含义以及用法。

我们结合案例讲解下这几个函数的使用规则

数据统计背景:

我们现在有多个公司,多个部门,多位员工的薪水情况。现在我们需要按照多种维度去统计薪水。 

我们看下这几个函数的用法ROLLUP,  GROUPING SETS, CUBE

我们从 GROUPING SETS , ROLLUP, CUBE 一点点讨论。

GROUPING SETS

GROUPING SETS作为GROUP BY的子句,允许开发人员在GROUP BY语句后面指定多个统计选项,可以简单理解为多条group by语句通过union all把查询结果聚合起来结合起来,下面是几个实例可以帮助我们了解.

首先我们学习下 GROUPING SETS 

GROUPING SETS 用法如下:

SELECT

a,b...,f

FROM test_table

GROUP BY

a,b...,f

GROUPING SETS ((?,...,?),(xxx),(yyy))

GROUPING SETS 中间可以填写多个条件。

其中 (?,...,?) 可以为 a~f 中不重复的任意项

具体例子如下:

SELECT

a,b,SUM(c)

FROM test_table

GROUP BY

a,b

GROUPING SETS ((a),(a,b),())

等价于

SELECT

a

,NULL

,SUM(c)

FROM test_table

GROUP BY

a

UNION ALL

SELECT

a

,b

,SUM(c)

FROM test_table

GROUP BY

a

,b

UNION ALL

SELECT

NULL

,NULL

,SUM(c)

FROM test_table

;


实际案例,我们想按照公司 ,和整体 去统计员工的薪水, 但是我们想在 一条语句中完成。

我们该如何编写SQL 呢?

SQL如下:

SELECT

grouping__id

,company_name

,dep_name

,user_id

,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

GROUP BY

company_name

,dep_name

,user_id

GROUPING SETS ((company_name), ())

ORDER BY

grouping__id

;

输出如下:

INFO  : MapReduce Jobs Launched:

INFO  : Stage-Stage-1: Map: 1  Reduce: 1  Cumulative CPU: 4.13 sec  HDFS Read: 11666 HDFS Write: 175 SUCCESS

INFO  : Stage-Stage-2: Map: 1  Reduce: 1  Cumulative CPU: 3.73 sec  HDFS Read: 7060 HDFS Write: 188 SUCCESS

INFO  : Total MapReduce CPU Time Spent: 7 seconds 860 msec

INFO  : Completed executing command(queryId=hive_20200408032038_18e04047-b8c0-4d07-a5de-00ccbc7cb4cc); Time taken: 51.459 seconds

INFO  : OK

+---------------+---------------+-----------+----------+---------------+

| grouping__id  | company_name  | dep_name  | user_id  | total_salary  |

+---------------+---------------+-----------+----------+---------------+

| 0            | NULL          | NULL      | NULL    | 133500.00    |

| 1            | x.qx          | NULL      | NULL    | 59500.00      |

| 1            | s.zh          | NULL      | NULL    | 74000.00      |

+---------------+---------------+-----------+----------+---------------+

可以看到 grouping_id 为0, 计算的是整体的薪水和,而grouping_id 为1,  计算的是分公司的薪水

我们刚才说过 用 GROUPING SETS  和  GROUP BY + UNION ALL 是等价的,但是它们真的一样么。我们运行 EXPLAIN 检验一下。

下面给出两个运行结果等价的SQL 的 EXPLAIN 结果。

EXPLAIN

SELECT

*

FROM

(

SELECT

0 AS mark

,NULL

,NULL

,NULL

,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

UNION ALL

SELECT

1 AS mark

,company_name

,NULL

,NULL

,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

GROUP BY

company_name

) tmp

ORDER BY mark

;


SQL2

EXPLAIN

SELECT

grouping__id

,company_name

,dep_name

,user_id

,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

GROUP BY

company_name

,dep_name

,user_id

GROUPING SETS ((company_name), ())

ORDER BY

grouping__id

;


先贴出2个SQL的分别EXPLAIN 结果

UNION ALL 的SQL

注意 : UNION ALL 中做聚合增加更多的SQL 会增加 JOB 数量。

STAGE DEPENDENCIES:                               

  Stage-1 is a root stage                         

  Stage-2 depends on stages: Stage-1, Stage-3     

  Stage-3 is a root stage                         

  Stage-0 depends on stages: Stage-2             


STAGE PLANS:                                     

  Stage: Stage-1                                 

    Map Reduce                                   

      Map Operator Tree:                         

          TableScan                               

            alias: datacube_salary_org           

            filterExpr: (pt = '20200407') (type: boolean)

            Statistics: Num rows: 7 Data size: 340 Basic stats: COMPLETE Column stats: NONE

            Select Operator                       

              expressions: salary (type: decimal(10,2))

              outputColumnNames: salary           

              Statistics: Num rows: 7 Data size: 340 Basic stats: COMPLETE Column stats: NONE

              Group By Operator                   

                aggregations: sum(salary)         

                mode: hash                       

                outputColumnNames: _col0         

                Statistics: Num rows: 1 Data size: 112 Basic stats: COMPLETE Column stats: NONE

                Reduce Output Operator           

                  sort order:                     

                  Statistics: Num rows: 1 Data size: 112 Basic stats: COMPLETE Column stats: NONE

                  value expressions: _col0 (type: decimal(20,2))

      Reduce Operator Tree:                       

        Group By Operator                         

          aggregations: sum(VALUE._col0)         

          mode: mergepartial                     

          outputColumnNames: _col0               

          Statistics: Num rows: 1 Data size: 112 Basic stats: COMPLETE Column stats: NONE

          Select Operator                         

            expressions: 0 (type: int), null (type: string), _col0 (type: decimal(20,2))

            outputColumnNames: _col0, _col1, _col4

            Statistics: Num rows: 1 Data size: 112 Basic stats: COMPLETE Column stats: NONE

            File Output Operator                 

              compressed: false                   

              table:                             

                  input format: org.apache.hadoop.mapred.SequenceFileInputFormat

                  output format: org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat

                  serde: org.apache.hadoop.hive.serde2.lazybinary.LazyBinarySerDe


  Stage: Stage-2                                 

    Map Reduce                                   

      Map Operator Tree:                         

          TableScan                               

            Union                                 

              Statistics: Num rows: 4 Data size: 257 Basic stats: COMPLETE Column stats: NONE

              Select Operator                     

                expressions: _col0 (type: int), _col1 (type: string), _col4 (type: decimal(20,2))

                outputColumnNames: _col0, _col1, _col4

                Statistics: Num rows: 4 Data size: 257 Basic stats: COMPLETE Column stats: NONE

                Reduce Output Operator           

                  key expressions: _col0 (type: int)

                  sort order: +                   

                  Statistics: Num rows: 4 Data size: 257 Basic stats: COMPLETE Column stats: NONE

                  value expressions: _col1 (type: string), _col4 (type: decimal(20,2))

          TableScan                               

            Union                                 

              Statistics: Num rows: 4 Data size: 257 Basic stats: COMPLETE Column stats: NONE

              Select Operator                     

                expressions: _col0 (type: int), _col1 (type: string), _col4 (type: decimal(20,2))

                outputColumnNames: _col0, _col1, _col4

                Statistics: Num rows: 4 Data size: 257 Basic stats: COMPLETE Column stats: NONE

                Reduce Output Operator           

                  key expressions: _col0 (type: int)

                  sort order: +                   

                  Statistics: Num rows: 4 Data size: 257 Basic stats: COMPLETE Column stats: NONE

                  value expressions: _col1 (type: string), _col4 (type: decimal(20,2))

      Reduce Operator Tree:                       

        Select Operator                           

          expressions: KEY.reducesinkkey0 (type: int), VALUE._col0 (type: string), null (type: void), null (type: void), VALUE._col3 (type: decimal(20,2))

          outputColumnNames: _col0, _col1, _col2, _col3, _col4

          Statistics: Num rows: 4 Data size: 257 Basic stats: COMPLETE Column stats: NONE

          File Output Operator                   

            compressed: false                     

            Statistics: Num rows: 4 Data size: 257 Basic stats: COMPLETE Column stats: NONE

            table:                               

                input format: org.apache.hadoop.mapred.SequenceFileInputFormat

                output format: org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat

                serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe


  Stage: Stage-3                                 

    Map Reduce                                   

      Map Operator Tree:                         

          TableScan                               

            alias: datacube_salary_org           

            filterExpr: (pt = '20200407') (type: boolean)

            Statistics: Num rows: 7 Data size: 340 Basic stats: COMPLETE Column stats: NONE

            Select Operator                       

              expressions: company_name (type: string), salary (type: decimal(10,2))

              outputColumnNames: company_name, salary

              Statistics: Num rows: 7 Data size: 340 Basic stats: COMPLETE Column stats: NONE

              Group By Operator                   

                aggregations: sum(salary)         

                keys: company_name (type: string) 

                mode: hash                       

                outputColumnNames: _col0, _col1   

                Statistics: Num rows: 7 Data size: 340 Basic stats: COMPLETE Column stats: NONE

                Reduce Output Operator           

                  key expressions: _col0 (type: string)

                  sort order: +                   

                  Map-reduce partition columns: _col0 (type: string)

                  Statistics: Num rows: 7 Data size: 340 Basic stats: COMPLETE Column stats: NONE

                  value expressions: _col1 (type: decimal(20,2))

      Reduce Operator Tree:                       

        Group By Operator                         

          aggregations: sum(VALUE._col0)         

          keys: KEY._col0 (type: string)         

          mode: mergepartial                     

          outputColumnNames: _col0, _col1         

          Statistics: Num rows: 3 Data size: 145 Basic stats: COMPLETE Column stats: NONE

          Select Operator                         

            expressions: 1 (type: int), _col0 (type: string), _col1 (type: decimal(20,2))

            outputColumnNames: _col0, _col1, _col4

            Statistics: Num rows: 3 Data size: 145 Basic stats: COMPLETE Column stats: NONE

            File Output Operator                 

              compressed: false                   

              table:                             

                  input format: org.apache.hadoop.mapred.SequenceFileInputFormat

                  output format: org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat

                  serde: org.apache.hadoop.hive.serde2.lazybinary.LazyBinarySerDe


  Stage: Stage-0                                 

    Fetch Operator                               

      limit: -1                                   

      Processor Tree:                             

        ListSink                                 


GROUPING SETS 的SQL

STAGE DEPENDENCIES:                               

  Stage-1 is a root stage                         

  Stage-2 depends on stages: Stage-1             

  Stage-0 depends on stages: Stage-2             


STAGE PLANS:                                     

  Stage: Stage-1                                 

    Map Reduce                                   

      Map Operator Tree:                         

          TableScan                               

            alias: datacube_salary_org           

            filterExpr: (pt = '20200407') (type: boolean)

            Statistics: Num rows: 7 Data size: 340 Basic stats: COMPLETE Column stats: NONE

            Select Operator                       

              expressions: company_name (type: string), dep_name (type: string), user_id (type: bigint), salary (type: decimal(10,2))

              outputColumnNames: company_name, dep_name, user_id, salary

              Statistics: Num rows: 7 Data size: 340 Basic stats: COMPLETE Column stats: NONE

              Group By Operator                   

                aggregations: sum(salary)         

                keys: company_name (type: string), dep_name (type: string), user_id (type: bigint), 0 (type: int)

                mode: hash                       

                outputColumnNames: _col0, _col1, _col2, _col3, _col4

                Statistics: Num rows: 14 Data size: 680 Basic stats: COMPLETE Column stats: NONE

                Reduce Output Operator           

                  key expressions: _col0 (type: string), _col1 (type: string), _col2 (type: bigint), _col3 (type: int)

                  sort order: ++++               

                  Map-reduce partition columns: _col0 (type: string), _col1 (type: string), _col2 (type: bigint), _col3 (type: int)

                  Statistics: Num rows: 14 Data size: 680 Basic stats: COMPLETE Column stats: NONE

                  value expressions: _col4 (type: decimal(20,2))

      Reduce Operator Tree:                       

        Group By Operator                         

          aggregations: sum(VALUE._col0)         

          keys: KEY._col0 (type: string), KEY._col1 (type: string), KEY._col2 (type: bigint), KEY._col3 (type: int)

          mode: mergepartial                     

          outputColumnNames: _col0, _col1, _col2, _col3, _col4

          Statistics: Num rows: 7 Data size: 340 Basic stats: COMPLETE Column stats: NONE

          Select Operator                         

            expressions: _col3 (type: int), _col0 (type: string), _col1 (type: string), _col2 (type: bigint), _col4 (type: decimal(20,2))

            outputColumnNames: _col0, _col1, _col2, _col3, _col4

            Statistics: Num rows: 7 Data size: 340 Basic stats: COMPLETE Column stats: NONE

            File Output Operator                 

              compressed: false                   

              table:                             

                  input format: org.apache.hadoop.mapred.SequenceFileInputFormat

                  output format: org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat

                  serde: org.apache.hadoop.hive.serde2.lazybinary.LazyBinarySerDe


  Stage: Stage-2                                 

    Map Reduce                                   

      Map Operator Tree:                         

          TableScan                               

            Reduce Output Operator               

              key expressions: _col0 (type: int) 

              sort order: +                       

              Statistics: Num rows: 7 Data size: 340 Basic stats: COMPLETE Column stats: NONE

              value expressions: _col1 (type: string), _col2 (type: string), _col3 (type: bigint), _col4 (type: decimal(20,2))

      Reduce Operator Tree:                       

        Select Operator                           

          expressions: KEY.reducesinkkey0 (type: int), VALUE._col0 (type: string), VALUE._col1 (type: string), VALUE._col2 (type: bigint), VALUE._col3 (type: decimal(20,2))

          outputColumnNames: _col0, _col1, _col2, _col3, _col4

          Statistics: Num rows: 7 Data size: 340 Basic stats: COMPLETE Column stats: NONE

          File Output Operator                   

            compressed: false                     

            Statistics: Num rows: 7 Data size: 340 Basic stats: COMPLETE Column stats: NONE

            table:                               

                input format: org.apache.hadoop.mapred.SequenceFileInputFormat

                output format: org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat

                serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe


  Stage: Stage-0                                 

    Fetch Operator                               

      limit: -1                                   

      Processor Tree:                             

        ListSink                                 


通过以上分析比较,我们不难发现 GROUPING SETS 的作业划分数量更少,

实际情况下 : GROUPING SETS 的 运行效率也要高于 UNION ALL 的 GROUP BY 形式

下面是对比的执行时间

GROUPING SETS

INFO  : MapReduce Jobs Launched:

INFO  : Stage-Stage-1: Map: 1  Reduce: 1  Cumulative CPU: 3.62 sec  HDFS Read: 11666 HDFS Write: 175 SUCCESS

INFO  : Stage-Stage-2: Map: 1  Reduce: 1  Cumulative CPU: 3.51 sec  HDFS Read: 7060 HDFS Write: 188 SUCCESS

INFO  : Total MapReduce CPU Time Spent: 7 seconds 130 msec

INFO  : Completed executing command(queryId=hive_20200408045412_4ab9e09f-436e-4433-9a1f-a03d5b32ef3e); Time taken: 49.676 seconds

INFO  : OK

+---------------+---------------+-----------+----------+---------------+

| grouping__id  | company_name  | dep_name  | user_id  | total_salary  |

+---------------+---------------+-----------+----------+---------------+

| 0            | NULL          | NULL      | NULL    | 133500.00    |

| 1            | x.qx          | NULL      | NULL    | 59500.00      |

| 1            | s.zh          | NULL      | NULL    | 74000.00      |

+---------------+---------------+-----------+----------+---------------+

UNION ALL 的 GROUP BY

INFO  : MapReduce Jobs Launched:

INFO  : Stage-Stage-1: Map: 1  Reduce: 1  Cumulative CPU: 3.7 sec  HDFS Read: 10541 HDFS Write: 119 SUCCESS

INFO  : Stage-Stage-3: Map: 1  Reduce: 1  Cumulative CPU: 4.34 sec  HDFS Read: 10919 HDFS Write: 152 SUCCESS

INFO  : Stage-Stage-2: Map: 2  Reduce: 1  Cumulative CPU: 5.08 sec  HDFS Read: 12932 HDFS Write: 188 SUCCESS

INFO  : Total MapReduce CPU Time Spent: 13 seconds 120 msec

INFO  : Completed executing command(queryId=hive_20200408045141_2033cbf6-a457-4bdb-aaec-65900b386972); Time taken: 84.365 seconds

INFO  : OK

+-----------+----------+----------+----------+-------------------+

| tmp.mark  | tmp._c1  | tmp._c2  | tmp._c3  | tmp.total_salary  |

+-----------+----------+----------+----------+-------------------+

| 0        | NULL    | NULL    | NULL    | 133500.00        |

| 1        | x.qx    | NULL    | NULL    | 59500.00          |

| 1        | s.zh    | NULL    | NULL    | 74000.00          |

+-----------+----------+----------+----------+-------------------+

可以看到 GROUPING SETS 的执行效率更高

ROLLUP

rollup可以实现从右到左递减多级的统计,显示统计某一层次结构的聚合。(即指定字段组合的递减汇总,如with rollup a,b,c,d   那对应的二进制只能是1111,1110,1100,1000,0000)

SELECT

grouping__id

,company_name

,dep_name

,user_id

,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

GROUP BY

company_name

,dep_name

,user_id

WITH ROLLUP

ORDER BY

grouping__id

;


等价于

SELECT

grouping__id

,company_name

,dep_name

,user_id

,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

GROUP BY

company_name

,dep_name

,user_id

GROUPING SETS ((company_name, dep_name, user_id), (company_name, dep_name), (company_name),())

ORDER BY

grouping__id

;


等价于

SELECT

company_name

,dep_name

,user_id

,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

GROUP BY

company_name

,dep_name

,user_id

UNION ALL

SELECT

company_name

,dep_name

,NULL

,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

GROUP BY

company_name

,dep_name

UNION ALL

SELECT

company_name

,NULL

,NULL

,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

GROUP BY

company_name

UNION ALL

SELECT

NULL

,NULL

,NULL

,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

;


运算结果

INFO  : MapReduce Jobs Launched:

INFO  : Stage-Stage-1: Map: 1  Reduce: 1  Cumulative CPU: 4.21 sec  HDFS Read: 11674 HDFS Write: 563 SUCCESS

INFO  : Stage-Stage-2: Map: 1  Reduce: 1  Cumulative CPU: 3.91 sec  HDFS Read: 7448 HDFS Write: 602 SUCCESS

INFO  : Total MapReduce CPU Time Spent: 8 seconds 120 msec

INFO  : Completed executing command(queryId=hive_20200408052638_740f42b9-6f08-49a6-8123-9a77aedc6b19); Time taken: 50.563 seconds

INFO  : OK

+---------------+---------------+-----------+----------+---------------+

| grouping__id  | company_name  | dep_name  | user_id  | total_salary  |

+---------------+---------------+-----------+----------+---------------+

| 0            | NULL          | NULL      | NULL    | 133500.00    |

| 1            | s.zh          | NULL      | NULL    | 74000.00      |

| 1            | x.qx          | NULL      | NULL    | 59500.00      |

| 3            | s.zh          | tester    | NULL    | 20000.00      |

| 3            | x.qx          | kiccp    | NULL    | 8600.00      |

| 3            | x.qx          | finance  | NULL    | 50900.00      |

| 3            | s.zh          | enginer  | NULL    | 54000.00      |

| 7            | x.qx          | kiccp    | 7        | 8600.00      |

| 7            | x.qx          | finance  | 6        | 13000.00      |

| 7            | x.qx          | finance  | 5        | 24500.00      |

| 7            | x.qx          | finance  | 4        | 13400.00      |

| 7            | s.zh          | enginer  | 2        | 26000.00      |

| 7            | s.zh          | enginer  | 1        | 28000.00      |

| 7            | s.zh          | tester    | 3        | 20000.00      |

+---------------+---------------+-----------+----------+---------------+


这里我们简要的说一下 GROUPING__ID 的计算规则

SELECT

 grouping__id

 ,company_name

 ,dep_name

 ,user_id

 ,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

GROUP BY 

 company_name

 ,dep_name

 ,user_id 

 WITH ROLLUP

ORDER BY

 grouping__id

;

可以看到 GROUP BY 中有 三个字段, 依次是 company_name, dep_name, user_id,可以看作3位二进制(0/1),(0/1),(0/1) ,低位对应 company_name,高位对应 user_id ,如果这一位聚合了(GROUP BY 中存在该字段),则为0,GROUPING 函数对应也返回0 ,最后 GROUPING_ID 为对应2进制 转换的 10进制数。

1) ROLLUP 的 全分组的子集

SELECT 

 company_name

 ,dep_name

 ,user_id

 ,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

GROUP BY 

 company_name

 ,dep_name

 ,user_id 

对应的结果

+---------------+---------------+-----------+----------+---------------+

| grouping__id  | company_name  | dep_name  | user_id  | total_salary  |

+---------------+---------------+-----------+----------+---------------+

| 7            | x.qx          | kiccp    | 7        | 8600.00      |

| 7            | x.qx          | finance  | 6        | 13000.00      |

| 7            | x.qx          | finance  | 5        | 24500.00      |

| 7            | x.qx          | finance  | 4        | 13400.00      |

| 7            | s.zh          | enginer  | 2        | 26000.00      |

| 7            | s.zh          | enginer  | 1        | 28000.00      |

| 7            | s.zh          | tester    | 3        | 20000.00      |

+---------------+---------------+-----------+----------+---------------+

因为 GROUP BY 这三个字段都有,则为 111 ,所以 GROUPING__ID 为7

2)  ROLLUP  company_name, dep_name 分组的子集

SELECT

company_name

,dep_name

,NULL

,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

GROUP BY

company_name

,dep_name

对应的子集

+---------------+---------------+-----------+----------+---------------+

| grouping__id  | company_name  | dep_name  | user_id  | total_salary  |

+---------------+---------------+-----------+----------+---------------+

| 3            | s.zh          | tester    | NULL    | 20000.00      |

| 3            | x.qx          | kiccp    | NULL    | 8600.00      |

| 3            | x.qx          | finance  | NULL    | 50900.00      |

| 3            | s.zh          | enginer  | NULL    | 54000.00      |

+---------------+---------------+-----------+----------+---------------+

CUBE 

  cube简称数据魔方,可以实现hive多个任意维度的查询,cube(a,b,c)则首先会对(a,b,c)进行group by,然后依次是(a,b),(a,c),(a),(b,c),(b),(c),最后在对全表进行group by,它会统计所选列中值的所有组合的聚合(即所有字段的组合)

SELECT

 grouping__id

 ,company_name

 ,dep_name

 ,user_id

 ,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

GROUP BY 

 company_name

 ,dep_name

 ,user_id 

 WITH CUBE

ORDER BY

 grouping__id

;

等价于 

(company_name,dep_name,user_id)

SELECT

company_name

,dep_name

,user_id

,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

GROUP BY

company_name

,dep_name

,user_id

UNION ALL

SELECT

company_name

,dep_name

,NULL

,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

GROUP BY

company_name

,dep_name

UNION ALL

SELECT

company_name

,NULL

,user_id

,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

GROUP BY

company_name

,user_id

UNION ALL

SELECT

company_name

,NULL

,NULL

,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

GROUP BY

company_name

UNION ALL

SELECT

NULL

,dep_name

,user_id

,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

GROUP BY

dep_name

,user_id

UNION ALL

SELECT

NULL

,dep_name

,NULL

,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

GROUP BY

dep_name

UNION ALL

SELECT

NULL

,NULL

,user_id

,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

GROUP BY

user_id

UNION ALL

SELECT

NULL

,NULL

,NULL

,SUM(salary) AS total_salary

FROM datacube_salary_org

WHERE pt = '20200407'

;


结果如下:

+---------------+---------------+-----------+----------+---------------+

| grouping__id  | company_name  | dep_name  | user_id  | total_salary  |

+---------------+---------------+-----------+----------+---------------+

| 0            | NULL          | NULL      | NULL    | 133500.00    |

| 1            | s.zh          | NULL      | NULL    | 74000.00      |

| 1            | x.qx          | NULL      | NULL    | 59500.00      |

| 2            | NULL          | finance  | NULL    | 50900.00      |

| 2            | NULL          | kiccp    | NULL    | 8600.00      |

| 2            | NULL          | tester    | NULL    | 20000.00      |

| 2            | NULL          | enginer  | NULL    | 54000.00      |

| 3            | s.zh          | tester    | NULL    | 20000.00      |

| 3            | s.zh          | enginer  | NULL    | 54000.00      |

| 3            | x.qx          | kiccp    | NULL    | 8600.00      |

| 3            | x.qx          | finance  | NULL    | 50900.00      |

| 4            | NULL          | NULL      | 7        | 8600.00      |

| 4            | NULL          | NULL      | 5        | 24500.00      |

| 4            | NULL          | NULL      | 4        | 13400.00      |

| 4            | NULL          | NULL      | 3        | 20000.00      |

| 4            | NULL          | NULL      | 2        | 26000.00      |

| 4            | NULL          | NULL      | 1        | 28000.00      |

| 4            | NULL          | NULL      | 6        | 13000.00      |

| 5            | s.zh          | NULL      | 2        | 26000.00      |

| 5            | s.zh          | NULL      | 3        | 20000.00      |

| 5            | x.qx          | NULL      | 5        | 24500.00      |

| 5            | x.qx          | NULL      | 6        | 13000.00      |

| 5            | s.zh          | NULL      | 1        | 28000.00      |

| 5            | x.qx          | NULL      | 7        | 8600.00      |

| 5            | x.qx          | NULL      | 4        | 13400.00      |

| 6            | NULL          | enginer  | 1        | 28000.00      |

| 6            | NULL          | finance  | 4        | 13400.00      |

| 6            | NULL          | tester    | 3        | 20000.00      |

| 6            | NULL          | finance  | 5        | 24500.00      |

| 6            | NULL          | kiccp    | 7        | 8600.00      |

| 6            | NULL          | enginer  | 2        | 26000.00      |

| 6            | NULL          | finance  | 6        | 13000.00      |

| 7            | x.qx          | finance  | 5        | 24500.00      |

| 7            | x.qx          | finance  | 4        | 13400.00      |

| 7            | x.qx          | kiccp    | 7        | 8600.00      |

| 7            | s.zh          | tester    | 3        | 20000.00      |

| 7            | s.zh          | enginer  | 2        | 26000.00      |

| 7            | s.zh          | enginer  | 1        | 28000.00      |

| 7            | x.qx          | finance  | 6        | 13000.00      |

+---------------+---------------+-----------+----------+---------------+

总结:

grouping sets:指定字段的组合来做group by

with rollup:指定字段的组合依次从右向左来递减做group by

with cube:指定字段的全组合

————————————————

版权声明:本文为CSDN博主「高达一号」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/u010003835/article/details/105353510

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,458评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,454评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,171评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,062评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,440评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,661评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,906评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,609评论 0 200
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,379评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,600评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,085评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,409评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,072评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,088评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,860评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,704评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,608评论 2 270

推荐阅读更多精彩内容