Oracle分组函数之ROLLUP用法
rollup函数
本博客简单介绍一下oracle分组函数之rollup的用法,rollup函数常用于分组统计,也是属于oracle分析函数的一种
环境准备
create table dept as select * from scott.dept;
create table emp as select * from scott.emp;
业务场景:求各部门的工资总和及其所有部门的工资总和
这里可以用union来做,先按部门统计工资之和,然后在统计全部部门的工资之和
select a.dname, sum(b.sal)
from scott.dept a, scott.emp b
where a.deptno = b.deptno
group by a.dname
union all
select null, sum(b.sal)
from scott.dept a, scott.emp b
where a.deptno = b.deptno;
上面是用union来做,然后用rollup来做,语法更简单,而且性能更好
select a.dname, sum(b.sal)
from scott.dept a, scott.emp b
where a.deptno = b.deptno
group by rollup(a.dname);

业务场景:基于上面的统计,再加需求,现在要看看每个部门岗位对应的工资之和
select a.dname, b.job, sum(b.sal)
from scott.dept a, scott.emp b
where a.deptno = b.deptno
group by a.dname, b.job
union all//各部门的工资之和
select a.dname, null, sum(b.sal)
from scott.dept a, scott.emp b
where a.deptno = b.deptno
group by a.dname
union all//所有部门工资之和
select null, null, sum(b.sal)
from scott.dept a, scott.emp b
where a.deptno = b.deptno;
用rollup实现,语法更简单
select a.dname, b.job, sum(b.sal)
from scott.dept a, scott.emp b
where a.deptno = b.deptno
group by rollup(a.dname, b.job);

假如再加个时间统计的,可以用下面sql:
select to_char(b.hiredate, 'yyyy') hiredate, a.dname, b.job, sum(b.sal)
from scott.dept a, scott.emp b
where a.deptno = b.deptno
group by rollup(to_char(b.hiredate, 'yyyy'), a.dname, b.job);
cube函数
select a.dname, b.job, sum(b.sal)
from scott.dept a, scott.emp b
where a.deptno = b.deptno
group by cube(a.dname, b.job);

cube函数是维度更细的统计,语法和rollup类似
假设有n个维度,那么rollup会有n个聚合,cube会有2n个聚合
rollup统计列
rollup(a,b) 统计列包含:(a,b)、(a)、()
rollup(a,b,c) 统计列包含:(a,b,c)、(a,b)、(a)、()
....cube统计列
cube(a,b) 统计列包含:(a,b)、(a)、(b)、()
cube(a,b,c) 统计列包含:(a,b,c)、(a,b)、(a,c)、(b,c)、(a)、(b)、(c)、()
....
Oracle分组函数之ROLLUP用法的更多相关文章
- [转]【ROLLUP】Oracle分组函数之ROLLUP魅力
原创:http://blog.itpub.net/519536/viewspace-610995 本文通过演示给出Oracle ROLLUP分组函数的用法,体验一下Oracle在统计查询领域中的函数魅 ...
- Oracle分组函数之ROLLUP
功能介绍: 首先是进行无字段的聚合,然后在对字段进行从左到右依次组合后聚合 创建表: Create Table score ( classID Int, studentName ), subject ...
- 【转】【CUBE】Oracle分组函数之CUBE魅力
http://blog.itpub.net/519536/viewspace-610997/ Oracle的CUBE与ROLLUP功能很相似,也是在数据统计分析领域的一把好手. 关于ROLLUP的查 ...
- Oracle分组函数之CUBE魅力
Oracle的CUBE与ROLLUP功能很相似,也是在数据统计分析领域的一把好手. 关于ROLLUP的查询统计功能请参考文章<Oracle分组函数之ROLLUP魅力>(http://www ...
- oracle 分组函数执行分析
先上例了: select job as "JOB1", avg(sal) as "avg sal" from scott.emp group by " ...
- Oracle分组函数以及数据分组
简单总结一下对于数据的分组和分组函数. 本文所举实例,数据来源oracle用户scott下的emp,dept ,salgrade 3表:数据如下: 一.分组函数 1.sum()求和函数.max()求最 ...
- Oracle 分组函数
分组函数的介绍 分组函数作用于一组数据,并对一组数据返回一个值. (引用网上的一张图) 分组函数的使用规则 SELECT [column,] group_function(column) FROM t ...
- Oracle分组函数实例
分组函数也叫聚合函数.如果在查询只想要查分组函数,那么跟平时的查询语句并无不同: SQL ,,,,) ; SUM(T.PRIZENUM) AVG(T.PRIZENUM) --------------- ...
- oracle 分组函数、视图
组函数 分组函数作用于一组数据,对每一组返回一个值 组函数类型: 1.计数 count(列名 或 表达式) 对满足的行数进行统计 2.求和 sum(列名 或 表达式 ...
随机推荐
- CodeForces Round#229 DIV2 C 递归DP
这条路是只说哦话题,没有注意到k只有最大射程10,所以昨天晚上,一个很长的纠结.没有好的办法来处理,后来不情愿地去寻找解决问题的办法,研究发现,人们对开始到句子,由于k的范围比较小 所以....... ...
- [C/C++]_[VS2010使用源代码UTF8中国字符串转码ANSI问题]
场景: 1.思想vs设置源文件UTF8编码,的代码串中国出现在它必须是utf8编码.不幸的是没有,假设源代码出现在中国字符串,在内存公交码ANSI编码. Unicode(UTF8) 代码页(65001 ...
- A Byte of Python(简明Python教程) for Python 3.0 下载
A Byte of Python v1.92 (for Python 3.0) 官方下载地址,当前(20120730) 最新版本 1.92 基于Python3的 下载: http://files.s ...
- 基于WPF实现双色球
原文:基于WPF实现双色球 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37591671/article/details/82959625 ...
- WPF-Button|IsCancel&&IsDefault
原文:WPF-Button|IsCancel&&IsDefault Button个别属性 <Button ToolTip="ESC" IsDefault=&q ...
- WPF DataGrid 的RowDetailsTemplate的使用
<Window x:Class="DataGridExam.DataGridRowDetailsExam" xmlns="http://schemas ...
- 客户端技术的一点思考(数据存储用SQLite, XMPP通讯用Gloox, Web交互用LibCurl, 数据打包用Protocol Buffer, socket通讯用boost asio)
今天看到CSDN上这么一篇< 彻底放弃没落的MFC,对新人的忠告!>, 作为一个一直在Windows上搞客户端开发的C++程序员,几年前也有过类似的隐忧(参见 落伍的感觉), 现在却有一些 ...
- 关于SetLength报Out of memory的研究及解决办法
关于SetLength报Out of memory的研究及解决办法 最近在做一个GIS系统, 在读GIS数据时采用了动态数组,突然读一个数据时SetLength报错!Out of memory 仔细研 ...
- SQLServer 可更新订阅数据在线架构更改(增加字段)方案
原文:SQLServer 可更新订阅数据在线架构更改(增加字段)方案 之前一直查找冲突发布和订阅数据不一致的原因,后来发现多少数据库升级引起,因为一直以来都是在发布数据库增加字段,订阅也会自动同步.在 ...
- WPF Textblock Run 空白问题
消除Run之前的空白是将Run标签布局时头尾相连 如: <TextBlock > <Run Text="A"></Run> <Run Te ...