Mysql,Oracle使用rollup函数完成行列统计

时间 2014-02-25 00:05:38  ITeye-博客

原文  http://53873039oycg.iteye.com/blog/2021445

主题 MySQLOracle数据库

昨天突然在 一篇博客中看到了Mysql也有rollup函数,原博文使用了rollup进行行列统计,原博文链接如下:

http://www.cnblogs.com/lhj588/archive/2012/06/15/2550392.html

本博文主要是记录下mysql和oracle使用rollup函数进行行列统计,内容比较简单。

首先是mysql,建表测试:

CREATE TABLE `tmysql_test_hanglietongji` (
  `id` int(11) NOT NULL,
  `c1` char(2) COLLATE utf8_bin DEFAULT NULL,
  `c2` char(2) COLLATE utf8_bin DEFAULT NULL,
  `c3` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
INSERT INTO `tmysql_test_hanglietongji` VALUES (1, ‘A1‘, ‘B1‘, 9);
INSERT INTO `tmysql_test_hanglietongji` VALUES (2, ‘A2‘, ‘B1‘, 7);
INSERT INTO `tmysql_test_hanglietongji` VALUES (3, ‘A3‘, ‘B1‘, 4);
INSERT INTO `tmysql_test_hanglietongji` VALUES (4, ‘A4‘, ‘B1‘, 2);
INSERT INTO `tmysql_test_hanglietongji` VALUES (5, ‘A1‘, ‘B2‘, 2);
INSERT INTO `tmysql_test_hanglietongji` VALUES (6, ‘A2‘, ‘B2‘, 9);
INSERT INTO `tmysql_test_hanglietongji` VALUES (7, ‘A3‘, ‘B2‘, 8);
INSERT INTO `tmysql_test_hanglietongji` VALUES (8, ‘A4‘, ‘B2‘, 5);
INSERT INTO `tmysql_test_hanglietongji` VALUES (9, ‘A1‘, ‘B3‘, 1);
INSERT INTO `tmysql_test_hanglietongji` VALUES (10, ‘A2‘, ‘B3‘, 8);
INSERT INTO `tmysql_test_hanglietongji` VALUES (11, ‘A3‘, ‘B3‘, 8);
INSERT INTO `tmysql_test_hanglietongji` VALUES (12, ‘A4‘, ‘B3‘, 6);
INSERT INTO `tmysql_test_hanglietongji` VALUES (13, ‘A1‘, ‘B4‘, 8);
INSERT INTO `tmysql_test_hanglietongji` VALUES (14, ‘A2‘, ‘B4‘, 2);
INSERT INTO `tmysql_test_hanglietongji` VALUES (15, ‘A3‘, ‘B4‘, 6);
INSERT INTO `tmysql_test_hanglietongji` VALUES (16, ‘A4‘, ‘B4‘, 9);
INSERT INTO `tmysql_test_hanglietongji` VALUES (17, ‘A1‘, ‘B4‘, 3);
INSERT INTO `tmysql_test_hanglietongji` VALUES (18, ‘A2‘, ‘B4‘, 5);
INSERT INTO `tmysql_test_hanglietongji` VALUES (19, ‘A3‘, ‘B4‘, 2);
INSERT INTO `tmysql_test_hanglietongji` VALUES (20, ‘A4‘, ‘B4‘, 5);

要完成的效果如下:


      最简单的是使用union,如下:

select ifnull(c1, ‘total‘) as ‘total‘,
       sum(if(c2 = ‘B1‘, C3, 0)) AS B1,
       sum(if(c2 = ‘B2‘, C3, 0)) AS B2,
       sum(if(c2 = ‘B3‘, C3, 0)) AS B3,
       sum(if(c2 = ‘B4‘, C3, 0)) AS B4,
       SUM(C3) AS TOTAL
  from tmysql_test_hanglietongji
 group by C1
union
select ‘total‘ as ‘total‘,
       sum(if(c2 = ‘B1‘, C3, 0)) AS B1,
       sum(if(c2 = ‘B2‘, C3, 0)) AS B2,
       sum(if(c2 = ‘B3‘, C3, 0)) AS B3,
       sum(if(c2 = ‘B4‘, C3, 0)) AS B4,
       SUM(C3) AS TOTAL
  from tmysql_test_hanglietongji
 order by 1

也可以使用with rollup函数。注意当使用 rollup时, 你不能同时使用 order by子句进行结果排序

select ifnull(c1, ‘total‘) ‘total‘,
       sum(if(c2 = ‘B1‘, C3, 0)) AS B1,
       sum(if(c2 = ‘B2‘, C3, 0)) AS B2,
       sum(if(c2 = ‘B3‘, C3, 0)) AS B3,
       sum(if(c2 = ‘B4‘, C3, 0)) AS B4,
       SUM(C3) AS TOTAL
  from tmysql_test_hanglietongji
 group by C1 with rollup;

with rollup其实是第一个的简化。

也可以这样写:

SELECT IFNULL(c1, ‘total‘) AS total,
       SUM(IF(c2 = ‘B1‘, c3, 0)) AS B1,
       SUM(IF(c2 = ‘B2‘, c3, 0)) AS B2,
       SUM(IF(c2 = ‘B3‘, c3, 0)) AS B3,
       SUM(IF(c2 = ‘B4‘, c3, 0)) AS B4,
       SUM(IF(c2 = ‘total‘, c3, 0)) AS total
  FROM (SELECT c1, IFNULL(c2, ‘total‘) AS c2, SUM(c3) AS c3
          FROM tmysql_test_hanglietongji
         GROUP BY c1, c2 WITH ROLLUP
        HAVING c1 IS NOT NULL) AS A
 GROUP BY c1 WITH ROLLUP;

HAVING c1 IS NOT NULL条件主要是过滤掉对整个tmysql_test_hanglietongji 表求和的那一行,以上面的子查询为例:

SELECT c1, IFNULL(c2, ‘total‘) AS c2, SUM(c3) AS c3
          FROM tmysql_test_hanglietongji
         GROUP BY c1, c2 WITH ROLLUP

结果是:

 
   相当于:

SELECT c1, IFNULL(c2, ‘total‘) AS c2, SUM(c3) AS c3
FROM tmysql_test_hanglietongji
GROUP BY c1, c2
union ALL
SELECT c1, ‘total‘ AS c2, SUM(c3) AS c3
FROM tmysql_test_hanglietongji
GROUP BY c1
union ALL
SELECT NULL, ‘total‘ AS c2, SUM(c3) AS c3
FROM tmysql_test_hanglietongji

结果是:

 
    可以看出group by c1,c2 with rollup相当于group by c1,c2 union group by c1(c2替换为NULL) union (c1,c2全部替换为NULL)。

这里的替换规则参考了链接

http://blog.itpub.net/519536/viewspace-610995

原文是替换Oracle的rollup,在Mysql中也适用。

使用普通sql写法是:

SELECT IFNULL(c1, ‘total‘) AS total,
       SUM(IF(c2 = ‘B1‘, c3, 0)) AS B1,
       SUM(IF(c2 = ‘B2‘, c3, 0)) AS B2,
       SUM(IF(c2 = ‘B3‘, c3, 0)) AS B3,
       SUM(IF(c2 = ‘B4‘, c3, 0)) AS B4,
       SUM(IF(c2 = ‘total‘, c3, 0)) AS total
  FROM (SELECT c1, IFNULL(c2, ‘total‘) AS c2, SUM(c3) AS c3
          FROM tmysql_test_hanglietongji
         GROUP BY c1, c2
        HAVING c1 IS NOT NULL
        union
        SELECT c1, ‘total‘ as c2, SUM(c3) AS c3
          FROM tmysql_test_hanglietongji
         group by c1) A
 group by c1
UNION
SELECT ‘total‘ as total,
       SUM(IF(c2 = ‘B1‘, c3, 0)) AS B1,
       SUM(IF(c2 = ‘B2‘, c3, 0)) AS B2,
       SUM(IF(c2 = ‘B3‘, c3, 0)) AS B3,
       SUM(IF(c2 = ‘B4‘, c3, 0)) AS B4,
       SUM(IF(c2 = ‘total‘, c3, 0)) AS total
  FROM (SELECT c1, IFNULL(c2, ‘total‘) AS c2, SUM(c3) AS c3
          FROM tmysql_test_hanglietongji
         GROUP BY c1, c2
        HAVING c1 IS NOT NULL
        union
        SELECT c1, ‘total‘ as c2, SUM(c3) AS c3
          FROM tmysql_test_hanglietongji
         group by c1) A

少了一个是因为上面的having要求c1 is not null,所以替换c1为NULL就没有了。

下面看下oracle中怎么写,想要的效果如图:


   首先建表。

create table TSQL_TEST_HANGLIETONGJI
(
  ID NUMBER(4) not null,
  C1 VARCHAR2(2),
  C2 VARCHAR2(2),
  C3 NUMBER(4)
)
;
alter table TSQL_TEST_HANGLIETONGJI
  add primary key (ID);

insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (1, ‘A1‘, ‘B1‘, 9);
insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (2, ‘A2‘, ‘B1‘, 7);
insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (3, ‘A3‘, ‘B1‘, 4);
insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (4, ‘A4‘, ‘B1‘, 2);
insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (5, ‘A1‘, ‘B2‘, 2);
insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (6, ‘A2‘, ‘B2‘, 9);
insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (7, ‘A3‘, ‘B2‘, 8);
insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (8, ‘A4‘, ‘B2‘, 5);
insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (9, ‘A1‘, ‘B3‘, 1);
insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (10, ‘A2‘, ‘B3‘, 8);
insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (11, ‘A3‘, ‘B3‘, 8);
insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (12, ‘A4‘, ‘B3‘, 6);
insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (13, ‘A1‘, ‘B4‘, 8);
insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (14, ‘A2‘, ‘B4‘, 2);
insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (15, ‘A3‘, ‘B4‘, 6);
insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (16, ‘A4‘, ‘B4‘, 9);
insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (17, ‘A1‘, ‘B4‘, 3);
insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (18, ‘A2‘, ‘B4‘, 5);
insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (19, ‘A3‘, ‘B4‘, 2);
insert into TSQL_TEST_HANGLIETONGJI (ID, C1, C2, C3)
values (20, ‘A4‘, ‘B4‘, 5);

最简单的写法是:

select c1,
       sum(decode(c2,‘B1‘, C3, 0)) AS B1,
       sum(decode(c2 ,‘B2‘, C3, 0)) AS B2,
       sum(decode(c2 ,‘B3‘, C3, 0)) AS B3,
       sum(decode(c2 ,‘B4‘, C3, 0)) AS B4,
       SUM(C3) AS TOTAL
  from tsql_test_hanglietongji
 group by C1
UNION
SELECT ‘TOTAL‘,
       sum(decode(c2 ,‘B1‘, C3, 0)) AS B1,
       sum(decode(c2 ,‘B2‘, C3, 0)) AS B2,
       sum(decode(c2 ,‘B3‘, C3, 0)) AS B3,
       sum(decode(c2 ,‘B4‘, C3, 0)) AS B4,
       SUM(C3)
  FROM tsql_test_hanglietongji

然后使用rollup函数简化。

SELECT nvl(c1, ‘total‘) AS total,
       SUM(decode(c2, ‘B1‘, c3, 0)) AS B1,
       SUM(decode(c2, ‘B2‘, c3, 0)) AS B2,
       SUM(decode(c2, ‘B3‘, c3, 0)) AS B3,
       SUM(decode(c2, ‘B4‘, c3, 0)) AS B4,
       sum(c3) AS total
  FROM tsql_test_hanglietongji
 GROUP BY ROLLUP(c1)

也可以这么写:

SELECT nvl(c1, ‘total‘) AS total_c,
       SUM(decode(c2, ‘B1‘, c3, 0)) AS B1,
       SUM(decode(c2, ‘B2‘, c3, 0)) AS B2,
       SUM(decode(c2, ‘B3‘, c3, 0)) AS B3,
       SUM(decode(c2, ‘B4‘, c3, 0)) AS B4,
       SUM(decode(c2, ‘total‘, c3, 0)) AS total_r
  FROM (SELECT c1, nvl(c2, ‘total‘) AS c2, SUM(c3) AS c3
          FROM tsql_test_hanglietongji
         GROUP BY ROLLUP(c1, c2)
        HAVING c1 IS NOT NULL) A
 GROUP BY ROLLUP(c1);

rollup和普通sql替换上面也说了,举个例子:

SELECT c1, nvl(c2, ‘total‘) AS c2, SUM(c3) AS c3
          FROM tsql_test_hanglietongji
         GROUP BY ROLLUP(c1, c2)

效果是:

 
   普通sql写法是:

SELECT c1, nvl(c2, ‘total‘) AS c2, SUM(c3) AS c3
  FROM tsql_test_hanglietongji
 GROUP BY c1, c2
union all
SELECT c1, nvl(null, ‘total‘) AS c2, SUM(c3) AS c3
  FROM tsql_test_hanglietongji
 GROUP BY c1
union all
SELECT NULL, ‘total‘ AS c2, SUM(c3) AS c3
  FROM tsql_test_hanglietongji
 order by 1, 2

细心的朋友也许注意到了,第二个union all带了order by 1,2而上面的mysql没有带order by,这和mysql和oracle对NULL的默认排序规则有关。

使用普通sql重写rollup为:

SELECT nvl(c1, ‘total‘) AS total_c,
       SUM(decode(c2, ‘B1‘, c3, 0)) AS B1,
       SUM(decode(c2, ‘B2‘, c3, 0)) AS B2,
       SUM(decode(c2, ‘B3‘, c3, 0)) AS B3,
       SUM(decode(c2, ‘B4‘, c3, 0)) AS B4,
       SUM(decode(c2, ‘total‘, c3, 0)) AS total_r
  FROM (SELECT c1, nvl(c2, ‘total‘) AS c2, SUM(c3) AS c3
          FROM tsql_test_hanglietongji
         GROUP BY c1, c2
        HAVING c1 IS NOT NULL
        union all
        SELECT c1, nvl(null, ‘total‘) AS c2, SUM(c3) AS c3
          FROM tsql_test_hanglietongji
         GROUP BY c1
        HAVING c1 IS NOT NULL) A
 GROUP BY c1
union all
SELECT nvl(null, ‘total‘) AS total_c,
       SUM(decode(c2, ‘B1‘, c3, 0)) AS B1,
       SUM(decode(c2, ‘B2‘, c3, 0)) AS B2,
       SUM(decode(c2, ‘B3‘, c3, 0)) AS B3,
       SUM(decode(c2, ‘B4‘, c3, 0)) AS B4,
       SUM(decode(c2, ‘total‘, c3, 0)) AS total_r
  FROM (SELECT c1, nvl(c2, ‘total‘) AS c2, SUM(c3) AS c3
          FROM tsql_test_hanglietongji
         GROUP BY c1, c2
        HAVING c1 IS NOT NULL
        union all
        SELECT c1, nvl(null, ‘total‘) AS c2, SUM(c3) AS c3
          FROM tsql_test_hanglietongji
         GROUP BY c1
        HAVING c1 IS NOT NULL) A
 order by 1

这里也排除了c1 is null的情况。

通过上面的对比,发现oracle和mysql的rollup非常相似,对rollup函数感兴趣的朋友请仔细搜索rollup学习。

到这里该结束了,有任何意见请留言,如文中sql有错误也请指出,谢谢。

时间: 2024-10-06 17:35:13

Mysql,Oracle使用rollup函数完成行列统计的相关文章

mysql的with rollup

无意间发现了mysql的with rollup函数(用在group by 函数后面) 测试 1. SELECT NAME,DATE,score FROM stu 结果是 2. SELECT NAME,GROUP_CONCAT(DATE),SUM(score) FROM stu GROUP BY NAME 结果是 3.后面加上with rollup SELECT NAME,GROUP_CONCAT(DATE),SUM(score) FROM stu GROUP BY NAME WITH ROLLU

ORACLE rollup函数

rollup函数应用场景: 主要使用在 分组中,将每个分组求汇总值(就是小计),最后再讲所有值(除去小计)求和(就是合计) 当然,使用union 也可以达到同样的效果.先将需要查询的分组查出来,再union上和 然后再union上所有分组的和. 当然,分组比较多的时候,就吐血了. 实例: select nvl(type,'合计') type, case when type is null then null else nvl(class,'小计') end class,sum(grade) gr

MySQL创建字段+数据处理函数+汇总数据(聚集函数)+分组数据

[0]README 0.1)本文部分文字描述转自"MySQL 必知必会",旨在review"MySQL创建字段+数据处理函数+汇总数据(聚集函数)+分组数据" 的基础知识: [1]创建计算字段 1)problem+solution 1.1)problem:存储在表中的数据都不是应用程序所需要的.我们需要直接从数据库中检索出转换,计算或格式化过的数据: 1.2)solution:这就是计算字段发挥作用所在了, 计算字段是运行时在 select语句内创建的: 2)字段定

rollup函数 和cube函数 的区别?

有的小伙伴会发现在数据统计报表的时候会经常在最后对列进行一个汇总,那么在oracle中是那些函数来实现汇总的呢?今天就来讲一下rollup函数和cube函数的区分. 首先,创建一张表tmp1,数据如下: 那么,我们先看一下cube汇总出来的数据是什么样子的吧?select t_class,t_address,(t_number) t_number from tmp1 group by cube(t_class,t_address);有的小伙伴可能已经发现了,cube函数汇总的数据相当于把所有的可

springmvc4 mybatis 整合 框架源码 bootstrap html5 mysql oracle sqlsever spring SSM

获取[下载地址]   QQ: 313596790   [免费支持更新] 三大数据库 mysql  oracle  sqlsever   更专业.更强悍.适合不同用户群体 [新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统] A 集成代码生成器 [正反双向(单表.主表.明细表.树形表,开发利器)+快速构建表单; QQ:313596790 freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本,处理类,service等完整模块 B 集成阿里巴巴数据库连

mysql存储过程、存储函数及流程控制

存储过程 一.基本语法 create procedure sp_name([proc_parameter[,...]]) [characteristic...]routine_body begin end sp_name 表示存储过程的名字 proc_parameter 存储过程参数例表[IN OUT INOUT]三个部分组成 其中IN 表示传进来的参数 其中OUT 表示传出去的参数 其中INOUT 表示传进来但最终传回的参数 routine_body 参数是SQL代码的内容(类似于触发器的fo

Oracle时间日期函数

ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02   13:45:25为例)           Year:              yy two digits 两位年                显示值:07        yyy three digits 三位年                显示值:007        yyyy four digits 四位年                显示值:2007                    Month

重写Oracle的wm_concat函数,自定义分隔符、排序

oracle中,wm_concat函数是一个聚合函数,和mysql中的group_concat函数类似,不过group_concat函数比较强大,可以定义分隔符和排序,当然所谓强大是相对的,这里假使我们不知道oracle中的over函数,也不知道listagg函数. 我们先来看看wm_concat函数能实现什么功能,通俗点==>列传行,如果不明白,请看下面截图(可以看到分隔符默认为','顺序也是杂乱的)          所以,接下来,我们开始重写wm_concat函数(需要注意和需要说明的地方

springmvc+mybatis+spring 整合 bootstrap html5 mysql oracle SSM框架源码 SSH maven

获取[下载地址]   QQ: 313596790   [免费支持更新]支持三大数据库 mysql  oracle  sqlsever   更专业.更强悍.适合不同用户群体[新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统]A 代码生成器(开发利器);      增删改查的处理类,service层,mybatis的xml,SQL( mysql   和oracle)脚本,   jsp页面 都生成   就不用写搬砖的代码了,生成的放到项目里,可以直接运行B 阿里巴巴数据库连接池druid