摘自 http://blog.itpub.net/26977915/viewspace-734114/

在报表语句中经常要使用各种分组汇总,rollup和cube就是常用的分组汇总方式。

第一:group by rollup

1、如果使用诸如group by rollup(A,B,C)的方式分组,那么返回的分组结果是
(A,B,C) (A,B) (A) (NULL) 一共四种结果。即从右到左递减,最后来个合计。

例如:

SQL> select * from t;

YEARS     MONTHS PRODUCT_NA      SALES
---------- ---------- ---------- ----------
      2008          1 A                1000
      2008          1 B                1500
      2008          2 A                2000
      2008          2 B                3000
      2008          2 C                1000
      2008          3 A                3000

已选择6行。

SQL> select years,months,product_name,sum(sales) sum_sales from t
  2  group by rollup(years,months,product_name)
  3  /

YEARS     MONTHS PRODUCT_NA  SUM_SALES
---------- ---------- ---------- ----------
      2008          1 A                1000 ----------group by (years,months,product_name)
      2008          1 B                1500
      2008          1                  2500 ----------group by (years,months)
      2008          2 A                2000
      2008          2 B                3000
      2008          2 C                1000
      2008          2                  6000 ----------group by (years,months)
      2008          3 A                3000
      2008          3                  3000 ----------group by (years,months)
      2008                            11500 ----------group by (years)
                                      11500 ----------group by (NULL)

已选择11行。

2、如果使用诸如group by A,ROLLUP(B,C) 那么返回的分组方式是:
(A,B,C)  (A,B) (A,NULL)  及在这种情况下,先计算rollup里面的分组情况,再与A组合。

例如:
SQL> select years,months,product_name,sum(sales) sum_sales from t
  2  group by years,rollup(months,product_name)
  3  /

YEARS     MONTHS PRODUCT_NA  SUM_SALES
---------- ---------- ---------- ----------
      2008          1 A                1000 ----------group by (years,months,product_name)
      2008          1 B                1500
      2008          1                  2500 ----------group by (years,months)
      2008          2 A                2000
      2008          2 B                3000
      2008          2 C                1000
      2008          2                  6000
      2008          3 A                3000
      2008          3                  3000
      2008                            11500 ----------group by (years)

已选择10行。

第二:group by cube

1、如果使用诸如cube(A,B,C)的方式,那么返回的分组组合是
(A) (A,B) (A,C) (A,B,C) (B) (B,C) (C) (null) 共8种组合方式

例如:

SQL> select years,months,product_name,sum(sales) sum_sales from t
  2  group by cube(years,months,product_name)
  3  /

YEARS     MONTHS PRODUCT_NA  SUM_SALES
---------- ---------- ---------- ----------
                                      11500 ----------group by (null)
                      A                6000 ----------group by (product_name)
                      B                4500
                      C                1000
                    1                  2500
                    1 A                1000
                    1 B                1500
                    2                  6000
                    2 A                2000
                    2 B                3000
                    2 C                1000
                    3                  3000 ----------group by (months)
                    3 A                3000 ----------group by (months,product_name)
      2008                            11500 ----------group by (years)
      2008            A                6000
      2008            B                4500
      2008            C                1000 ----------group by (years,product_name)
      2008          1                  2500
      2008          1 A                1000
      2008          1 B                1500
      2008          2                  6000
      2008          2 A                2000
      2008          2 B                3000
      2008          2 C                1000
      2008          3                  3000 ----------group by (years,months)
      2008          3 A                3000 ----------group by (years,months,product_name)

已选择26行。

2、如果使用GROUP BY A,CUBE(B,C),那么返回的分组组合为:
(A,B) (A,B,C) (A,C) (A)

例如:
SQL> select years,months,product_name,sum(sales) sum_sales from t
  2  group by years,cube(months,product_name)
  3  /

YEARS     MONTHS PRODUCT_NA  SUM_SALES
---------- ---------- ---------- ----------
      2008                            11500 ----------group by (years)
      2008            A                6000 ----------group by (years,product_name)
      2008            B                4500
      2008            C                1000
      2008          1                  2500 ----------group by (years,months)
      2008          1 A                1000 ----------group by (years,months,product_name)
      2008          1 B                1500
      2008          2                  6000
      2008          2 A                2000
      2008          2 B                3000
      2008          2 C                1000
      2008          3                  3000
      2008          3 A                3000

已选择13行。

3、如果使用GROUP BY A,ROLLUP(B,C),CUBE(D,E),那么返回的分组组合为:

先分解cube:

a,rollup(b,c),d,e
a,rollup(b,c),d
a,rollup(b,c),e
a,rollup(b,c)

再分解ROLLUP而得到最终所有情况为:

a,b,c,d,e
a,b,d,e
a,d,e
a,b,c,d
a,b,d
a,d
a,b,c,e
a,b,e
a,e
a,b,c
a,b
a

例如:
SQL> select years,months,product_name,sum(sales) sum_sales from t
  2  group by years,rollup(months),cube(product_name)
  3  /

YEARS     MONTHS PRODUCT_NA  SUM_SALES
---------- ---------- ---------- ----------
      2008          1 A                1000 ----------group by (years,months,product_name)
      2008          2 A                2000
      2008          3 A                3000
      2008          1 B                1500
      2008          2 B                3000
      2008          2 C                1000
      2008            A                6000 ----------group by (years,product_name)
      2008            B                4500
      2008            C                1000
      2008          1                  2500 ----------group by (years,product_name)
      2008          2                  6000
      2008          3                  3000
      2008                            11500 ----------group by (years)

已选择13行。

第三:grouping sets
如果使用group by A,grouping sets(B,C) 那么相当于group by A,B UNION ALL group by A,C

例如:
SQL> select years,months,product_name,sum(sales) sum_sales from t
  2  group by years,grouping sets(months,product_name)
  3  /

YEARS     MONTHS PRODUCT_NA  SUM_SALES
---------- ---------- ---------- ----------
      2008          2                  6000 ----------group by (years,months)
      2008          1                  2500
      2008          3                  3000
      2008            B                4500 ----------group by (years,product_name)
      2008            C                1000
      2008            A                6000

已选择6行。

-----------------------------------------------------------------------------------------------------------------华丽的分割线!

现实中可能希望出现小计、合计等字样的报表,那么可以使用grouping函数来达到美化的效果!

第三:grouping(exp),当没有对exp分组汇总时,便返回1;

例如:
SQL> select months,product_name,sum(sales) sum_sales,grouping(product_name) from t
  2  group by rollup(months,product_name)
  3  /

MONTHS PRODUCT_NA  SUM_SALES GROUPING(PRODUCT_NAME)
---------- ---------- ---------- ----------------------
         1 A                1000                      0 ----------group by (months,product_name)
         1 B                1500                      0
         1                  2500                      1 ----------group by (months)
         2 A                2000                      0
         2 B                3000                      0
         2 C                1000                      0
         2                  6000                      1 ----------group by (months)
         3 A                3000                      0
         3                  3000                      1 ----------group by (months)
                           11500                      1 ----------group by (null)

已选择10行。

第四:GROUPING_ID(exp1,exp2,…,expN)={GROUPING(exp1)||GROUPING(exp2)||…||GROUPING(expN)}变成十进制数,如:
如果GROUPING(A)=1,GROUPING(B)=0,GROUPING(C)=1,那么
GROUPING_ID(A,B,C) = [101]二进制 = 5,
GROUPING_ID(B,A,C) = [011]二进制 = 3.

例如:
SQL> select years,months,product_name,sum(sales) sum_sales,grouping_id(years,months,product_name) g_id from t
  2  group by rollup(years,months,product_name)
  3  /

YEARS     MONTHS PRODUCT_NA  SUM_SALES       G_ID
---------- ---------- ---------- ---------- ----------
      2008          1 A                1000          0
      2008          1 B                1500          0
      2008          1                  2500          1 ----------group by (years,months) 001=1
      2008          2 A                2000          0
      2008          2 B                3000          0
      2008          2 C                1000          0
      2008          2                  6000          1
      2008          3 A                3000          0
      2008          3                  3000          1
      2008                            11500          3 ----------group by (years)   011=3
                                      11500          7 ----------group by (null)    111=7

已选择11行。

了解了grouping和grouping_id函数后,便可以结合decode函数来生成小计合计的效果了;

SQL> select decode(grouping(months)+grouping(product_name),1,'月份小计',2,'合计:',months) months,
  2  product_name,sum(sales) sum_sales from t
  3  group by rollup(months,product_name)
  4  /

MONTHS                                   PRODUCT_NA  SUM_SALES
---------------------------------------- ---------- ----------
1                                        A                1000
1                                        B                1500
月份小计                                                  2500
2                                        A                2000
2                                        B                3000
2                                        C                1000
月份小计                                                  6000
3                                        A                3000
月份小计                                                  3000
合计:                                                   11500

已选择10行。

SQL> select decode(grouping_id(months,product_name),1,'月份小计:',2,'产品小计:',3,'合计:',months) months,
  2  product_name,sum(sales) sum_sales from t
  3  group by cube(months,product_name)
  4  order by 2
  5  /

MONTHS                                   PRODUCT_NA  SUM_SALES
---------------------------------------- ---------- ----------
1                                        A                1000
2                                        A                2000
3                                        A                3000
产品小计:                               A                6000
1                                        B                1500
2                                        B                3000
产品小计:                               B                4500
2                                        C                1000
产品小计:                               C                1000
月份小计:                                                2500
月份小计:                                                6000
月份小计:                                                3000
合计:                                                   11500

已选择13行。

点评:group by rollup、group by cube、grouping sets、grouping函数、grouping_id函数这些属于报表常用函数,要灵活运用!

【转】rollup、cub、grouping sets、grouping、grouping_id在报表中的应用的更多相关文章

  1. Oracle的rollup、cube、grouping sets函数

    转载自:https://blog.csdn.net/huang_xw/article/details/6402396 Oracle的group by除了基本用法以外,还有3种扩展用法,分别是rollu ...

  2. Oracle中group by 的扩展函数rollup、cube、grouping sets

    Oracle的group by除了基本使用方法以外,还有3种扩展使用方法,各自是rollup.cube.grouping sets.分别介绍例如以下: 1.rollup 对数据库表emp.如果当中两个 ...

  3. Hive函数:GROUPING SETS,GROUPING__ID,CUBE,ROLLUP

    参考:lxw大数据田地:http://lxw1234.com/archives/2015/04/193.htm 数据准备: CREATE EXTERNAL TABLE test_data ( mont ...

  4. Hive高级聚合GROUPING SETS,ROLLUP以及CUBE

    scala> import org.apache.spark.sql.hive.HiveContextimport org.apache.spark.sql.hive.HiveContext s ...

  5. 解析数仓OLAP函数:ROLLUP、CUBE、GROUPING SETS

    摘要:GaussDB(DWS) ROLLUP,CUBE,GROUPING SETS等OLAP函数的原理解析. 本文分享自华为云社区<GaussDB(DWS) OLAP函数浅析>,作者: D ...

  6. hive grouping sets 等聚合函数

    函数说明: grouping sets 在一个 group by 查询中,根据不同的维度组合进行聚合,等价于将不同维度的 group by 结果集进行 union allcube 根据 group b ...

  7. 综合练习: PIVOT、UNPIVOT、GROUPING SETS、GROUPING_ID_1

    综合练习: PIVOT.UNPIVOT.GROUPING SETS.GROUPING_ID 问题1:Desired output: empid cnt2007 cnt2008 cnt2009 ---- ...

  8. group by <grouping sets(...) ><cube(...)>

    GROUP BY      GROUPING SETS() 后面将还会写学习 with cube,  with rollup,以及将它们转换为标准的GROUP BY的子句GROUP SET(), CU ...

  9. SQL Server ->> GROUPING SETS, CUBE, ROLLUP, GROUPING, GROUPING_ID

    在我们制作报表的时候常常需要分组聚合.多组聚合和总合.如果通过另外的T-SQL语句来聚合难免性能太差.如果通过报表工具的聚合功能虽说比使用额外的T-SQL语句性能上要好很多,不过不够干脆,还是需要先生 ...

随机推荐

  1. ros科大讯飞语音识别

    转自http://www.ncnynl.com/archives/201611/1069.html ROS入门教程-编写科大讯飞语音SDK的ROS包 说明 ROS软件包xfei_asr是集成自科大讯飞 ...

  2. 判断IE版本的HTML语句详解,如:[if lte IE 9]……[endif]

    我们常常会在网页的HTML里面看到形如[if lte IE 9]……[endif]的代码,表示的是限定某些浏览器版本才能执行的语句,那么这些判断语句的规则是什么呢?请看下文: <!--[if ! ...

  3. Anagrams

    这题Leetcode上面的描述不清楚.怎么也得举两个例子吧,不然谁懂? 题目的意思是,给定一些字符串,比如["abc","cba","bac" ...

  4. ajax里面success函数return上层接收不到

    开发一个小功能,在success fail里直接return,没有收到返回值.排查,查了下往上的博客,参考了以下三个: http://blog.csdn.net/fairyhawk/article/d ...

  5. AngularJs的UI组件ui-Bootstrap分享(六)——Tabs

    tabs控件使用uib-tabset指令和uib-tab指令,效果是这样的: <!DOCTYPE html> <html ng-app="ui.bootstrap.demo ...

  6. excel 转换日期

    早上一朋友问我excel中如何将类似这样“19850421”的文本日期转换为“1985-04-21”.我的第一反应就是直接设置单元格格式为日期,于是打开excel试了试结果显示“########### ...

  7. Java Base64 类

    package org.yp.ypfinancing.core.service.payV2.domain.service.Sdp.utils; public final class Base64 { ...

  8. python的各种编辑器-PyScripter、pycharm 、atom、vscode、Sublime Text等等

    RT,本文主要列举python的各种编辑器-PyScripter.pycharm .atom.vscode.Sublime Text等等. PyScripter 开源 免费 windows only ...

  9. MFC程序执行顺序 .

    1.创建Application object对象theApp 程序一开始生产一个(且只有一个)Application object对象theApp,也即一个CWinApp对象,这个全局对象一产生,便执 ...

  10. 图表控件FlowChart.NET详细介绍及免费下载地址

    FlowChart.NET是一款专业的.NET平台下的流程图及图表控件,它可以运行在任何C#, VB.NET或Delphi.NET语言编写的软件中.能够帮助你创建工作流程图.对象层次和关系图.网络拓扑 ...