group by <grouping sets(...) ><cube(...)>
GROUP BY
GROUPING SETS()
后面将还会写学习 with cube, with rollup,以及将它们转换为标准的GROUP BY的子句GROUP SET(), CUBE的用法(虽然MSSQL以后将会去掉)
这里面的大小写混淆来写不规范,以后还是习惯使用大写吧,有时是为节省宽度空间用小写,标准还是用大写SQL的关键字
--group by 子句中的cube, rollup, --这样理解: cube立方(既然是立方,就是变多了), rollup卷起,包起来(按字面意思理解) if object_id('dbo.orders','U') is not null drop table dbo.orders; GO CREATE TABLE dbo.Orders ( orderid INT NOT NULL, orderdate DATE NOT NULL, empid INT NOT NULL, custid VARCHAR(5) NOT NULL, qty INT NOT NULL, CONSTRAINT PK_Orders PRIMARY KEY(orderid) ); GO INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty) VALUES (30001, '20070802', 3, 'A', 10), (10001, '20071224', 2, 'A', 12), (10005, '20071224', 1, 'B', 20), (40001, '20080109', 2, 'A', 40), (10006, '20080118', 1, 'C', 14), (20001, '20080212', 2, 'B', 12), (40005, '20090212', 3, 'A', 10), (20002, '20090216', 1, 'C', 20), (30003, '20090418', 2, 'B', 15), (30004, '20070418', 3, 'C', 22), -------------------------------------------------------------------------------- --下面单个SELECT...是可以查询的,用Union联合是不可以的 , 回顾一下 union all的用法 --使用union需要3个条件: 1. 字段列数、字段列序相同 2.类型兼容 3.查询结果集中的字段名称以第1个查询结果集中的字段名称为名 select empid, sum(qty) as SumQty from Orders group by empid union ---下面3处union,并不准备联合,放此处,只是为再熟悉 select custid, sum(qty) as SumQty from Orders group by custid union select empid,custid,sum(qty) as SumQty from Orders group by empid,custid union select sum(qty) as SumQty from Orders --select * from Orders -------------------------------------------------------------------------------- --Union All 集合运行要求所有的结果集包含相同的列数,所以补充了空列(null占位) select empid, null, sum(qty) as SumQty --select empid as empidA, null, sum(qty) as SumQty , 联合起来后的结果集将显示empidA from Orders group by empid union all select null, custid, sum(qty) as SumQty from Orders group by custid union all select empid,custid,sum(qty) as SumQty --select empid as empidB, null, sum(qty) as SumQty,即使有empidB,也会显示为empidA from Orders group by empid,custid union all select null, null, sum(qty) as SumQty from Orders -------------------------------------------------------------------------------- ...... union去重, union all 不去重,以后再补充此处 -------------------------------------------------------------------------------- --上面几个Union All连接起来,结果集等价于下面的 select empid,custid,sum(qty) as SumQty from Orders group by ------------------------------------此处group by 的子句 grouping sets Grouping sets ( (empid,custid), (empid), (custid), () ); -------------------------------------------------------------------------------- -- 上面的写法,等价于: 在group by 子句中加上 cube(empid,custid),它代表了这2种情况的任意组合,标准SQL) -- cube(a,b) <=== >包括 GROUPING SETS((a,b),(a),(b),() ) select empid,custid,sum(qty) as SumQty from Orders group by cube(empid,custid) ------------------------------------此处group by 的子句 cube -------------------------------------------------------------------------------- --旧式的写法,只是有group by ... 分组,但不是作为 group by 的子句,非标准的SQL写法(以后MSSQL会去掉这种写法) select empid,custid,sum(qty) as SumQty from Orders group by empid,custid with cube -------------------------------------------------------------------------------- --ROLLUP,与CUBE对应(认为生成所有可能的结果集),而ROLLUP(只生成单个的结果集),认为成员之间有a>b>c的层次关系 --如grouping sets((a,b,c), (a), (b), ()) <===>rollup((a), (b), (c)) select year(orderdate) as OrderYear, month(orderdate) as OrderMonty, day(orderdate) as OrderDay, sum(qty) as SumQty from Orders group by rollup(year(orderdate),month(orderdate),day(orderdate)) /* 参考:《SQL Server 2008宝典_第2版_刘智勇_刘径舟_编著》,有实体书,讲解还是没有老外的书细致,老外的书会有个来龙去脉
《Microsoft SQL Server 2008技术内幕 T-SQL语言基础》 《Microsoft SQL Server 2012 T-SQL Fundamentals》 */ -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- --------------------------------------------------------------------------------
OnionYang@