国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > Oracle > 正文

Oracle分組函數之ROLLUP的基本用法

2020-07-26 13:58:21
字體:
來源:轉載
供稿:網友

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.dnameunion allselect 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.jobunion all//各部門的工資之和select a.dname, null, sum(b.sal) from scott.dept a, scott.emp b where a.deptno = b.deptno group by a.dnameunion 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)、()

....

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新建县| 和龙市| 海阳市| 筠连县| 简阳市| 金湖县| 波密县| 彭州市| 象州县| 丰镇市| 安化县| 汉阴县| 彩票| 西丰县| 蕲春县| 泉州市| 晋江市| 巩留县| 西乡县| 岑巩县| 古田县| 平罗县| 利津县| 玉田县| 新野县| 绥宁县| 兰坪| 宁波市| 云安县| 高州市| 蕉岭县| 承德县| 乐清市| 瓦房店市| 射阳县| 通许县| 义乌市| 渑池县| 新河县| 鹰潭市| 海南省|