hive-sql : grouping运算符(自动汇总)

人一旦懒起来, 就喜欢少写几行代码;  于是对于自动汇总 再汇总合计, 就想到了 grouping 函数; 

 

举例: 

通常计算合计是分别计算出合计行和按照商品种类进行汇总的结果,然后通过union all连接在一起:

#!/bin/bash
select '合计' as product_type, sum(sale_price) from Product
union all
select product_type, sum(sale_price) from Product group by product_type;

 

--------------------------------------------------------------------------      开始玩转  grouping函数      ------------------------------------------------------------------------------


grouping函数:让null更加容易分辨

grouping运算符包含以下3种:1.rollup(同时得出合计和小计)

                                                 2.cube(用数据来搭积木)

                                                 3.grouping sets(取得期望的意思)

 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

rollup的使用方法: 

#!/bin/bash
select case when grouping(product_type) = 1 then '商品种类 '
		else product_type end as product_type
     , case when grouping(regist_date) = 1 then '登记日期 合计'
                else cast(regist_date as varchar(16)) end as regist_date
     , sum(sale_price) as sum_price
from Product
group by rollup(product_type, regist_date);

使用rollup时多出了最上方的合计行以及3条不同商品种类的小计行(也就是未使用登记日期作为聚合键的记录),这4行就是超级分组记录。

相当于3种聚合:1.group by()

                           2.group by(product_type)

                           3.group by(product_type, regist_date)

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

cube:用数据来搭积木 

#!/bin/bash
select case when grouping(product_type) = 1 then '商品种类 合计'
            else product_type end as product_type
     , case when grouping(regist_date) = 1 then '登记日期 合计'
	    else cast(regist_date as varchar(16)) end as regist_date
     , sum(sale_price) as sum_price
from Product
group by cube(product_type, regist_date);

 这里的cube按以下4种方式聚合:1.group by()

                                                     2.group by(product_type)

                                                     3.group by(regist_date)

                                                     4.group by(product_type, regist_date)

所谓cube,就是将group by子句中的聚合键的所有可能的组合的汇总结果集中到一个结果中。因此,组合的个数就是2的n次方(n为聚合键的个数)。

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

grouping sets:取得期望的意思 

#!/bin/bash
select case when grouping(product_type) = 1 then '商品种类 合计'
            else product_type end as product_type
     , case when grouping(regist_date) = 1 then '登记日期 合计'
            else cast(regist_date as varchar(16)) end as regist_date
     , sum(sale_price) as sum_price
from Product
group by grouping sets(product_type, regist_date);

 grouping sets用于从中取出个别条件对应的不固定结果(不常用)

 

 

 

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

原文链接:https://blog.csdn.net/qq_41805514/article/details/81777946

 

posted @ 2021-06-23 15:48  subsir  阅读(1572)  评论(0编辑  收藏  举报