• 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……




  • 相关阅读:
    静态邻接表dijkstra
    最短路径系列【最短路径、哈密顿路等】
    python 给多人发送邮件,且将结果添加为附件
    Excel调换数据位置
    try ... except...,好处是执行失败后,仍然可以继续运行
    制作表头,2种方式
    工资表变工资条,2种方式
    C言语教程第一章: C言语概论 (4)
    从红旗5.0说起——看Linux的内存解决
    红旗Linux桌面4.1文本安装历程图解(二)
  • 原文地址:https://www.cnblogs.com/YuyuanNo1/p/9176262.html
Copyright © 2020-2023  润新知