分组集(Grouping Sets)是多个分组的并集,用于在一个查询中,按照不同的分组列对集合进行聚合运算,等价于对单个分组使用“union all”,计算多个结果集的并集。使用分组集的聚合查询,返回的select 子句相同,由于select子句只能引用分组列,因此,在单个分组中缺失的分组列,TSQL返回NULL值。

TSQL使用 group by 子句分组,有4种不同的语法:

  • group by a,b
  • group by rollup(a,b)
  • group by cube(a,b)
  • group by grouping sets((),(a),(a,b),rollup(a,b),cube(a,b))

一,分组集

1,单个分组

以集合的视角来看 “group by a,b” 子句,等价于 “group by grouping sets (a,b)”,(a,b) 是单个分组,是集合相乘的结果:(a)*(b)=(a,b) ;

2,预定义的分组集(grouping sets)

  • rollup(a,b) :预定义的分组集是(),(a),(a,b);
  • cube(a,b) :预定义的的分组集是(),(a),(b),(a,b);

3,使用grouping sets 自定义分组集

单个分组的集合是分组集, 分组集 grouping sets((a),(a,b)) :表示两个分组 (a,b),(a) 的并集,查询的结果等价于:

group by (a,b)
union all
group by(a)

4,分组集运算

分组集运算法则:

  • () :表示空集,整个集合作为一个分组;任何集合和空集相乘,结果是:(a)*()=(a);
  • 分组集相乘:两两组合,例如,{(a),(b)}*{(c),(d)}={(a,c),(a,d),(b,c),(b,d)}
  • 集合相乘时,不会去重:例如,{(a),(b)}*{(),(a)}={(a),(a,a),(b),(b,a)}
  • (a,a)等价于集合(a):例如,{(a),(b)}*{(),(a)}={(a),(a),(b),(b,a)}
  • group by是分组集相乘:例如, group by grouping sets((a),(b)),c 等价于 group by grouping sets((a,c),(b,c))
  • grouping sets是分组集求并集,不会去重:例如,grouping sets((a),(a)) 等价于 grouping sets(a) union all grouping sets(a)

4.1,解析:grouping sets(rollup(a,b),b) 等价于 group by cube(a,b)

解析过程:rollup(a,b)定义的分组集是(),(a),(a,b),并上分组(b),就是:((),(a),(a,b),(b)),等价于cube(a,b)。

4.2,解析 group by grouping sets((a),(b)), rollup(a)

解析过程:grouping sets((a),(b)),定义两个分组集合((a),(b)),rollup(a)定义两个分组集合:((),(a)),

两个分组集进行相乘:{(a),(b)}*{(),(a)}={(a),(a),(b),(b,a)},集合相乘时,不会去重;

二,示例

1,创建示例数据

create table dbo.Inventory
(
Item int not null,
Color varchar(10) not null,
Quantity int not null,
Store int not null
)

2,将整个集合作为一个分组,grouping sets 是()

select null as Item, null as Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory--等价于
select  null as Item, null as Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by grouping sets(())

3,grouping sets是(a)

select Item,null as Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by  Item
order by Item--等价于
select Item,null as Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by grouping sets( (Item))
order by Item

4,grouping sets 是(a,b)

select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from Inventory
group by  Item,Color
order by Item,Color--等价于
select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from Inventory
group by grouping sets( (Item,Color))
order by Item,Color

5,分组集是:rollup(a,b),或 grouping sets是((),(a),(a,b))

--rollup(a,b)的grouping sets是(),(a),(a,b)
select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by ROLLUP(Item,Color)
order by Item,Color--等价于
select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by grouping sets((),(Item),(Item,Color))
order by Item,Color

6,分组集是:cube(a,b),或grouping sets是(),(a),(b),(a,b)

--cube(a,b)的grouping sets是(),(a),(b),(a,b)
select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by ROLLUP(Item,Color)
order by Item,Color--等价于
select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by grouping sets((),(Item),(Color),(Item,Color))
order by Item,Color

7,对rollup(a,b),使用单个分组和union来实现

--rollup(a,b)的grouping sets是(),(a),(a,b)
select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by ROLLUP(Item,Color)
order by Item,Color--等价于
select null as Item, null as Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventoryunion ALL
select Item,null as Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by Item union ALL
select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by  Item,Color 

8,对cube(a,b),使用单个分组和union来实现

--cube(a,b)的grouping sets是(),(a),(b),(a,b)
select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by cube(Item,Color)
order by Item,Color--等价于
select null as Item, null as Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventoryunion ALL
select Item,null as Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by  Item union ALL
select null as Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by  Color
union all
select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by Item,Color 

9, cube(a,b)的等价分组集是:grouping sets(rollup(a,b),b),或grouping sets((),(a),(a,b),(b))

--cube(a,b)的组合是(),(a),(b),(a,b)
select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from Inventory
group by CUBE( Item,Color)
order by Item,Color--等价于
select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by grouping sets((),(Item),(Color),(Item,Color))
order by Item,Color--等价于
select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by grouping sets(rollup(Item,Color),(Color))
order by Item,Color

10,分组集相乘,结果集不去重

解析 grouping sets((a),(b)), rollup(a)等价于 grouping((a),(a),(b),(b,a))

解析过程是:grouping sets((a),(b)), 定义两个分组集是(a),(b),rollup(a)定义两个分组集是:(),(a)

对这个分组集进行集合乘法运算:{(a),(b)}*{(),(a)}={(a),(a),(b),(b,a)}

select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by grouping sets((Item),(Color)),rollup(Item)
order by Item,Color--等价于
select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by grouping sets((Item),(Color),(Item,Item),(Color,Item))
order by Item,Color

解析: grouping sets((a,b)),rollup(a)

解析过程:grouping sets((a,b)),定义分组集(a,b),rollup(a)定义分组集:{(),(a)},对这两个分组集合进行集合乘法运算:(a,b)*{(),(a)}={(a,b),(a,b)},实际上是两个相同的group by grouping sets((a,b)) 进行 union all 运算求并集。

select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by grouping sets((Item,Color)),rollup(Item)
order by Item,Color--等价于
select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from dbo.Inventory
group by grouping sets((Item,Color),(Color,Item))
order by Item,Color

三,分组集用法总结

1, cube和rollup 预定义grouping sets,

  • rollup(a,b):预定义的grouping sets是(),(a),(a,b);
  • cube(a,b):预定义的grouping sets是(),(a),(b),(a,b);

2,集合的乘法

group by a,b 表示的是分组集(a),(b)的乘法:(a)*(b)=(a,b)

group by grouping sets((a),(b)),c 表示的是分组集((a),(b)),(c)的乘法:((a),(b))*(c)=((a,c),(b,c))

3,集合的并集

grouping sets((a),(b)),表示的是分组集的并集,等价于:

grouping sets(a)
union all
grouping sets(b)

参考文档:

Using GROUP BY with ROLLUP, CUBE, and GROUPING SETS

GROUP BY (Transact-SQL)

GROUPING SETS Equivalents

TSQL 分组集(Grouping Sets)相关推荐

  1. Hive sql分组函数grouping sets、cube、rollup用法简介

    文章目录 1.数据如下: 2.建表如下: 3.grouping sets 4.cube 5.rollup 1.数据如下: user_id,dep_id,group_id,salary 10001,a, ...

  2. mysql group by cube_group by、grouping sets、with rollup、with cube方法

    场景 在编写报表的 sql 脚本的时候,可能会遇到多维度组合的情况,例如下面的情况.常规的做法是编写不同维度组合的 sql ,然后再使用 union all 进行全集(当分组维度数量比较多的时候,un ...

  3. oracle中的roll up,oracle  group by 与roll up,cube,grouping sets,grouping_id联合使用

    Oracle的group by除了基本用法以外,还有3种扩展用法,分别是rollup.cube.grouping sets. 1 rollup 假设有一个表test,有A.B.C.D.E5列. 如果使 ...

  4. presto和hive中grouping sets的格式不一致问题

    背景 遇到的问题,在presto中使用hive中的grouping sets报错 报错信息如下 [1] Query failed (#20220811_003524_00009_hzxre): lin ...

  5. [转]详解Oracle高级分组函数(ROLLUP, CUBE, GROUPING SETS)

    原文地址:http://blog.csdn.net/u014558001/article/details/42387929 本文主要讲解 ROLLUP, CUBE, GROUPING SETS的主要用 ...

  6. T-SQL中的GROUP BY GROUPING SETS

    最近遇到一个情况,需要在内网系统中出一个统计报表.需要根据不同条件使用多个group by语句.需要将所有聚合的数据进行UNION操作来完成不同维度的统计查看. 直到发现在SQL SERVER 200 ...

  7. postgresql 集合类型_PostgreSQL 分组集合新功能(GROUPING SETS,CUBE,ROLLUP)

    PostgreSQL 分组集合新功能(GROUPING SETS,CUBE,ROLLUP) 实验环境 操作系统:windows 10 家庭中文版 数据库系统: PostgreSQL 9.6.2 说明 ...

  8. oracle 分组统计效率,Oracle 分组求和函数(rollup、cube、grouping sets)

    文章目录 1 场景 1.1 概念 1.2 思维导图 1.3 数据准备 2 知识点小结 2.1 group by 2.2 grouping sets:单独分组 2.3 rollup:累计累加 2.4 c ...

  9. Oracle中grouping sets 操作符的使用,可以帮你生成多种分组统计结果

    在Oracle中,grouping sets 操作符是 group by 子句的进一步扩展.在Oracle Database 9i之前,使用 group by 子句一次只能显示单种分组结果,如果要生成 ...

最新文章

  1. SQL Server数据库新建拥有部分查看操作权限的用户
  2. 三种方式搭建yum源
  3. 论设计,需求和编码三者的关系
  4. 【错误记录】Android Studio 配置 AspectJ 报错 ( all buildscript {} blocks must appear before any plugins {} )
  5. Spring系列教程四:Spring对Bean的管理细节
  6. 90题细品吴恩达《机器学习》,感受被刷题支配的恐惧
  7. CRM Interactive Report的UI设计
  8. GUI Design Studio 4 5 151 0原型设计工具的使用
  9. iptables的应用
  10. 单片机c语言论文,基于51单片机的C语言程序设计论文.doc
  11. jdbc sql拼接字符串
  12. java docx转pdf_在java中将docx转换为pdf
  13. go字符串转byte_go语言中int和byte转换方式
  14. 用AlexNet训练MSTAR数据集
  15. java 集合元素自定义排序——Comparator.comparing , 不用实现 Comparable 接口
  16. Ubuntu Linux访问小米手机存储卡
  17. [矩阵的QR分解系列一] 施密特(Schmidt)正交规范化
  18. 软件设计师之法律法规知识
  19. Tools_@截屏工具@OCR识别工具@图片文字翻译工具长截屏,普通截屏套件推荐(by QQ)@鼠标键盘动作录制
  20. Day7 牛客 回文素数

热门文章

  1. 计算机的启动盘,Win7使用UltraISO制作U盘启动盘的方法
  2. python---常用模块,网站找模块方法、啄木鸟社区
  3. windows 10 家庭版打不开gpedit.msc组策略的解决办法
  4. 做加推的超级IP名片店铺原来这么赚钱!
  5. [本人经历]嵌入式工程师2022校招面试题:cvte+北京朝歌
  6. 遵化有教php基础知识,2019年河北省唐山市遵化市中小学语文教师招聘/编制考试历年真题试卷及答案解析...
  7. HTML Rendering Error(This view has crashed)处理方法
  8. 干货 | 5G全面基础知识
  9. 计算机关闭了休眠还是休眠了,电脑关机还是休眠?Win10关闭方式查看命令
  10. Design Compiler NXT:RTL Synthesis Workship (1)DC流程概述