TSQL 分组集(Grouping Sets)
分组集(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.Inventory union 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.Inventory union 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
TSQL 分组集(Grouping Sets)的更多相关文章
- T-SQL基础(7) - 透视,逆透视和分组集
透视转换: use tempdb;if object_id('dbo.Orders', 'U') is not null drop table dbo.Orders;create table dbo. ...
- Oracle的rollup、cube、grouping sets函数
转载自:https://blog.csdn.net/huang_xw/article/details/6402396 Oracle的group by除了基本用法以外,还有3种扩展用法,分别是rollu ...
- Group By Grouping Sets
Group by分组函数的自定义,与group by配合使用可更加灵活的对结果集进行分组,Grouping sets会对各个层级进行汇总,然后将各个层级的汇总值union all在一起,但却比单纯的g ...
- Group By 多个分组集小结 --GROUPING SETS,GROUP BY CUBE,GROUP BY ROLLUP,GROUPING(),GROUPING_ID()
T-SQL 多个分组集共有三种 GROUPING SETS, CUBE, 以及ROLLUP, 其中 CUBE和ROLLUP可以当做是GROUPING SETS的简写版 示例数据库下载: http:// ...
- [转]详解Oracle高级分组函数(ROLLUP, CUBE, GROUPING SETS)
原文地址:http://blog.csdn.net/u014558001/article/details/42387929 本文主要讲解 ROLLUP, CUBE, GROUPING SETS的主要用 ...
- Oracle分组小计、总计示例(grouping sets的使用)
1.首先创建一个表 create table TE ( ID VARCHAR2(2), T_CODE VARCHAR2(4), T_NAME VARCHAR2(4), T_A ...
- Oracle分组函数之Grouping Sets
功能介绍: 自定义分组的字段 创建表: 插入测试数据: Grouping Sets(null,t.classid,(t.classid,t.studentname)),类似于ROLLUP Select ...
- SQL Server里Grouping Sets的威力
在SQL Server里,你有没有想进行跨越多个列/纬度的聚集操作,不使用SSAS许可(SQL Server分析服务).我不是说在生产里使用开发版,也不是说安装盗版SQL Server. 不可能的任务 ...
- GROUPING SETS、CUBE、ROLLUP
其实还是写一个Demo 比较好 USE tempdb IF OBJECT_ID( 'dbo.T1' , 'U' )IS NOT NULL BEGIN DROP TABLE dbo.T1; END; G ...
随机推荐
- ASP.NET Aries 入门开发教程3:开发一个列表页面及操控查询区
前言: Aries框架毕竟是开发框架,所以重点还是要写代码的,这样开发人员才不会失业,哈. 步骤1:新建html 建一个Html,主要有三步: 1:引入Aries.Loader.js 2:弄一个tab ...
- Connect() 2016 大会的主题 ---微软大法好
文章首发于微信公众号"dotnet跨平台",欢迎关注,可以扫页面左面的二维码. 今年 Connect 大会的主题是 Big possibilities. Bold technolo ...
- 工欲善其事,必先利其器 之 VS2013全攻略(安装,技巧,快捷键,插件)!
如有需要WPF工具的朋友可以移步 工欲善其事,必先利其器 之 WPF篇: 随着开发轨迹来看高效WPF开发的工具和技巧 之前一篇<c++的性能, c#的产能?!鱼和熊掌可以兼得,.NET NATI ...
- HTML5 sessionStorage会话存储
sessionStorage 是HTML5新增的一个会话存储对象,用于临时保存同一窗口(或标签页)的数据,在关闭窗口或标签页之后将会删除这些数据.本篇主要介绍 sessionStorage(会话存储) ...
- Paypal开发中遇到请求被中止: 未能创建 SSL/TLS 安全通道及解决方案
最近在基于ASP.NET上开发了Paypal支付平台,在ASP.NET开发的过程中没有遇到这个问题,但是引用到MVC开发模式中的时候就出现了"未能创建 SSL/TLS 安全通道及解决方案&q ...
- Redis链表实现
链表在 Redis 中的应用非常广泛, 比如列表键的底层实现之一就是链表: 当一个列表键包含了数量比较多的元素, 又或者列表中包含的元素都是比较长的字符串时, Redis 就会使用链表作为列表键的底层 ...
- 程序猿都没对象,JS竟然有对象?
现在做项目基本是套用框架,不论是网上的前端还是后端框架,也会寻找一些封装好的插件拿来即用,但还是希望拿来时最好自己过后再回过头了解里面的原理,学习里面优秀的东西,不论代码封装性,还是小到命名. 好吧, ...
- arcgis api for js入门开发系列八聚合效果(含源代码)
上一篇实现了demo的图层控制模块,本篇新增聚合效果,截图如下(源代码见文章底部): 聚合效果实现的思路如下: 1.map.html引用聚合包,项目已经包含进来了的聚合文件夹: <script ...
- Activity之概览屏幕(Overview Screen)
概览屏幕 概览屏幕(也称为最新动态屏幕.最近任务列表或最近使用的应用)是一个系统级别 UI,其中列出了最近访问过的 Activity 和任务. 用户可以浏览该列表并选择要恢复的任务,也可以通过滑动清除 ...
- 如何区别exists与not exists?
1.exists:sql返回结果集为真:not exists:sql不返回结果集为真.详解过程如图: exists not exists