SQL Server2008 程序设计 汇总 GROUP BY,WITH ROLLUP,WITH CUBE,GROUPING SETS(..)

--SQL Server2008 程序设计 汇总 GROUP BY ,WITH ROLLUP  WITH CUBE  GROUPING SET(..)

  • /********************************************************************************
  • *主题:SQL Server2008 程序设计 汇总 group by ,WITH ROLLUP  WITH CUBE
  • *说明:本文是个人学习的一些笔记和个人愚见
  • *      有很多地方你可能觉得有异议,欢迎一起讨论
  • *作者:Stephenzhou(阿蒙)
  • *日期: 2012.12.5
  • *Mail:[email protected]
  • *另外:转载请著名出处。
  • **********************************************************************************/
--SQL Server2008 程序设计 汇总 group by ,WITH ROLLUP  WITH CUBE

/********************************************************************************
 *主题:SQL Server2008 程序设计 汇总 group by ,WITH ROLLUP  WITH CUBE
 *说明:本文是个人学习的一些笔记和个人愚见
 *      有很多地方你可能觉得有异议,欢迎一起讨论          

 *作者:Stephenzhou(阿蒙)
 *日期: 2012.12.5  

 *Mail:[email protected]
 *另外:转载请著名出处。
**********************************************************************************/    

以下是测试数据

  1. IF OBJECT_ID(‘Inventory‘) is not null
  2. drop table Inventory
  3. go
  4. create table Inventory(
  5. Store varchar(2),
  6. Item varchar(20),
  7. Color varchar(10),
  8. Quantity decimal
  9. )
  10. insert into Inventory values(‘NY‘,‘Table‘,‘Blue‘,124)
  11. insert into Inventory values(‘NJ‘,‘Table‘,‘Blue‘,100)
  12. insert into Inventory values(‘NY‘,‘Table‘,‘Red‘,29)
  13. insert into Inventory values(‘NJ‘,‘Table‘,‘Red‘,56)
  14. insert into Inventory values(‘PA‘,‘Table‘,‘Red‘,138)
  15. insert into Inventory values(‘NY‘,‘Table‘,‘Green‘,229)
  16. insert into Inventory values(‘PA‘,‘Table‘,‘Green‘,304)
  17. insert into Inventory values(‘NY‘,‘Chair‘,‘Blue‘,101)
  18. insert into Inventory values(‘NJ‘,‘Chair‘,‘Blue‘,22)
  19. insert into Inventory values(‘NY‘,‘Chair‘,‘Red‘,21)
  20. insert into Inventory values(‘NJ‘,‘Chair‘,‘Red‘,10)
  21. insert into Inventory values(‘PA‘,‘Chair‘,‘Red‘,136)
  22. insert into Inventory values(‘NJ‘,‘Sofa‘,‘Green‘,2)
IF OBJECT_ID(‘Inventory‘) is not null
drop table Inventory
go
create table Inventory(
Store varchar(2),
Item varchar(20),
Color varchar(10),
Quantity decimal
)
insert into Inventory values(‘NY‘,‘Table‘,‘Blue‘,124)
insert into Inventory values(‘NJ‘,‘Table‘,‘Blue‘,100)
insert into Inventory values(‘NY‘,‘Table‘,‘Red‘,29)
insert into Inventory values(‘NJ‘,‘Table‘,‘Red‘,56)
insert into Inventory values(‘PA‘,‘Table‘,‘Red‘,138)
insert into Inventory values(‘NY‘,‘Table‘,‘Green‘,229)
insert into Inventory values(‘PA‘,‘Table‘,‘Green‘,304)
insert into Inventory values(‘NY‘,‘Chair‘,‘Blue‘,101)
insert into Inventory values(‘NJ‘,‘Chair‘,‘Blue‘,22)
insert into Inventory values(‘NY‘,‘Chair‘,‘Red‘,21)
insert into Inventory values(‘NJ‘,‘Chair‘,‘Red‘,10)
insert into Inventory values(‘PA‘,‘Chair‘,‘Red‘,136)
insert into Inventory values(‘NJ‘,‘Sofa‘,‘Green‘,2)

--一般的group by

  1. select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
  2. from Inventory
  3. group by  Item,Color
  4. order by Item,Color
  5. /*
  6. Item                 Color      TotalQuantity                           Stores
  7. -------------------- ---------- --------------------------------------- -----------
  8. Chair                Blue       123                                     2
  9. Chair                Red        167                                     3
  10. Sofa                 Green      2                                       1
  11. Table                Blue       224                                     2
  12. Table                Green      533                                     2
  13. Table                Red        223                                     3
  14. (6 行受影响)
  15. */
select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from Inventory
group by  Item,Color
order by Item,Color
/*
Item                 Color      TotalQuantity                           Stores
-------------------- ---------- --------------------------------------- -----------
Chair                Blue       123                                     2
Chair                Red        167                                     3
Sofa                 Green      2                                       1
Table                Blue       224                                     2
Table                Green      533                                     2
Table                Red        223                                     3

(6 行受影响)
*/

 GROUP BY   .. WITH ROLLUP

多了4个rollup行

  1. select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
  2. from Inventory
  3. group by  Item,Color WITH ROLLUP --group by rollup(item,color)
  4. order by Item,Color
  5. /*
  6. Item                 Color      TotalQuantity                           Stores
  7. -------------------- ---------- --------------------------------------- -----------
  8. NULL                 NULL       1272                                    13
  9. Chair                NULL       290                                     5
  10. Chair                Blue       123                                     2
  11. Chair                Red        167                                     3
  12. Sofa                 NULL       2                                       1
  13. Sofa                 Green      2                                       1
  14. Table                NULL       980                                     7
  15. Table                Blue       224                                     2
  16. Table                Green      533                                     2
  17. Table                Red        223                                     3
  18. (10 行受影响)
  19. */
 select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from Inventory
group by  Item,Color WITH ROLLUP --group by rollup(item,color)
order by Item,Color
/*
Item                 Color      TotalQuantity                           Stores
-------------------- ---------- --------------------------------------- -----------
NULL                 NULL       1272                                    13
Chair                NULL       290                                     5
Chair                Blue       123                                     2
Chair                Red        167                                     3
Sofa                 NULL       2                                       1
Sofa                 Green      2                                       1
Table                NULL       980                                     7
Table                Blue       224                                     2
Table                Green      533                                     2
Table                Red        223                                     3

(10 行受影响)
*/

WITH CUBE 多维数据集,多维数据集的纬度取决于分组列的数目

  1. select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
  2. from Inventory
  3. group by  Item,Color WITH cube --group by cube(item,color)
  4. order by Item,Color
  5. /*
  6. Item                 Color      TotalQuantity                           Stores
  7. -------------------- ---------- --------------------------------------- -----------
  8. NULL                 NULL       1272                                    13
  9. NULL                 Blue       347                                     4
  10. NULL                 Green      535                                     3
  11. NULL                 Red        390                                     6
  12. Chair                NULL       290                                     5
  13. Chair                Blue       123                                     2
  14. Chair                Red        167                                     3
  15. Sofa                 NULL       2                                       1
  16. Sofa                 Green      2                                       1
  17. Table                NULL       980                                     7
  18. Table                Blue       224                                     2
  19. Table                Green      533                                     2
  20. Table                Red        223                                     3
  21. (13 行受影响)
  22. */
 select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from Inventory
group by  Item,Color WITH cube --group by cube(item,color)
order by Item,Color

/*
Item                 Color      TotalQuantity                           Stores
-------------------- ---------- --------------------------------------- -----------
NULL                 NULL       1272                                    13
NULL                 Blue       347                                     4
NULL                 Green      535                                     3
NULL                 Red        390                                     6
Chair                NULL       290                                     5
Chair                Blue       123                                     2
Chair                Red        167                                     3
Sofa                 NULL       2                                       1
Sofa                 Green      2                                       1
Table                NULL       980                                     7
Table                Blue       224                                     2
Table                Green      533                                     2
Table                Red        223                                     3

(13 行受影响)

*/

GROUPING SETS(..) 仅返回最高级别

  1. select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
  2. from Inventory
  3. group by GROUPING sets(Item,Color)
  4. order by Item,Color
  5. /*
  6. Item                 Color      TotalQuantity                           Stores
  7. -------------------- ---------- --------------------------------------- -----------
  8. NULL                 Blue       347                                     4
  9. NULL                 Green      535                                     3
  10. NULL                 Red        390                                     6
  11. Chair                NULL       290                                     5
  12. Sofa                 NULL       2                                       1
  13. Table                NULL       980                                     7
  14. (6 行受影响)
  15. */
 select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from Inventory
group by GROUPING sets(Item,Color)
order by Item,Color

/*
Item                 Color      TotalQuantity                           Stores
-------------------- ---------- --------------------------------------- -----------
NULL                 Blue       347                                     4
NULL                 Green      535                                     3
NULL                 Red        390                                     6
Chair                NULL       290                                     5
Sofa                 NULL       2                                       1
Table                NULL       980                                     7

(6 行受影响)
*/
 

混合使用:

返回store最高级别和cube的两个item,color纬度所以级别组合

  1. select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
  2. from Inventory
  3. group by GROUPING sets(Store) ,cube(Item,color)
  4. order by Item,Color
  5. /*
  6. Item                 Color      TotalQuantity                           Stores
  7. -------------------- ---------- --------------------------------------- -----------
  8. NULL                 NULL       190                                     5
  9. NULL                 NULL       504                                     5
  10. NULL                 NULL       578                                     3
  11. NULL                 Blue       225                                     2
  12. NULL                 Blue       122                                     2
  13. NULL                 Green      2                                       1
  14. NULL                 Green      229                                     1
  15. NULL                 Green      304                                     1
  16. NULL                 Red        274                                     2
  17. NULL                 Red        66                                      2
  18. NULL                 Red        50                                      2
  19. Chair                NULL       32                                      2
  20. Chair                NULL       122                                     2
  21. Chair                NULL       136                                     1
  22. Chair                Blue       101                                     1
  23. Chair                Blue       22                                      1
  24. Chair                Red        10                                      1
  25. Chair                Red        21                                      1
  26. Chair                Red        136                                     1
  27. Sofa                 NULL       2                                       1
  28. Sofa                 Green      2                                       1
  29. Table                NULL       156                                     2
  30. Table                NULL       382                                     3
  31. Table                NULL       442                                     2
  32. Table                Blue       100                                     1
  33. Table                Blue       124                                     1
  34. Table                Green      229                                     1
  35. Table                Green      304                                     1
  36. Table                Red        29                                      1
  37. Table                Red        56                                      1
  38. Table                Red        138                                     1
  39. (31 行受影响)
  40. */

  select Item,Color,SUM(Quantity) as TotalQuantity,COUNT(Store) as Stores
from Inventory
group by GROUPING sets(Store) ,cube(Item,color)
order by Item,Color

/*
Item                 Color      TotalQuantity                           Stores
-------------------- ---------- --------------------------------------- -----------
NULL                 NULL       190                                     5
NULL                 NULL       504                                     5
NULL                 NULL       578                                     3
NULL                 Blue       225                                     2
NULL                 Blue       122                                     2
NULL                 Green      2                                       1
NULL                 Green      229                                     1
NULL                 Green      304                                     1
NULL                 Red        274                                     2
NULL                 Red        66                                      2
NULL                 Red        50                                      2
Chair                NULL       32                                      2
Chair                NULL       122                                     2
Chair                NULL       136                                     1
Chair                Blue       101                                     1
Chair                Blue       22                                      1
Chair                Red        10                                      1
Chair                Red        21                                      1
Chair                Red        136                                     1
Sofa                 NULL       2                                       1
Sofa                 Green      2                                       1
Table                NULL       156                                     2
Table                NULL       382                                     3
Table                NULL       442                                     2
Table                Blue       100                                     1
Table                Blue       124                                     1
Table                Green      229                                     1
Table                Green      304                                     1
Table                Red        29                                      1
Table                Red        56                                      1
Table                Red        138                                     1

(31 行受影响)
*/

转载请著名出处。  *博客地址:http://blog.csdn.net/szstephenzhou

时间: 2024-10-10 01:04:57

SQL Server2008 程序设计 汇总 GROUP BY,WITH ROLLUP,WITH CUBE,GROUPING SETS(..)的相关文章

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

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

高级聚合函数rollup(),cube(),grouping sets()

rollup(),cube(),grouping sets() 上面这几个函数,是对group by分组功能做的功能扩展. a.rollup() 功能:在原结果基础上追加一行总合计记录 rollup(字段1,字段2)会追加按字段1进行的合计记录, 最后再追加一个总合计记录 select deptno,count(*) from emp group by rollup(deptno); select deptno,job,count(*) from emp group by rollup(dept

SQL 中GROUP BY 、ROLLUP、CUBE 关系和区别

转自:http://www.cnblogs.com/dyufei/archive/2009/11/12/2573974.html 不言自明,看SQL就完全理解了,不需要过多解释,不错,分享之: ROLLUP 运算符生成的结果集类似于 CUBE 运算符生成的结果集. 下面是 CUBE 和 ROLLUP 之间的具体区别: CUBE 生成的结果集显示了所选列中值的所有组合的聚合. ROLLUP 生成的结果集显示了所选列中值的某一层次结构的聚合. ROLLUP 优点: (1)ROLLUP 返回单个结果集

SQL Server2008宝典 全书代码

3.3.7 -- ============================================= -- Create database template -- ============================================= USE master GO -- Drop the database if it already exists IF EXISTS ( SELECT name FROM sys.databases WHERE name = N'test

Oracle中group by 的扩展函数rollup、cube、grouping sets

Oracle的group by除了基本用法以外,还有3种扩展用法,分别是rollup.cube.grouping sets,分别介绍如下: 1.rollup 对数据库表emp,假设其中两个字段名为a,b,c. 如果使用group by rollup(a,b),首先会对(a,b)进行group by ,然后对 a 进行 group by ,最后对全表进行 group by 操作. 如下查询结果: 查询语句 Select deptno,job,sum(sal) from emp group by r

【转】rollup、cub、grouping sets、grouping、grouping_id在报表中的应用

摘自 http://blog.itpub.net/26977915/viewspace-734114/ 在报表语句中经常要使用各种分组汇总,rollup和cube就是常用的分组汇总方式. 第一:group by rollup 1.如果使用诸如group by rollup(A,B,C)的方式分组,那么返回的分组结果是(A,B,C) (A,B) (A) (NULL) 一共四种结果.即从右到左递减,最后来个合计. 例如: SQL> select * from t; YEARS     MONTHS

SQL Server 之 GROUP BY、GROUPING SETS、ROLLUP、CUBE

原文:SQL Server 之 GROUP BY.GROUPING SETS.ROLLUP.CUBE 1.创建表 Staff CREATE TABLE [dbo].[Staff]( [ID] [int] IDENTITY(1,1) NOT NULL, [Name] [varchar](50) NULL, [Sex] [varchar](50) NULL, [Department] [varchar](50) NULL, [Money] [int] NULL, [CreateDate] [date

64位 SQL Server2008链接访问Oracle 过程汇总解决方法记录

64位 SQL Server2008链接访问Oracle 过程汇总解决方法记录 经过几天不停的网上找资料,实验,终于联通了. 环境:系统:win 2008 ,SqlServer2008 R2, 连接Oracle10g 在SqlServer2008 R2机器上需要安装Oracle客户端32位和64位两个,然后配置连接别名.设置注册表.详细情况见下面 1.错误1 ------------------------------------------------ 64位机器上建立OLE_DB链接报错 报

Oralce 高级 sql 之 rollup 与 cube

在 oracle sql 查询中,oracle 提供了一些高级功能能够简化 sql 查询语句以及提高查询性能.下面将介绍 rollup,cube的用法与区别. 1. rollup rollup 能够在 select 语句中计算指定分组字段的多个层次的小计以及合计. rollup 非常容易使用并且非常高效. rollup 从右到左扫描分组字段, 逐步创建更高级别的小计. 最后再创建一行总计. rollup 将创建 n + 1 个层次(级别)的小计, n 指的是 rollup 中的字段数. 1.1