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用法的更多相关文章

  1. [转]【ROLLUP】Oracle分组函数之ROLLUP魅力

    原创:http://blog.itpub.net/519536/viewspace-610995 本文通过演示给出Oracle ROLLUP分组函数的用法,体验一下Oracle在统计查询领域中的函数魅 ...

  2. Oracle分组函数之ROLLUP

    功能介绍: 首先是进行无字段的聚合,然后在对字段进行从左到右依次组合后聚合 创建表: Create Table score ( classID Int, studentName ), subject ...

  3. 【转】【CUBE】Oracle分组函数之CUBE魅力

    http://blog.itpub.net/519536/viewspace-610997/ Oracle的CUBE与ROLLUP功能很相似,也是在数据统计分析领域的一把好手.  关于ROLLUP的查 ...

  4. Oracle分组函数之CUBE魅力

    Oracle的CUBE与ROLLUP功能很相似,也是在数据统计分析领域的一把好手. 关于ROLLUP的查询统计功能请参考文章<Oracle分组函数之ROLLUP魅力>(http://www ...

  5. oracle 分组函数执行分析

    先上例了: select job as "JOB1", avg(sal) as "avg sal" from scott.emp group by " ...

  6. Oracle分组函数以及数据分组

    简单总结一下对于数据的分组和分组函数. 本文所举实例,数据来源oracle用户scott下的emp,dept ,salgrade 3表:数据如下: 一.分组函数 1.sum()求和函数.max()求最 ...

  7. Oracle 分组函数

    分组函数的介绍 分组函数作用于一组数据,并对一组数据返回一个值. (引用网上的一张图) 分组函数的使用规则 SELECT [column,] group_function(column) FROM t ...

  8. Oracle分组函数实例

    分组函数也叫聚合函数.如果在查询只想要查分组函数,那么跟平时的查询语句并无不同: SQL ,,,,) ; SUM(T.PRIZENUM) AVG(T.PRIZENUM) --------------- ...

  9. oracle 分组函数、视图

    组函数 分组函数作用于一组数据,对每一组返回一个值 组函数类型: 1.计数        count(列名 或 表达式)     对满足的行数进行统计 2.求和        sum(列名 或 表达式 ...

随机推荐

  1. 线性滤波器(linear filter)与非线性滤波器(non-linear filter)

    1. 均值滤波器与中值滤波器 image processing - Difference between linear and non linear filter - Signal Processin ...

  2. 简明Python3教程 3.介绍

    介绍 Python是少有的几种既强大又简单的编程语言.你将惊喜地发现通过使用Python即可轻松专注于解决问题而非和你所用的语言格式与结构. 下面是Python的官方介绍: Python is an ...

  3. 乞讨 间隔[a,b]在见面p^k*q*^m(k&gt;m)中数号码

    标题叙述性说明: 1<=a,b<=10^18,p,q他们是素数  2<=p,q<=10^9; 求在[a,b]内能够表示为  x*p^k*q^m  k > m   的数的个 ...

  4. cacti由snmp监控带宽

    1.安装和配置snmp a. yum install -y net-snmp net-snmp-utils b. chkconfig snmpd on c. chkconfig –list|grep ...

  5. matlab 基础知识class &lt; superclass_name

    matlab中的class,在matlab中也能够通过创建类的方式实现面向对象编程. 1)类定义 >> classdef (attribute1 = value,...) classnam ...

  6. OpenGL(十八) 顶点数组和抗锯齿(反走样)设置

    顶点数组函数可以在一个数组里包含大量的与顶点相关的数据,并且可以减少函数的调用.使用顶点数组需要先启用顶点数组功能,使用glEnableClientState函数启用顶点数组,参数可以是GL_VERT ...

  7. Linux 下非 root 用户安装 theano(配置 GPU)

    非 root 用户,安装 Python 第三方的包,尤其像 theano,存在大量的依赖项,存在的主要问题,是安装各个包时的权限问题.所幸,存在这样一个集成工具,叫 anaconda,其已经内置了许多 ...

  8. React学习(3)——ref,key,PureComponent,bindActionCreator

    ref 如果在html里设置ref那么它就指向这个真实的DOM节点. 如果在组件里设置ref,那么它就指向这个组件实例的引用,和组件里面的this互等. 我们经常在表单input,select里使用, ...

  9. Bootstrap路径导航

    @{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport&q ...

  10. 最好的方式是用VirtualAlloc分配虚拟内存,它既不是在堆也不是在栈,而是直接在进程的地址空间中保留一块内存

    申请效率的比较 栈:由系统自动分配,速度较快.但程序员是无法控制的. 堆:是由new分配的内存,最好的方式是用VirtualAlloc分配虚拟内存,它既不是在堆也不是在栈,而是直接在进程的地址空间中保 ...