oracle 高级分组

10.高级分组
本章目标:
对于增强的group by需要掌握:
1.使用rollup(也就是roll up累计的意思)操作产生subtotal(小计)的值。
2.使用cube操作产生cross-tabulation(列联交叉表)的值。
3.使用grouping函数标识通过rollup和cube建立的行的值。
4.使用grouping sets产生一个single result set(结果集)。
5.使用grouping_id和group_id函数。
关键字:rollup(累计,累加),cube(交叉),subtotal(小计),cross-tabulation(列联交叉表,交叉列表)。

背景知识:
我们知道,通过group by指定列,可以求出按照指定的列一次性统计组的信息,比如求sum,min,max,avg等。然而在实际应用中,比如数据仓库中,我们需要对数据提供多维分析,对每个维度分析得到汇总数据,提供多角度的数据分析支持。那么单纯使用group by就很难达到这种目标,当然,我们可以使用union all来将多个维度的分析数据汇总,但是在性能上来说,性能就很低了。所以oracle在8i的时候,提供了增强的group by的一系列特性。本章就专门研究一下这些特性。
注:
对于分组的group by,rollup,cube,grouping sets后面的列不必要都出现在查询列中,但是不可出现没有分组的列,这符合SQL的语义要求。
数据准备:
10.1 多维汇总
        首先,有一需求:根据all_orders表和region表,计算对应区域每个月的销售额汇总。这个需求很简单,只要通过group by就可以实现,如下:
       SELECT r.name region, 
       TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month, SUM(o.tot_sales)
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
GROUP BY r.name, o.month; 
显示数据如下:
REGION                            MONTH            SUM(O.TOT_SALES)
---------------------------------------- ---------------- ---------------------------------------------------
Mid-Atlantic                             5月                       1778805
Mid-Atlantic                             8月                       1381560
Mid-Atlantic                             9月                       1178694
Southeast US                             10月                      1610523
Southeast US                             2月                       1855269
Southeast US                             3月                       1967979
Mid-Atlantic                             10月                      1530351
New England                              8月                       1642968
New England                              11月                      1384185
New England                              12月                      1599942
Southeast US                             11月                      1661598
Southeast US                             12月                      1841100
Southeast US                             6月                       1705716
New England                              7月                       1678002
Mid-Atlantic                             4月                       1623438
Mid-Atlantic                             11月                      1598667
New England                              5月                       1698855
Southeast US                             8月                       1436295
New England                              6月                       1510062
New England                              9月                       1726767
Southeast US                             4月                       1830051
Mid-Atlantic                             2月                       1286028
Mid-Atlantic                             7月                       1820742
New England                              1月                       1527645
New England                              3月                       1699449
New England                              10月                      1648944
Southeast US                             1月                       1137063
Southeast US                             5月                       1983282
Southeast US                             7月                       1670976
Mid-Atlantic                             1月                       1832091
Mid-Atlantic                             12月                      1477374
Southeast US                             9月                       1905633
Mid-Atlantic                             3月                       1911093
Mid-Atlantic                             6月                       1504455
New England                              2月                       1847238
New England                              4月                       1792866
        


上面的语句很容易实现对应区域每个月的销售额的汇总统计,但是更复杂的需求,我们可能要对跨越所有月份的每个区域单独汇总,生成小计,并且加上所有区域的汇总或者对应每个月跨越所有区域,生成小计,并且加上所有月的汇总统计,简而言之,也就是要实现多级别小计和总计的统计。实现这个需求,简单的group by就无法实现了,下面逐步研究实现的方法。
10.1.1 UNION
        在一个数据仓库应用中,经常需要生成多维度的汇总数据,小计和合计就是跨越多个维度的。生成小计和合计是数据仓库应用的一个核心内容。
        我们已经意识到,使用简单的group by并不能实现上述汇总查询后,再按照相关列计算小计和合计。那么我们可以使用group by来计算上面我们提到的小计和合计,然后采用union连接相关结果,获得我们想要的答案。

1.        实现对每个区域按月汇总的数据。
2.        实现对每个区域所有月份的小计。
3.        实现对所有区域所有月份的总计。
分别使用group by实现上述需求,然后union结果。当然这是一种实现方式,在Oracle8i之前,只有这种方式才能实现这种需求,后面我们将要说这种方式的缺点。
--对每个区域按月分组
SELECT r.name region, 
       TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month, SUM(o.tot_sales)
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
GROUP BY r.name, o.month
UNION ALL
--对每个区域的所有月分组小计,因此,月份为NULL
SELECT r.name region, NULL, SUM(o.tot_sales)
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
GROUP BY r.name
UNION ALL
--对所有区域所有月份总计,因此月份和区域都为NULL
SELECT NULL, NULL, SUM(o.tot_sales)
FROM all_orders o JOIN region r
ON r.region_id = o.region_id;

REGION                               MONTH            SUM(O.TOT_SALES)
---------------------------------------- ---------------- ---------------------------------------------------------
New England                              1月                       1527645
New England                              2月                       1847238
New England                              3月                       1699449
New England                              4月                       1792866
New England                              5月                       1698855
New England                              6月                       1510062
New England                              7月                       1678002
New England                              8月                       1642968
New England                              9月                       1726767
New England                              10月                      1648944
New England                              11月                      1384185
New England                              12月                      1599942
Mid-Atlantic                             1月                       1832091
Mid-Atlantic                             2月                       1286028
Mid-Atlantic                             3月                       1911093

Mid-Atlantic                             4月                       1623438
Mid-Atlantic                             5月                       1778805
Mid-Atlantic                             6月                       1504455
Mid-Atlantic                             7月                       1820742
Mid-Atlantic                             8月                       1381560
Mid-Atlantic                             9月                       1178694
Mid-Atlantic                             10月                      1530351
Mid-Atlantic                             11月                      1598667
Mid-Atlantic                             12月                      1477374
Southeast US                             1月                       1137063
Southeast US                             2月                       1855269
Southeast US                             3月                       1967979
Southeast US                             4月                       1830051
Southeast US                             5月                       1983282
Southeast US                             6月                       1705716
Southeast US                             7月                       1670976
Southeast US                             8月                       1436295
Southeast US                             9月                       1905633
Southeast US                             10月                      1610523
Southeast US                             11月                      1661598
Southeast US                             12月                      1841100
Mid-Atlantic                                                      18923298
New England                                                       19756923
Southeast US                                                      20605485
                                                                  59285706        

分析这个结果可以看出,比如Mid_Atlantic的小计数据,就是上面Mid_Atlantic按月分组的明细数据的再次汇总。最后的59285706就是所有按区域和月份分组的明细的汇总,也等于上面按区域汇总的小计的和。
其实,现在可以看出,如果有一个分组函数,能够对指定列的全部分组,然后能够对指定列逐渐减少分组,直到所有的列排列完成,比如上面的区域名,月份分组。如果能实现分组统计:
区域名,月份
区域名                  //对应区域的小计
全部汇总                //所有区域所有月份的合计
那么我们的问题就能很简单地解决了,Oracle引入了RollUp,专门就是解决这个问题。

下面回到上面用Union实现这个功能上来,分析它的缺点,首先看下这个查询的执行计划(环境不同,计划可能不同):
PLAN_TABLE_OUTPUT

-----------------------------------------------------

| Id | Operation                  | Name            |

-----------------------------------------------------

|  0 | SELECT STATEMENT           |                 |

|  1 | UNION-ALL                  |                 |

|  2 | SORT GROUP BY              |                 |

|  3 | MERGE JOIN                 |                 |

|  4 | TABLE ACCESS BY INDEX ROWID| REGION          |

|  5 | INDEX FULL SCAN            | REGION_PK       |

|* 6 | SORT JOIN                  |                 |

|  7 | TABLE ACCESS FULL          | ALL_ORDERS      |

|  8 | SORT GROUP BY              |                 |

|  9 | MERGE JOIN                 |                 |

|  10| TABLE ACCESS BY INDEX ROWID| REGION          |

|  11| INDEX FULL SCAN            | REGION_PK       |

|* 12| SORT JOIN                  |                 |

|  13| TABLE ACCESS FULL          | ALL_ORDERS      |

|  14| SORT AGGREGATE             |                 |

|  15| NESTED LOOPS               |                 |

|  16| TABLE ACCESS FULL          | ALL_ORDERS      |

|* 17| INDEX UNIQUE SCAN          | REGION_PK       |


        分析执行计划,得出oracle需要做下列动作才能完成这个查询:
Three FULL TABLE scans on all_orders
Three INDEX scan on region_pk (Primary key of table region)
Two Sort-Merge Joins
One NESTED LOOPS JOIN
Two SORT GROUP BY operations
One SORT AGGREGATE operation
One UNION ALL
可以知道,性能是非常差的,我们的原始表all_orders和region很小,实际上一般最少有几百万条,而且我们的分组汇总列很少,如果很多,还要写更多的union,性能很不好,为了解决这个问题,请看下节ROLLUP。
10.1.2 ROLLUP
        从Oracle8i开始,oracle提供了很多在一个查询中生成多级别汇总数据的特性,第10章我们研究的是对group by的扩展,关于扩展内容和版本对应关系如下表:
        
功能        版本
ROLLUP        oracle8i
CUBE        oracle8i
GROUPING SETS        oracle9i

        本节研究rollup,对cube和grouping sets稍后介绍。ROLLUP是对group by的扩展,因此,它只能出现在group by子句中,依赖于分组的列,对每个分组会生成汇总数据,如下:
        SELECT ….
FROM ….
GROUP BY ROLLUP(C1,C2,C3….C(n-1),C(n));
总共会进行n+1个分组。
        那么实际上有n+1个group by的union all结果。
第1个分组:全分组。C1,C2,C3….C(n-1),C(n)
第2个分组:C1,C2,C3….C(n-1);//这个分组实际上就是对前面前n-1列分组的小计.
----然后逐渐递减分组列
第n个分组:C1。对上一个分组的小计。
第n+1个分组。不分组全量汇总。相当于合计。也是对group by C1的小计。相当于group by null。
有了rollup,对10.1.1实现的union多级别汇总就可以使用rollup实现,如下:
SELECT r.name region, 
TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month, SUM(o.tot_sales)
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
GROUP BY ROLLUP (r.name, o.month);
REGION                                   MONTH            SUM(O.TOT_SALES)
---------------------------------------- ---------------- ----------------
New England                              1月                       1527645
New England                              2月                       1847238
New England                              3月                       1699449
New England                              4月                       1792866
New England                              5月                       1698855
New England                              6月                       1510062
New England                              7月                       1678002
New England                              8月                       1642968
New England                              9月                       1726767
New England                              10月                      1648944
New England                              11月                      1384185
New England                              12月                      1599942
New England                                                       19756923
Mid-Atlantic                             1月                       1832091
Mid-Atlantic                             2月                       1286028
Mid-Atlantic                             3月                       1911093
Mid-Atlantic                             4月                       1623438
Mid-Atlantic                             5月                       1778805
Mid-Atlantic                             6月                       1504455
Mid-Atlantic                             7月                       1820742
Mid-Atlantic                             8月                       1381560
Mid-Atlantic                             9月                       1178694
Mid-Atlantic                             10月                      1530351
Mid-Atlantic                             11月                      1598667
Mid-Atlantic                             12月                      1477374
Mid-Atlantic                                                      18923298
Southeast US                             1月                       1137063
Southeast US                             2月                       1855269
Southeast US                             3月                       1967979
Southeast US                             4月                       1830051
Southeast US                             5月                       1983282
Southeast US                             6月                       1705716
Southeast US                             7月                       1670976
Southeast US                             8月                       1436295
Southeast US                             9月                       1905633
Southeast US                             10月                      1610523
Southeast US                             11月                      1661598
Southeast US                             12月                      1841100
Southeast US                                                      20605485
                                                                  59285706

从上面的结果可以看出,rollup和group by联合一起使用,达到了按group by列顺序分组,并且实现小计和合计的功能。黄色部分就是对上面组的小计,最后红色的就是合计。而且,rollup这种分组还是有序的,先全部分组,然后对每个分组小计,最后合计,使处理结果更清楚。union all就很难实现这种效果了。而且最重要的是性能比union all好,通过上面查询的执行计划可以看出:
         
rollup仅仅一个索引扫描region,一个全表扫描all_orders,然后通过nested loop,最后sort group by rollup就能得到结果。比union all多次扫描效率高很多。

rollup改变列的顺序,可以轻松改变统计结果,上面的查询小计是对每个区域的所有月小计,我们可以改变列的顺序达到对每个月份的所有区域小计,如下:
SELECT r.name region, 
       TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month, SUM(o.tot_sales)
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
GROUP BY ROLLUP ( o.month,r.name);
REGION                                MONTH            SUM(O.TOT_SALES)
---------------------------------------- ---------------- ----------------
New England                             1月                       1527645
Mid-Atlantic                             1月                       1832091
Southeast US                             1月                       1137063
                                        1月                       4496799
New England                             2月                       1847238
Mid-Atlantic                              2月                       1286028
Southeast US                              2月                       1855269
                                         2月                       4988535
New England                              3月                       1699449
Mid-Atlantic                             3月                       1911093
Southeast US                             3月                       1967979
                                         3月                       5578521
New England                              4月                       1792866
Mid-Atlantic                             4月                       1623438
Southeast US                             4月                       1830051
                                         4月                       5246355
New England                              5月                       1698855
Mid-Atlantic                             5月                       1778805
Southeast US                             5月                       1983282
                                         5月                       5460942
New England                              6月                       1510062
Mid-Atlantic                             6月                       1504455
Southeast US                             6月                       1705716
                                         6月                       4720233
New England                              7月                       1678002
Mid-Atlantic                             7月                       1820742
Southeast US                             7月                       1670976
                                         7月                       5169720
New England                              8月                       1642968
Mid-Atlantic                             8月                       1381560
Southeast US                             8月                       1436295
                                         8月                       4460823
New England                              9月                       1726767
Mid-Atlantic                             9月                       1178694
Southeast US                             9月                       1905633
                                         9月                       4811094
New England                              10月                      1648944
Mid-Atlantic                             10月                      1530351
Southeast US                             10月                      1610523
                                         10月                      4789818
New England                              11月                      1384185
Mid-Atlantic                             11月                      1598667
Southeast US                             11月                      1661598
                                         11月                      4644450
New England                              12月                      1599942
Mid-Atlantic                             12月                      1477374
Southeast US                             12月                      1841100
                                         12月                      4918416
                                                                  59285706
可以看出,达到了按月小计的功能,当然最后红色的合计值和按区域小计一致。
注意:
rollup中列的顺序不同,则统计的结果不同。因为它是按列从右递减分组的。
比如,我们需要统计第1个季度,并且年月汇总所有区域的数据,如下:
SELECT o.year, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month,
r.name region, SUM(o.tot_sales) sums
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
WHERE o.month BETWEEN 1 AND 3
GROUP BY ROLLUP (o.year, o.month, r.name);

YEAR MONTH         REGION                                         SUMS
--------- ---------------- ---------------------------------------- ---------------------------------------------
     2000 1月              New England                                 1018430
     2000 1月              Mid-Atlantic                                1221394
     2000 1月              Southeast US                                 758042
     2000 1月                                                          2997866
     2000 2月              New England                                 1231492
     2000 2月              Mid-Atlantic                                 857352
     2000 2月              Southeast US                                1236846
     2000 2月                                                          3325690
     2000 3月              New England                                 1132966
     2000 3月              Mid-Atlantic                                1274062
     2000 3月              Southeast US                                1311986
     2000 3月                                                          3719014
     2000                                                             10042570
     2001 1月              New England                                  509215
     2001 1月              Mid-Atlantic                                 610697
     2001 1月              Southeast US                                 379021
     2001 1月                                                          1498933
     2001 2月              New England                                  615746
     2001 2月              Mid-Atlantic                                 428676
     2001 2月              Southeast US                                 618423
     2001 2月                                                          1662845
     2001 3月              New England                                  566483
     2001 3月              Mid-Atlantic                                 637031
     2001 3月              Southeast US                                 655993
     2001 3月                                                          1859507
     2001                                                              5021285
                                                                      15063855
可以看出,只要在rollup中增加或改变相关列的顺序,就能达到我们想要的小计和合计功能。
10.1.3部分ROLLUP
以上使用的rollup是完全的rollup查询,比如有n列,那么会生成n-1个小计,然后n-2个小计对n-1个,直到汇总合计。如果在实际查询中,有的小计或合计我们不需要,那么久要使用局部rollup,局部rollup就是将不需要小计(合计)的列放在group by中,而不是放在rollup中。
请看下面的查询:
SELECT o.year, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month,
r.name region, SUM(o.tot_sales)
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
WHERE o.month BETWEEN 1 AND 3
GROUP BY o.year, ROLLUP (o.month, r.name);
这个相当于GROUP BY ROLLUP (o.year,o.month, r.name) 但是去掉了最后一行的汇总。
因为每次分组都有year。没有group by null。
再看这个查询:
SELECT o.year, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month,
r.name region, SUM(o.tot_sales)
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
WHERE o.month BETWEEN 1 AND 3
GROUP BY o.year, o.month,ROLLUP (r.name);
这个相当于GROUP BY ROLLUP (o.year,o.month, r.name)去掉了对指定年的汇总和全量汇总。因为每次都有对year和month的分组,小计只有对年和月的小计,如下:

      YEAR MONTH        REGION                            SUM(o.tot_sales)
---------- ---------------- ---------------------------------------- ----------------------------------------------
      2000 1月              New England                                 1018430
      2000 1月              Mid-Atlantic                                1221394
      2000 1月              Southeast US                                 758042
      2000 1月                                                          2997866
      2000 2月              New England                                 1231492
      2000 2月              Mid-Atlantic                                 857352
      2000 2月              Southeast US                                1236846
      2000 2月                                                          3325690
      2000 3月              New England                                 1132966
      2000 3月              Mid-Atlantic                                1274062
      2000 3月              Southeast US                                1311986
      2000 3月                                                          3719014
      2001 1月              New England                                  509215
      2001 1月              Mid-Atlantic                                 610697
      2001 1月              Southeast US                                 379021
      2001 1月                                                          1498933
      2001 2月              New England                                  615746
      2001 2月              Mid-Atlantic                                 428676
      2001 2月              Southeast US                                 618423
      2001 2月                                                          1662845
      2001 3月              New England                                  566483
      2001 3月              Mid-Atlantic                                 637031
      2001 3月              Southeast US                                 655993
      2001 3月                                                          1859507



10.1.4 CUBE
CUBE(交叉列表)也是对group by运算的一种扩展,它比rollup扩展更加精细,组合类型更多,rollup是按组合的列从右到左递减分组计算,而CUBE则是对所有可能的组合情况进行分组,这样分组的情况更多,覆盖所有的可能分组,并计算所有可能的分组的小计。比如:
CUBE(C1,C2,C3……C(N))对N个列进行CUBE分组,那么可能的分组情况有:
不分组:C(n,0)
取一列分组:C(n,1)
-----
取N列分组,全分组:C(n,n)
那么运用数学上的组合公式,得出所有所有可能的组合方式有:C(n,0)+C(n,1)+….+C(n,n)=2^n种。
我们以前面的rollup组合列为例子:rollup(name,month)是计算按区域名和月份分组以及每个区域的所有月份的小计以及总计。但是使用cube(name,month)则有4种分组,比rollup多一个每个月的所有区域的小计。下面比较一下这两种分组方式:

分组公式        描述
rollup(name,month)        分组情况有:
group by name,month
group by name,null  //每个区域所有月份小计
group by null,null  //合计
cube(name,month)        分组情况有:
group by null,null  //总计
group by null,month //每个月份的所有区域小计
group by name,null //每个区域的所有月份小计
group by name,month
      
CUBE使用方式:
和rollup一样,是
select …
from …
group by cube(分组列列表)
请看上面表格中的分组例子:
SELECT r.name region, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month, 
SUM(o.tot_sales) sums
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
GROUP BY CUBE(r.name, o.month)

REGION                                   MONTH                  SUMS
---------------------------------------- ---------------- ---------------------------------------------------
                                                            59285706
                                         1月                 4496799
                                         2月                 4988535
                                         3月                 5578521
                                         4月                 5246355
                                         5月                 5460942
                                         6月                 4720233
                                         7月                 5169720
                                         8月                 4460823
                                         9月                 4811094
                                         10月                4789818
                                         11月                4644450
                                         12月                4918416
New England                                                 19756923
New England                              1月                 1527645
New England                              2月                 1847238
New England                              3月                 1699449
New England                              4月                 1792866
New England                              5月                 1698855
New England                              6月                 1510062
New England                              7月                 1678002
New England                              8月                 1642968
New England                              9月                 1726767
New England                              10月                1648944
New England                              11月                1384185
New England                              12月                1599942
Mid-Atlantic                                                18923298
Mid-Atlantic                             1月                 1832091
Mid-Atlantic                             2月                 1286028
Mid-Atlantic                             3月                 1911093
Mid-Atlantic                             4月                 1623438
Mid-Atlantic                             5月                 1778805
Mid-Atlantic                             6月                 1504455
Mid-Atlantic                             7月                 1820742
Mid-Atlantic                             8月                 1381560
Mid-Atlantic                             9月                 1178694
Mid-Atlantic                             10月                1530351
Mid-Atlantic                             11月                1598667
Mid-Atlantic                             12月                1477374
Southeast US                                                20605485
Southeast US                             1月                 1137063
Southeast US                             2月                 1855269
Southeast US                             3月                 1967979
Southeast US                             4月                 1830051
Southeast US                             5月                 1983282
Southeast US                             6月                 1705716
Southeast US                             7月                 1670976
Southeast US                             8月                 1436295
Southeast US                             9月                 1905633
Southeast US                             10月                1610523
Southeast US                             11月                1661598
Southeast US                             12月                1841100

从上面结果可以看出,红色部分为group by null,null的结果,计算合计值。黄色部分为group by null,month的结果,计算每个月中所有区域的小计。绿色为group by name,null的结果,计算每个区域所有月份的小计。灰色的为group by name,month的结果。其中:
group by name,month的所有结果的和=group by name,null的和=group by null,month的和=合计值。
group by name,month中对应name的和=group by name,null对应的name的值。
group by name,month中对应month的和=group by null,month对应month的和。


当然对于cube的运算,和rollup一样,也可以使用union实现,但是cube的组合方式呈级数增长,则union也会增长,而且性能不好,访问表多次,无cube的优化,语句没有cube简单。上例我们可以使用对应的4个group by 然后union all获得结果,但是结果的顺序不能保证。
SELECT NULL region, NULL month, SUM(o.tot_sales)
FROM all_orders o JOIN region r
ON r.region_id = o.region_id 
UNION ALL
SELECT NULL, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month, SUM(o.tot_sales)
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
GROUP BY o.month
UNION ALL
SELECT r.name region, NULL, SUM(o.tot_sales)
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
GROUP BY r.name
UNION ALL
SELECT r.name region, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month, 
SUM(o.tot_sales)
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
GROUP BY r.name, o.month;

看下union的执行计划:
只需要访问region和all_orders一次,而且有专门的GENERATE CUBE计算,提高效率,保证执行结果的有序性。
实际上,有上面对cube的分析可以得出,那个cube语句实际上等价于下列rollup语句:
SELECT r.name region, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month, 
SUM(o.tot_sales)
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
GROUP BY rollup(r.name, o.month)
union 
SELECT r.name region, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month, 
SUM(o.tot_sales)
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
GROUP BY rollup( o.month,r.name);
不可以使用union all,因为有重复数据。


对于CUBE来说,列的名字只要一样,那么顺序无所谓,结果都是一样的,因为cube是各种可能情况的组合,只不过统计的结果顺序不同而已。但是对于rollup来说,列的顺序不同,则结果不同,详细见rollup。

        对本章的cube例子,改写cube的顺序,如下:
SELECT r.name region, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month, 
SUM(o.tot_sales) sums
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
GROUP BY CUBE(o.month, r.name);
REGION                                   MONTH                  SUMS
---------------------------------------- ---------------- ----------------------------------------------------
                                                            59285706
New England                                                 19756923
Mid-Atlantic                                                18923298
Southeast US                                                20605485
                                         1月                 4496799
New England                              1月                 1527645
Mid-Atlantic                             1月                 1832091
Southeast US                             1月                 1137063
                                         2月                 4988535
New England                              2月                 1847238
Mid-Atlantic                             2月                 1286028
Southeast US                             2月                 1855269
                                         3月                 5578521
New England                              3月                 1699449
Mid-Atlantic                             3月                 1911093
Southeast US                             3月                 1967979
                                         4月                 5246355
New England                              4月                 1792866
Mid-Atlantic                             4月                 1623438
Southeast US                             4月                 1830051
                                         5月                 5460942
New England                              5月                 1698855
Mid-Atlantic                             5月                 1778805
Southeast US                             5月                 1983282
                                         6月                 4720233
New England                              6月                 1510062
Mid-Atlantic                             6月                 1504455
Southeast US                             6月                 1705716
                                         7月                 5169720
New England                              7月                 1678002
Mid-Atlantic                             7月                 1820742
Southeast US                             7月                 1670976
                                         8月                 4460823
New England                              8月                 1642968
Mid-Atlantic                             8月                 1381560
Southeast US                             8月                 1436295
                                         9月                 4811094
New England                              9月                 1726767
Mid-Atlantic                             9月                 1178694
Southeast US                             9月                 1905633
                                         10月                4789818
New England                              10月                1648944
Mid-Atlantic                             10月                1530351
Southeast US                             10月                1610523
                                         11月                4644450
New England                              11月                1384185
Mid-Atlantic                             11月                1598667
Southeast US                             11月                1661598
                                         12月                4918416
New England                              12月                1599942
Mid-Atlantic                             12月                1477374
Southeast US                             12月                1841100
        
我们可以看出,与cube(r.name,o.month)结果一样,只不过顺序不一样。cube(o.month,r.name)的顺序是:
group by null,null
group by null,r.name
group by o.month,null
group by o.month,r.name
其中最后两个分组是先小计再分组。

10.1.5部分CUBE
 部分CUBE和部分ROLLUP类似,把不想要的小计和合计的列放到group by中,不放到cube中就可以了。比如:
SELECT r.name region, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month, 
SUM(o.tot_sales) sums
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
GROUP BY r.name, CUBE(o.month);
REGION                                   MONTH                  SUMS
---------------------------------------- ---------------- -------------------------------------------------
New England                                                 19756923
New England                              1月                 1527645
New England                              2月                 1847238
New England                              3月                 1699449
New England                              4月                 1792866
New England                              5月                 1698855
New England                              6月                 1510062
New England                              7月                 1678002
New England                              8月                 1642968
New England                              9月                 1726767
New England                              10月                1648944
New England                              11月                1384185
New England                              12月                1599942
Mid-Atlantic                                                18923298
Mid-Atlantic                             1月                 1832091
Mid-Atlantic                             2月                 1286028
Mid-Atlantic                             3月                 1911093
Mid-Atlantic                             4月                 1623438
Mid-Atlantic                             5月                 1778805
Mid-Atlantic                             6月                 1504455
Mid-Atlantic                             7月                 1820742
Mid-Atlantic                             8月                 1381560
Mid-Atlantic                             9月                 1178694
Mid-Atlantic                             10月                1530351
Mid-Atlantic                             11月                1598667
Mid-Atlantic                             12月                1477374
Southeast US                                                20605485
Southeast US                             1月                 1137063
Southeast US                             2月                 1855269
Southeast US                             3月                 1967979
Southeast US                             4月                 1830051
Southeast US                             5月                 1983282
Southeast US                             6月                 1705716
Southeast US                             7月                 1670976
Southeast US                             8月                 1436295
Southeast US                             9月                 1905633
Southeast US                             10月                1610523
Southeast US                             11月                1661598
Southeast US                             12月                1841100
 
从上面结果可以看出,当将区域名从cube中移到group by中,则总会按区域名统计,则结果中移除了按月小计和总计的结果。我们可以发现,如果cube中只有一个列,那么和rollup的结果一致,也就是上面的语句等价于:
SELECT r.name region, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month, 
SUM(o.tot_sales) sums
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
GROUP BY r.name, rollup(o.month);

注:对上面的rollup和cube的小计或总计中,有的列是null的表示按此列小计,如果前面的非汇总列都是null则表示总计。在实际应用中不可能把这些null给别人看,因为别人不知道你是用rollup或cube计算的。这时候可能会用到nvl或其它的转换,详细请看下节grouing函数。

10.1.6 GROUPING函数
 在实际应用中,使用rollup或cube可以统计小计和合计的值,那么在小计和合计中会出现列的值为NULL的情况,客户就不知道什么意思了。为了增强客户的可读性,我们可能会想到使用NVL函数,如下:
SELECT NVL(TO_CHAR(o.year), 'All Years') year,
NVL(TO_CHAR(TO_DATE(o.month, 'MM'), 'Month'), 'First Quarter') month,
NVL(r.name, 'All Regions') region, SUM(o.tot_sales) sums
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
WHERE o.month BETWEEN 1 AND 3
GROUP BY ROLLUP (o.year, o.month, r.name);
YEAR       MONTH                REGION                     SUMS
---------- -------------------- -------------------- -----------------------------------------------------
2000       1月                  New England             1018430
2000       1月                  Mid-Atlantic            1221394
2000       1月                  Southeast US             758042
2000       1月                  All Regions             2997866
2000       2月                  New England             1231492
2000       2月                  Mid-Atlantic             857352
2000       2月                  Southeast US            1236846
2000       2月                  All Regions             3325690
2000       3月                  New England             1132966
2000       3月                  Mid-Atlantic            1274062
2000       3月                  Southeast US            1311986
2000       3月                  All Regions             3719014
2000       First Quarter        All Regions            10042570
2001       1月                  New England              509215
2001       1月                  Mid-Atlantic             610697
2001       1月                  Southeast US             379021
2001       1月                  All Regions             1498933
2001       2月                  New England              615746
2001       2月                  Mid-Atlantic             428676
2001       2月                  Southeast US             618423
2001       2月                  All Regions             1662845
2001       3月                  New England              566483
2001       3月                  Mid-Atlantic             637031
2001       3月                  Southeast US             655993
2001       3月                  All Regions             1859507
2001       First Quarter        All Regions             5021285
All Years  First Quarter        All Regions            15063855

可以看出,原来是NULL的都被有意义的值代替,增加了用户可读性。使用NVL函数对这个例子来说是非常适合的,但是另外一个情况我们要考虑一下,如果这些列中本身就有NULL值的存在,那么我们使用NVL函数之后,到底是小计还是本身的值呢???带着这个问题,我们来看一个例子:
SELECT * FROM disputed_orders;
ORDER_NBR CUST_NBR SALES_EMP_ID SALE_PRICE ORDER_DT    EXPECTED_SHIP_DT  STATUS
--------- -------- ------------ ---------- ----------- -------------- ---------
     1001        1         7354         99 22-jul-2001 23-jul-2001    DELIVERED
     1000        1         7354            19-jul-2001 24-jul-2001
     1002        5         7368            12-jul-2001 25-jul-2001
     1003        4         7654         56 16-jul-2001 26-jul-2001    DELIVERED
     1004        4         7654         34 18-jul-2001 27-jul-2001    PENDING
     1005        8         7654         99 22-jul-2001 24-jul-2001    DELIVERED
     1006        1         7354            22-jul-2001 28-jul-2001
     1007        5         7368         25 20-jul-2001 22-jul-2001    PENDING
     1008        5         7368         25 21-jul-2001 23-jul-2001    PENDING
     1009        1         7354         56 18-jul-2001 22-jul-2001    DELIVERED
     1012        1         7354         99 22-jul-2001 23-jul-2001    DELIVERED
     1011        1         7354            19-jul-2001 24-jul-2001
     1015        5         7368            12-jul-2001 25-jul-2001
     1017        4         7654         56 16-jul-2001 26-jul-2001    DELIVERED
     1019        4         7654         34 18-jul-2001 27-jul-2001    PENDING
     1021        8         7654         99 22-jul-2001 24-jul-2001    DELIVERED
     1023        1         7354            22-jul-2001 28-jul-2001
     1025        5         7368         25 20-jul-2001 22-jul-2001    PENDING
     1027        5         7368         25 21-jul-2001 23-jul-2001    PENDING
     1029        1         7354         56 18-jul-2001 22-jul-2001    DELIVERED
 
可以看到,有的status值为NULL。那么现在我们需要汇总每个customer对应状态的数目以及所有customer每个状态的数目最后合计。使用NVL函数如下:
SELECT NVL(TO_CHAR(cust_nbr), 'All Customers') customer, 
NVL(status, 'All Status') status,
COUNT(*) FROM disputed_orders
GROUP BY CUBE(cust_nbr, status);
CUSTOMER             STATUS                 COUNT(*)
-------------------- -------------------- ----------
All Customers        All Status                    6
All Customers        All Status                   20
All Customers        PENDING                       6
All Customers        DELIVERED                     8
1                    All Status                    4
1                    All Status                    8
1                    DELIVERED                     4
4                    All Status                    4
4                    PENDING                       2
4                    DELIVERED                     2
5                    All Status                    2
5                    All Status                    6
5                    PENDING                       4
8                    All Status                    2
8                    DELIVERED                     2

从上面的结果可以看出,这种使用NVL函数没有任何意义,得出的结果混淆,比如黄色部分,对用户1有两个all status,其实其中一个是本身表中的status为null,因此统计混乱。
        Oracle为了解决这个问题,专门有一个grouping函数。grouping函数解释如下:
1.        grouping函数只有一个参数,接受来自于group by,rollup,cube,grouping sets的列。
2.        grouping函数对rollup,cube的汇总列(小计或合计行的列值)置为1,其他的非汇总列置为0,如在单独的group by中使用肯定全为0,因为没有通过rollup或cube的小计或合计行。
3.        grouping函数可以有效地避免nvl函数带来的小计或合计与本身表中有null带来的混淆问题。
4.        使用方式为:
SELECT  . . .  [GROUPING(grouping_column_name)]  . . . 
FROM  . . . 
GROUP BY  . . .  {ROLLUP | CUBE} (grouping_column_name)
下面用grouping函数改写上面的的例子如下:
SELECT  decode(grouping(cust_nbr),1,'ALL CUSTOMER',cust_nbr) customer, 
decode(grouping(status),1,'ALL STATUS',nvl(status,'原始值为空')) status,
COUNT(*) FROM disputed_orders
GROUP BY CUBE(cust_nbr, status);
CUSTOMER             STATUS                 COUNT(*)
-------------------- -------------------- ----------
ALL CUSTOMER         原始值为空                    6
ALL CUSTOMER         ALL STATUS                   20
ALL CUSTOMER         PENDING                       6
ALL CUSTOMER         DELIVERED                     8
1                    原始值为空                    4
1                    ALL STATUS                    8
1                    DELIVERED                     4
4                    ALL STATUS                    4
4                    PENDING                       2
4                    DELIVERED                     2
5                    原始值为空                    2
5                    ALL STATUS                    6
5                    PENDING                       4
8                    ALL STATUS                    2
8                    DELIVERED                     2                
使用decode和grouping函数,避免了null的问题,现在黄色部分对status为null的也统计了。汇总和status为null的不再混淆。

再看一个例子:
SELECT DECODE(GROUPING(o.year), 1, 'All Years', o.year) Year,
DECODE(GROUPING(o.month), 1, 'All Months', 
TO_CHAR(TO_DATE(o.month, 'MM'), 'Month')) Month,
DECODE(GROUPING(r.name), 1, 'All Regions', r.name) Region, SUM(o.tot_sales) sums
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
WHERE o.month BETWEEN 1 AND 3
GROUP BY ROLLUP (o.year, o.month, r.name);
YEAR       MONTH                REGION                     SUMS
---------- -------------------- -------------------- ----------
2000       January              New England             1018430
2000       January              Mid-Atlantic            1221394
2000       January              Southeast US             758042
2000       January              All Regions             2997866
2000       February             New England             1231492
2000       February             Mid-Atlantic             857352
2000       February             Southeast US            1236846
2000       February             All Regions             3325690
2000       March                New England             1132966
2000       March                Mid-Atlantic            1274062
2000       March                Southeast US            1311986
2000       March                All Regions             3719014
2000       All Months           All Regions            10042570
2001       January              New England              509215
2001       January              Mid-Atlantic             610697
2001       January              Southeast US             379021
2001       January              All Regions             1498933
2001       February             New England              615746
2001       February             Mid-Atlantic             428676
2001       February             Southeast US             618423
2001       February             All Regions             1662845
2001       March                New England              566483
2001       March                Mid-Atlantic             637031
2001       March                Southeast US             655993
2001       March                All Regions             1859507
2001       All Months           All Regions             5021285
All Years  All Months           All Regions            15063855

使用deocode(当然也可以用case,没有decode简单)联合grouping函数,使报表的小计和合计列描述有意义,避免原始数据有null而使用nvl函数带来混淆的问题。

10.1.6 GROUPING SETS

以上我们已经掌握了rollup,cube分组统计的知识。但是rollup和cube的分组统计包含了常规group by的统计明细以及对相关列的小计和合计值。如果我们需要的只是按每个分组列小计呢?oracle提供了grouping sets操作,对group by的另一个扩展,专门对分组列分别进行小计计算,不包括合计。使用方式和rollup和cube一样,都是放在group by中。如:
grouping sets(C1,C2….Cn)则分组方式有n种,等于列的数目。
group by c1,null,null…..null。
group by null,c2,null….null。
….
group by null,null……..Cn。
无group by null,null….null,也就是说没有合计行。
注意:grouping sets的统计结果和列的顺序无关。
如下例:
SELECT o.year, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month,
r.name region, SUM(o.tot_sales) sums
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
WHERE o.month BETWEEN 1 AND 3
GROUP BY GROUPING SETS (o.year, o.month, r.name);
         
YEAR MONTH                REGION                     SUMS
----- -------------------- -------------------- ------------------------------------------------
                           New England             5074332
                           Mid-Atlantic            5029212
                           Southeast US            4960311
      January                                      4496799
      February                                     4988535
      March                                        5578521
2001                                              5021285
2000                                             10042570
        
可以看出,没有常规group by统计结果,只有按每个单独列的小计,也不包含合计。改变grouping sets中列的顺序,不影响结果,上面的语句等价于:
SELECT o.year, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month,
r.name region, SUM(o.tot_sales) sums
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
WHERE o.month BETWEEN 1 AND 3
GROUP BY GROUPING SETS (o.month, r.name, o.year);

10.1.7ROLLUP、CUBE、GROUPING SETS总结
本章已经学习了小计和合计的三个主要增强的group by操作:rollup,cube,grouping sets操作。下面总结下他们的使用以及区别。
        
操作名        简述        列顺序是否要求        分组情况        使用情况
ROLLUP        ROLLUP操作可以查询出常规group by的结果以及按列小计和合计的结果。        和列顺序有关。见分组情况。        ROLLUP(C1,C2…Cn)。
总共会进行n+1个分组。那么实际上有n+1个group by的union all结果。
顺序为:
group by C1,C2….Cn
group by C1,C2,….Cn-1
group by C1,C2….Cn-2
group by C1
group by null,….null
可以看出是从右到左逐渐递减列的group by,所以和列的顺序有关,顺序不同,意义不同。
需要查询的分组明细以及小计和合计功能的时候使用。
CUBE        CUBE操作是对所有可能的列组合分组,包括常规group by以及所有分组小计和合计的值。        和列顺序无关。        CUBE(C1,C2,C3……C(N))对N个列进行CUBE分组,那么可能的分组情况有:
不分组:C(n,0)
取一列分组:C(n,1)
取N列分组,全分组:C(n,n)
那么运用数学上的组合公式,得出所有所有可能的组合方式有:C(n,0)+C(n,1)+….+C(n,n)=2^n种。
如果只有一个列,那么和rollup一样。
比如cube(name,month)
分组情况有:
group by null,null  //总计C(2,0)
group by null,month //每个月份的所有区域小计
group by name,null //每个区域的所有月份小计 C(2,1)
group by name,month //C(2,2)常规group by
因此,结果和列的顺序无关。
适用于需要统计各种可能的组合情况的小计和合计以及常规分组明细。
GROUPING SETS        只查询按每个列小计的结果,不包含合计值        和列顺序无关。        分组种类=列的数目。
grouping sets(C1,C2,…Cn)
那么等价于下列union all
group by C1,null…..
group by null,C2….
…
group by null,null….Cn        适用于只需要按每个列小计。

10.2分组扩展功能增强
        10.1节我们只是对分组扩展功能使用了几个简单的例子说明,还足以证明扩展分组功能的强大,这些简单的例子适用于oracle8i以及后续版本,但是在Oracle9i开始,对分组扩展的功能提供了新的可行操作:
        ◆ 在group by子句中可以重复列名。
        ◆ 对复合列进行分组。
◆ 分组连接。
本节内容就是围绕上面3点在9i中引入的分组扩展扩展新功能展开。

10.2.1在group by中使用重复列在Oracle8i的时候,在group by中重复列名是不允许的。比如group by中包含了扩展子句(cube,rollup等),在这些扩展子句内外使用相同的列名是不合法的,比如在oracle8i中这样写:
SELECT o.year, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month,
r.name region, SUM(o.tot_sales) total
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
WHERE o.month BETWEEN 1 AND 3
GROUP BY o.year,ROLLUP (o.year, o.month, r.name);
ERROR at line 6:
ORA-30490: Ambiguous expression in GROUP BY ROLLUP or CUBE list
报分组列名混淆错误。但是在Oracle9i之后,则是正确的,结果如下:

        
      YEAR MONTH    REGION                                        TOTAL
---------- ---------------- ---------------------------------------- ----------------------------------------------
      2000 1月              New England                                 1018430
      2000 1月              Mid-Atlantic                                1221394
      2000 1月              Southeast US                                 758042
      2000 1月                                                          2997866
      2000 2月              New England                                 1231492
      2000 2月              Mid-Atlantic                                 857352
      2000 2月              Southeast US                                1236846
      2000 2月                                                          3325690
      2000 3月              New England                                 1132966
      2000 3月              Mid-Atlantic                                1274062
      2000 3月              Southeast US                                1311986
      2000 3月                                                          3719014
      2001 1月              New England                                  509215
      2001 1月              Mid-Atlantic                                 610697
      2001 1月              Southeast US                                 379021
      2001 1月                                                          1498933
      2001 2月              New England                                  615746
      2001 2月              Mid-Atlantic                                 428676
      2001 2月              Southeast US                                 618423
      2001 2月                                                          1662845
      2001 3月              New England                                  566483
      2001 3月              Mid-Atlantic                                 637031
      2001 3月              Southeast US                                 655993
      2001 3月                                                          1859507
      2000                                                             10042570
      2001                                                              5021285
      2000                                                             10042570
      2001                                                              5021285


因为o.year同时出现在group by中和rollup中,所以对每年的所有月份的合计重复统计,见黄色部分,在group by中同名列出现多次没有什么多大用处,只不过为了说明在Oracle9i是允许的。

10.2.2对复合列进行分组 首先说下复合列在Oracle中的使用,想必复合列的使用大家已经比较熟悉了。常见的复合列是在子查询和group by使用。多个列以括号包括,逗号分隔,这样的一个组被当作一个整体。比如下面是一个子查询的例子:
select id,name,trade_id from product 
where (name,trade_id) in
(('易达卡',2),('畅听卡',2));

        ID NAME         TRADE_ID
---------- ---------- ------------------------------
        55 易达卡              2
        58 畅听卡              2
        可以看出,查询了name,trade_id分别对应('易达卡',2)和('畅听卡',2)的值。
        下面看下在单独的group by中使用复合列。例如:
        select id,name,trade_id,count(*) from product  group by (id,name),trade_id;

上面的语句就是先找id,name相同的放在一起,然后再找trade_id相同的放在一组,这里的括号相同于优先级。

在Oracle8i中,分组只支持列的独立分组,不支持复合列的分组,Oracle9i开始支持复合列的分组,也就是上面所说的多个列用括号包括,中间短号,括号内的列相当于一个整体,就像一个列一样,不可拆开。比如Oracle8i只允许rollup(a,b,c),但是oracle9i允许rollup(a,(b,c)),那么将(b,c)作为一个列参与分组计算。增强了分组的功能,可以过滤某些结果。如下例:
SELECT o.year, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month,
r.name region, SUM(o.tot_sales) total
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
WHERE o.month BETWEEN 1 AND 3
GROUP BY ROLLUP ((o.year, o.month),r.name);

      YEAR MONTH            REGION                                        TOTAL
---------- ---------------- ---------------------------------------- ----------
      2000 1月              New England                                 1018430
      2000 1月              Mid-Atlantic                                1221394
      2000 1月              Southeast US                                 758042
      2000 1月                                                          2997866
      2000 2月              New England                                 1231492
      2000 2月              Mid-Atlantic                                 857352
      2000 2月              Southeast US                                1236846
      2000 2月                                                          3325690
      2000 3月              New England                                 1132966
      2000 3月              Mid-Atlantic                                1274062
      2000 3月              Southeast US                                1311986
      2000 3月                                                          3719014
      2001 1月              New England                                  509215
      2001 1月              Mid-Atlantic                                 610697
      2001 1月              Southeast US                                 379021
      2001 1月                                                          1498933
      2001 2月              New England                                  615746
      2001 2月              Mid-Atlantic                                 428676
      2001 2月              Southeast US                                 618423
      2001 2月                                                          1662845
      2001 3月              New England                                  566483
      2001 3月              Mid-Atlantic                                 637031
      2001 3月              Southeast US                                 655993
      2001 3月                                                          1859507
                                                                       15063855

        

由上面的结果分析,GROUP BY ROLLUP ((o.year, o.month),r.name)相当于group by (o.year,o.month),r.name、group by (o.year,o.month),null和group by null,null三个的union结果。与GROUP BY ROLLUP (o.year, o.month,r.name)少了group by rollup o.year,null,null。按年计算所有月份的小计。因为(o.year,o.month)是复合列,相当于一个列,不可分隔。

虽然Oracle8i未提供复合列分组的功能,但是我们可以用复合列的思想,将两个列通过连接操作,变成一个列就可以了,当然在select显示的时候必须与group by中的一致。
下面是oracle8i的实现:

SELECT TO_CHAR(o.year)||' '||TO_CHAR(TO_DATE(o.month,'MM'),'Month') 
 Year_Month, 
 r.name region, SUM(o.tot_sales) 
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
WHERE o.month BETWEEN 1 AND 3
GROUP BY 
ROLLUP (TO_CHAR(o.year)||' '|| TO_CHAR(TO_DATE(o.month,'MM'),'Month'), r.name);
结果与oracle9i的例子一致,只不过year和month通过连接操作符变成一个列显示而已。
group by中使用复合列的好处在于过滤某些本来有的结果。比如上例就过滤了按年计算所有月份的小计值,但是保留了最后的合计值。如果使用前面说的局部rollup:group by o.year,rollup(o.month,r.name)则会丢掉最后的合计值,而且包含了按年统计所有月份的小计值,总之,应该根据具体需求,选择合适的方案。
10.2.3级联分组
 在Oracle9i及后续版本,允许在group by子句后面使用多个ROLLUP,CUBE,GROUPING SETS操作,这在Oracle8i中是不允许的,在Oracle8i中值允许使用其中的一个。
下面研究一下rollup级联分组。
比如在Oracle8i中查询如下语句:

SELECT o.year, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month,
r.name region, SUM(o.tot_sales) total
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
WHERE o.month BETWEEN 1 AND 3
GROUP BY ROLLUP (o.year, o.month), ROLLUP(r.name);        

那么报错:ERROR at line 6:
ORA-30489: Cannot have more than one rollup/cube expression list
在Oracle9i中运行结果为:

      YEAR MONTH    REGION                                        TOTAL
---------- ---------------- ---------------------------------------- -----------------------------------------------
                            New England                                 5074332
                            Mid-Atlantic                                5029212
                            Southeast US                                4960311
                                                                       15063855
      2000                  New England                                 3382888
      2000                  Mid-Atlantic                                3352808
      2000                  Southeast US                                3306874
      2000                                                             10042570
      2000 1月              New England                                 1018430
      2000 1月              Mid-Atlantic                                1221394
      2000 1月              Southeast US                                 758042
      2000 1月                                                          2997866
      2000 2月              New England                                 1231492
      2000 2月              Mid-Atlantic                                 857352
      2000 2月              Southeast US                                1236846
      2000 2月                                                          3325690
      2000 3月              New England                                 1132966
      2000 3月              Mid-Atlantic                                1274062
      2000 3月              Southeast US                                1311986
      2000 3月                                                          3719014
      2001                  New England                                 1691444
      2001                  Mid-Atlantic                                1676404
      2001                  Southeast US                                1653437
      2001                                                              5021285
      2001 1月              New England                                  509215
      2001 1月              Mid-Atlantic                                 610697
      2001 1月              Southeast US                                 379021
      2001 1月                                                          1498933
      2001 2月              New England                                  615746
      2001 2月              Mid-Atlantic                                 428676
      2001 2月              Southeast US                                 618423
      2001 2月                                                          1662845
      2001 3月              New England                                  566483
      2001 3月              Mid-Atlantic                                 637031
      2001 3月              Southeast US                                 655993
      2001 3月                                                          1859507

        
        
:等价于GROUP BY ROLLUP (o.year, o.month), ROLLUP(r.name)
GROUP BY o.year,o.month,r.name
GROUP BY o.year,null,r.name
GROUP BY null,null,r.name
GROUP BY null,null,null
GROUP BY o.year,o.month,null
GROUP BY o.year,null,null
的union all的结果。提供了比rollup(o.year,o.month,r.name)更多的分组功能,多了一个GROUP BY o.year,null,r.name。

group by null。因此才出现上述的6种分组结果:分组的结果为3*2=6,而没有group by null,o.month,r.name和group by null,o.month,null这两种结果。group by null,null。第2个rollup(r.name)的执行顺序是group by r.namegroup by o.year,null        当在group by中使用多个rollup,cube,grouping sets操作的时候,我们叫这样的分组为级联分组。级联分组的结果就是产生每个单独分组操作的交叉列表,但是要符合每个操作的处理步骤,比如上面的GROUP BY ROLLUP (o.year, o.month), ROLLUP(r.name)操作,第1个rollup(o.year,o.month)的执行顺序是group by o.year,o.month

根据上述的规则,我们将rollup(o.year,o.month)改为cube(o.year,o.month),那么cube的计算种类是4中,后面的rollup(r.name)为两种,那么这种级联分组应该有相当于group by的分类为4*2=8种,比rollup(o.year,o.month),rollup(r.name)多了group by null,o.month,r.name和group by null,o.month,null这两种结果。我们看下结果:

SELECT o.year, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month,
r.name region, SUM(o.tot_sales) total
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
WHERE o.month BETWEEN 1 AND 3
GROUP BY cube (o.year, o.month), ROLLUP(r.name);

 YEAR MONTH            REGION                                        TOTAL
---------- ---------------- ---------------------------------------- ----------
      2000 1月              New England                                 1018430
      2001 1月              New England                                  509215
      2000 1月              Mid-Atlantic                                1221394
      2001 1月              Mid-Atlantic                                 610697
      2000 1月              Southeast US                                 758042
      2001 1月              Southeast US                                 379021
      2000 2月              New England                                 1231492
      2001 2月              New England                                  615746
      2000 2月              Mid-Atlantic                                 857352
      2001 2月              Mid-Atlantic                                 428676
      2000 2月              Southeast US                                1236846
      2001 2月              Southeast US                                 618423
      2000 3月              New England                                 1132966
      2001 3月              New England                                  566483
      2000 3月              Mid-Atlantic                                1274062
      2001 3月              Mid-Atlantic                                 637031
      2000 3月              Southeast US                                1311986
      2001 3月              Southeast US                                 655993
             1月              New England                                 1527645
            1月               Mid-Atlantic                                1832091
             1月              Southeast US                                1137063
             2月              New England                                 1847238
            2月              Mid-Atlantic                                1286028
             2月              Southeast US                                1855269
             3月              New England                                 1699449
            3月              Mid-Atlantic                                1911093
             3月              Southeast US                                1967979
      2000                  New England                                 3382888
      2001                  New England                                 1691444
                            New England                                 5074332
      2000                  Mid-Atlantic                                3352808
      2001                  Mid-Atlantic                                1676404
                            Mid-Atlantic                                5029212
      2000                  Southeast US                                3306874
      2001                  Southeast US                                1653437
                            Southeast US                                4960311
            1月                                                          4496799
            2月                                                          4988535
            3月                                                          5578521
      2000                                                             10042570
      2001                                                              5021285
                                                                       15063855
      2000 1月                                                          2997866
      2001 1月                                                          1498933
      2000 2月                                                          3325690
      2001 2月                                                          1662845
      2000 3月                                                          3719014
      2001 3月                                                          1859507
        
        分析结果:黄色部分就是与使用rollup的时候多出的结果,正好是group by null,o.month,r.name和group by null,o.month,null的结果,其他与rollup(o.year,o.month),rollup(r.name)完全一致。

 下面研究一下CUBE级联分组。
 有了级联分组,可以将rollup与cube之间相互转换,比如:
 SELECT o.year, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month,
r.name region, SUM(o.tot_sales) total
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
WHERE o.month BETWEEN 1 AND 3
GROUP BY ROLLUP(o.year),  ROLLUP (o.month), ROLLUP (r.name);

3个单列的rollup级联分组,每个分组有2种,那么总共有2^3=8种,正好相当于下列cube运算的结果:
SELECT o.year, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month,
r.name region, SUM(o.tot_sales) total
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
WHERE o.month BETWEEN 1 AND 3
GROUP BY CUBE (o.year, o.month, r.name);

再来考虑一个问题,如果对于cube级联分组,会出现什么情况下,我们知道cube是对所有可能性进行分组,有n个列,分组的种类是2^n个。那么cube级联分组不管怎么拆分,实际上和单独的cube所有列的结果是一致的。比如下列语句和上面的一致(简写如下):
GROUP BY CUBE (o.year, o.month), CUBE (r.name);
GROUP BY CUBE (o.year), CUBE (o.month, r.name);
GROUP BY CUBE (o.year), CUBE (o.month), CUBE (r.name);

都是有8种分组方法,当然如果和其它的比如rollup联合起来,cube拆开与不拆开的结果也是一致的。

10.2.3.1 GROUPING SETS级联分组

Grouping Sets的级联分组很有用,因为可以知道,grouping sets分组只是对单列分别进行小计统计,比如有n列就是分别对这个n列进行单列小计,有n种结果。但是当我们需要使用Grouping sets获得复合列的小计的时候,那么单独的grouping sets分组就没有办法了,但是可以使用级联grouping sets操作。同rollup和cube的级联分组一样,比如grouping sets(a,b),grouping sets(c,d)那么有2*2=4种统计方法:分别是group by(a,c)、
group by(a,d)、group by(b,c)和group by(b,c),即列的交叉分组。如:
SELECT o.year, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month,
r.name region, SUM(o.tot_sales) total
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
WHERE o.month BETWEEN 1 AND 3
GROUP BY GROUPING SETS (o.year, o.month), GROUPING SETS (r.name);

     YEAR MONTH            REGION                                        TOTAL
--------- ---------------- ---------------------------------------- ----------
          3月              Mid-Atlantic                                1911093
          1月              New England                                 1527645
          2月              Southeast US                                1855269
          2月              Mid-Atlantic                                1286028
          1月              Southeast US                                1137063
          1月              Mid-Atlantic                                1832091
          3月              New England                                 1699449
          3月              Southeast US                                1967979
          2月              New England                                 1847238
     2000                  Mid-Atlantic                                3352808
     2001                  New England                                 1691444
     2000                  Southeast US                                3306874
     2001                  Southeast US                                1653437
     2000                  New England                                 3382888
     2001                  Mid-Atlantic                                1676404

上面的黄色部分就是对(o.month,r.name)的分组结果,红色部分是(o.year,r.name)的分组结果,有两个复合列的分组统计。

 下面我们对上面的例子,增加一个列,看看结果:
SELECT o.year, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month,
r.name region, SUM(o.tot_sales) total
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
WHERE o.month BETWEEN 1 AND 3
GROUP BY GROUPING SETS (o.year, o.month), GROUPING SETS (o.year, r. name);
        
      YEAR MONTH            REGION                                        TOTAL
---------- ---------------- ---------------------------------------- ----------
          3月              Mid-Atlantic                                1911093
           1月              New England                                 1527645
           2月              Southeast US                                1855269
          2月              Mid-Atlantic                                1286028
           1月              Southeast US                                1137063
          1月              Mid-Atlantic                                1832091
           3月              New England                                 1699449
           3月              Southeast US                                1967979
           2月              New England                                 1847238
      2000                  Mid-Atlantic                                3352808
      2001                  New England                                 1691444
      2000                  Southeast US                                3306874
      2001                  Southeast US                                1653437
      2000                  New England                                 3382888
      2001                  Mid-Atlantic                                1676404
      2000 1月                                                          2997866
      2000 2月                                                          3325690
      2000 3月                                                          3719014
      2000                                                             10042570
      2001 1月                                                          1498933
      2001 2月                                                          1662845
      2001 3月                                                          1859507
      2001                                                              5021285


GROUPING SETS (o.year, o.month), GROUPING SETS (o.year, r. name)相当于group by(o.year,o.year)等价于group by o.year,group by(o.year,r.name),group by(o.month,o.year),group by(o.month,r.name)对应上面4个区域的结果。

 其实,因为最终的结果可以转化为对应的group by分组的union结果,而group by分组和列的顺序没有关系,因此级联grouping sets和列的顺序也没有关系,只要分组的种类一致即可,比如上面的等价于: GROUPING SETS (o.year, r.name), GROUPING SETS (o.year, o.month);
GROUPING SETS (o.month, o.year), GROUPING SETS (r.name, o.year);

另外,在一个group by中可以同时使用grouping sets,cube,rollup,比如:
SELECT o.year, TO_CHAR(TO_DATE(o.month, 'MM'), 'Month') month,
r.name region, SUM(o.tot_sales) total
FROM all_orders o JOIN region r
ON r.region_id = o.region_id
WHERE o.month BETWEEN 1 AND 3
GROUP BY GROUPING SETS (o.month, o.year), ROLLUP(r.name), CUBE (o.year);
上面的有8种统计结果,但是这样的语句其实一般意义不大,如果需要这样复杂的统计,需要仔细分析。

猜你喜欢

转载自mxm910821.iteye.com/blog/1766775