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

GROUP BY (Transact-SQL)

GROUPING SETS Equivalents

时间: 2024-10-10 16:58:29

TSQL 分组集(Grouping Sets)的相关文章

笔记-Microsoft SQL Server 2008技术内幕:T-SQL语言基础-07 透视、逆透视及分组集

透视转换 透视数据是一种把数据从行的状态旋转为列的状态的处理.每个透视转换将涉及分组.扩展及聚合三个逻辑处理阶段,每个阶段都有相关的元素:分组阶段处理相关的分组或行元素,扩展阶段处理相关的扩展或列元素,聚合阶段处理相关的聚合元素和聚合函数.现在假设有一张表数据如下: 我现在需要查询出下面的结果: 需求分析:需要在结果中为每一个雇员生成一行记录,这就需要对Orders表中的行按照其empid列进行分组:从结果看,还需要为每一个客户生成一个不同的结果列,那么扩展元素就是custid列:最后还需要对数

Grouping Sets

Group by分组函数的自定义,与group by配合使用可更加灵活的对结果集进行分组,Grouping sets会对各个层级进行汇总,然后将各个层级的汇总值union all在一起,但却比单纯的group by + union all 效率要高 1 创建数据 CREATE TABLE employee ( name NVARCHAR2(10), gender NCHAR(1), country NVARCHAR2(10), department NVARCHAR2(10), salary N

Group By 多个分组集小结 --GROUPING SETS,GROUP BY CUBE,GROUP BY ROLLUP,GROUPING(),GROUPING_ID()

T-SQL 多个分组集共有三种 GROUPING SETS, CUBE, 以及ROLLUP, 其中 CUBE和ROLLUP可以当做是GROUPING SETS的简写版 示例数据库下载: http://files.cnblogs.com/files/haseo/TSQL2012.rar GROUPING SETS 列出所有你设置的分组集 SELECT shipperid, YEAR(shippeddate) AS shipyear, COUNT(*) AS numorders FROM Sales

TSql Grouping Sets

TSql Group by clause主要有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)) rollup 和cube的作用是预定义grouping sets(分组集),rollup(a,b) 预定义的分组集是(),(a),(a,b);cube(a,b)预定义的的分组集是(),(a),(b),(a,b). gr

T-Sql语法:GROUP BY子句GROUPING SETS、CUBE、ROLLUP

#cnblogs_post_body h1 { background-color: #A5A5A5; color: white; padding: 5px } GROUP BY子句 1.ROLLUP() 生成某一维度的分组的小计行,还生成一个总计行. 示例表: select * from student 我们来看一下具体示例: select sex,sclass,sum(score) from student group by rollup(sex,sclass) 如图中所示,ROLLUP()为

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

原文地址:http://blog.csdn.net/u014558001/article/details/42387929 本文主要讲解 ROLLUP, CUBE, GROUPING SETS的主要用法,这些函数可以理解为GroupBy分组函数封装后的精简用法,相当于多个union all 的组合显示效果,但是要比 多个union all的效率要高. 其实这些函数在时间的程序开发中应用的并不多,至少在我工作的多年时间中没用过几次,因为现在的各种开发工具/平台都自带了这些高级分组统计功能,使用的方

Oracle分组函数之Grouping Sets

功能介绍: 自定义分组的字段 创建表: 插入测试数据: Grouping Sets(null,t.classid,(t.classid,t.studentname)),类似于ROLLUP Select t.classid,t.studentname,Sum(t.score) From Score t Group By Grouping Sets(null,t.classid,(t.classid,t.studentname)); 查询结果: Grouping Sets(null,t.classi

Oracle分组小计、总计示例(grouping sets的使用)

1.首先创建一个表 Sql代码 [sql] view plain copy create table TE ( ID        VARCHAR2(2), T_CODE    VARCHAR2(4), T_NAME    VARCHAR2(4), T_AMOUNT  INTEGER, T_DEPT    VARCHAR2(4), T_PROJECT VARCHAR2(4), T_TYPE    VARCHAR2(1) ) 2.录入数据如下: Sql代码 [sql] view plain cop

GROUPING SETS、CUBE、ROLLUP

其实还是写一个Demo 比较好 USE tempdb IF OBJECT_ID( 'dbo.T1' , 'U' )IS NOT NULL BEGIN DROP TABLE dbo.T1; END; GO CREATE TABLE dbo.T1 ( id INT , productName VARCHAR(200) , price MONEY , num INT , amount INT , operatedate DATETIME ) GO DECLARE @i INT DECLARE @ran