博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL Server2008 程序设计 汇总 GROUP BY,WITH ROLLUP,WITH CUBE,GROUPING SETS(..)
阅读量:6710 次
发布时间:2019-06-25

本文共 11762 字,大约阅读时间需要 39 分钟。

--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 nulldrop table Inventorygocreate 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                                     2Chair                Red        167                                     3Sofa                 Green      2                                       1Table                Blue       224                                     2Table                Green      533                                     2Table                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                                    13Chair                NULL       290                                     5Chair                Blue       123                                     2Chair                Red        167                                     3Sofa                 NULL       2                                       1Sofa                 Green      2                                       1Table                NULL       980                                     7Table                Blue       224                                     2Table                Green      533                                     2Table                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                                    13NULL                 Blue       347                                     4NULL                 Green      535                                     3NULL                 Red        390                                     6Chair                NULL       290                                     5Chair                Blue       123                                     2Chair                Red        167                                     3Sofa                 NULL       2                                       1Sofa                 Green      2                                       1Table                NULL       980                                     7Table                Blue       224                                     2Table                Green      533                                     2Table                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                                     4NULL                 Green      535                                     3NULL                 Red        390                                     6Chair                NULL       290                                     5Sofa                 NULL       2                                       1Table                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                                     5NULL                 NULL       504                                     5NULL                 NULL       578                                     3NULL                 Blue       225                                     2NULL                 Blue       122                                     2NULL                 Green      2                                       1NULL                 Green      229                                     1NULL                 Green      304                                     1NULL                 Red        274                                     2NULL                 Red        66                                      2NULL                 Red        50                                      2Chair                NULL       32                                      2Chair                NULL       122                                     2Chair                NULL       136                                     1Chair                Blue       101                                     1Chair                Blue       22                                      1Chair                Red        10                                      1Chair                Red        21                                      1Chair                Red        136                                     1Sofa                 NULL       2                                       1Sofa                 Green      2                                       1Table                NULL       156                                     2Table                NULL       382                                     3Table                NULL       442                                     2Table                Blue       100                                     1Table                Blue       124                                     1Table                Green      229                                     1Table                Green      304                                     1Table                Red        29                                      1Table                Red        56                                      1Table                Red        138                                     1(31 行受影响)*/

转载请著名出处。  *博客地址:

转载于:https://www.cnblogs.com/blog-Aevin/p/4231090.html

你可能感兴趣的文章
使Android开发方便快捷的8个好工具
查看>>
递归与非递归遍历
查看>>
Nagios图像绘制插件PNP4Nagios部署和测试
查看>>
在SqlServer2008R2中,在一张表上加上insert、update、delete触发器(带游标)
查看>>
常用模块--- 正则模块 正则表达式 re 模块
查看>>
图解aclocal、autoconf、automake、autoheader、configure
查看>>
chapter 17
查看>>
C/C++ cast
查看>>
jfinal的controller默认访问的方法是什么
查看>>
Punycode
查看>>
HTML LIST 输入框自动查询追加框,自动过滤 HTML5
查看>>
file_get_contents调用接口出现的错误
查看>>
SQL Server 2008 调试存储过程(调用用户定义表类型)
查看>>
文件隐藏在一张图片里
查看>>
学c++需要先学c语言吗?
查看>>
ubuntu apt 安装 mpv
查看>>
内部类
查看>>
UNIX网络编程——Socket通信原理和实践
查看>>
fedora安装vim失败的解决
查看>>
【转】MySQL用户管理及SQL语句详解
查看>>