zoukankan      html  css  js  c++  java
  • Oracle的聚合函数group by结合CUBE和ROLLUP的使用

    转自:https://docs.oracle.com/cd/E11882_01/server.112/e25554/aggreg.htm#DWHSG8618

    CUBE Syntax

    CUBE appears in the GROUP BY clause in a SELECT statement. Its form is:

    SELECT …  GROUP BY CUBE (grouping_column_reference_list)
    

    Example 21-4 CUBE

    SELECT channel_desc, calendar_month_desc, countries.country_iso_code,
          TO_CHAR(SUM(amount_sold), '9,999,999,999') SALES$
    FROM sales, customers, times, channels, countries
    WHERE sales.time_id=times.time_id AND sales.cust_id=customers.cust_id AND
      sales.channel_id= channels.channel_id
     AND customers.country_id = countries.country_id
     AND channels.channel_desc IN
      ('Direct Sales', 'Internet') AND times.calendar_month_desc IN
      ('2000-09', '2000-10') AND countries.country_iso_code IN ('GB', 'US')
    GROUP BY CUBE(channel_desc, calendar_month_desc, countries.country_iso_code); 
    
    CHANNEL_DESC         CALENDAR CO SALES$
    -------------------- -------- -- --------------
                                          1,790,032
                                  GB        208,257
                                  US      1,581,775
                         2000-09            864,217
                         2000-09  GB        101,792
                         2000-09  US        762,425
                         2000-10            925,815
                         2000-10  GB        106,465
                         2000-10  US        819,351
    Internet                                292,387
    Internet                      GB         31,109
    Internet                      US        261,278
    Internet             2000-09            140,793
    Internet             2000-09  GB         16,569
    Internet             2000-09  US        124,224
    Internet             2000-10            151,593
    Internet             2000-10  GB         14,539
    Internet             2000-10  US        137,054
    Direct Sales                          1,497,646
    Direct Sales                  GB        177,148
    Direct Sales                  US      1,320,497
    Direct Sales         2000-09            723,424
    Direct Sales         2000-09  GB         85,223
    Direct Sales         2000-09  US        638,201
    Direct Sales         2000-10            774,222
    Direct Sales         2000-10  GB         91,925
    Direct Sales         2000-10  US        682,297


    ROLLUP Syntax

    ROLLUP appears in the GROUP BY clause in a SELECT statement. Its form is:

    SELECT … GROUP BY ROLLUP(grouping_column_reference_list)
    

    Example 21-2 ROLLUP

    This example uses the data in the sh sample schema data, the same data as was used in Figure 21-1. The ROLLUP is across three dimensions.

    SELECT channels.channel_desc, calendar_month_desc, 
           countries.country_iso_code,
           TO_CHAR(SUM(amount_sold), '9,999,999,999') SALES$
    FROM sales, customers, times, channels, countries
    WHERE sales.time_id=times.time_id 
      AND sales.cust_id=customers.cust_id 
      AND customers.country_id = countries.country_id
      AND sales.channel_id = channels.channel_id 
      AND channels.channel_desc IN ('Direct Sales', 'Internet') 
      AND times.calendar_month_desc IN ('2000-09', '2000-10') 
      AND countries.country_iso_code IN ('GB', 'US')
    GROUP BY 
      ROLLUP(channels.channel_desc, calendar_month_desc, countries.country_iso_code);
    
    CHANNEL_DESC         CALENDAR CO SALES$
    -------------------- -------- -- --------------
    Internet             2000-09  GB         16,569
    Internet             2000-09  US        124,224
    Internet             2000-09            140,793
    Internet             2000-10  GB         14,539
    Internet             2000-10  US        137,054
    Internet             2000-10            151,593
    Internet                                292,387
    Direct Sales         2000-09  GB         85,223
    Direct Sales         2000-09  US        638,201
    Direct Sales         2000-09            723,424
    Direct Sales         2000-10  GB         91,925
    Direct Sales         2000-10  US        682,297
    Direct Sales         2000-10            774,222
    Direct Sales                          1,497,646
                                          1,790,032

    、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
    仔细观察一下,CUBE与ROLLUP之间的细微差别
    rollup(a,b)   统计列包含:(a,b)、(a)、()
    rollup(a,b,c) 统计列包含:(a,b,c)、(a,b)、(a)、()
    ……以此类推ing……

    cube(a,b)     统计列包含:(a,b)、(a)、(b)、()
    cube(a,b,c)   统计列包含:(a,b,c)、(a,b)、(a,c)、(b,c)、(a)、(b)、(c)、()
    ……以此类推ing……




  • 相关阅读:
    C++ 动态链接库、静态链接库
    WIN32 PE结构 重定位表
    WIN32 PE Bound Import 绑定导入表
    WIN32申请内存 物理页 VirtualAlloc VirtualFree
    PE文件结构详解 PE导入表
    PE文件结构详解 PE导出表
    WIN PE文件插入MessageBox
    WIN32 创建窗口 WNDCLASS 和CreateWindow
    053464
    053463
  • 原文地址:https://www.cnblogs.com/YuyuanNo1/p/9176262.html
Copyright © 2011-2022 走看看