码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2015-01-17 22:15:14      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

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

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

以下是测试数据

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

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

标签:

原文地址:http://www.cnblogs.com/blog-Aevin/p/4231090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!