濮阳杆衣贸易有限公司

主頁 > 知識庫 > Oracle分組函數(shù)之ROLLUP的基本用法

Oracle分組函數(shù)之ROLLUP的基本用法

熱門標(biāo)簽:網(wǎng)貸外呼系統(tǒng)合法嗎 地圖標(biāo)注坐標(biāo)圖標(biāo) 杭州網(wǎng)絡(luò)外呼系統(tǒng)運(yùn)營商 手機(jī)地圖標(biāo)注門店 鶴壁電話機(jī)器人價格 地圖標(biāo)注效果的制作 安陽企業(yè)電銷機(jī)器人供應(yīng)商 汽車4s店百度地圖標(biāo)注店 電銷套路機(jī)器人

rollup函數(shù)

本博客簡單介紹一下oracle分組函數(shù)之rollup的用法,rollup函數(shù)常用于分組統(tǒng)計,也是屬于oracle分析函數(shù)的一種

環(huán)境準(zhǔn)備

create table dept as select * from scott.dept;
create table emp as select * from scott.emp;

業(yè)務(wù)場景:求各部門的工資總和及其所有部門的工資總和

這里可以用union來做,先按部門統(tǒng)計工資之和,然后在統(tǒng)計全部部門的工資之和

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);

業(yè)務(wù)場景:基于上面的統(tǒng)計,再加需求,現(xiàn)在要看看每個部門崗位對應(yīng)的工資之和

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實(shí)現(xiàn),語法更簡單

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);

假如再加個時間統(tǒng)計的,可以用下面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函數(shù)

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

函數(shù)是維度更細(xì)的統(tǒng)計,語法和rollup類似

假設(shè)有n個維度,那么rollup會有n個聚合,cube會有2n個聚合

rollup統(tǒng)計列

rollup(a,b) 統(tǒng)計列包含:(a,b)、(a)、()

rollup(a,b,c) 統(tǒng)計列包含:(a,b,c)、(a,b)、(a)、()

....

cube統(tǒng)計列

cube(a,b) 統(tǒng)計列包含:(a,b)、(a)、(b)、()

cube(a,b,c) 統(tǒng)計列包含:(a,b,c)、(a,b)、(a,c)、(b,c)、(a)、(b)、(c)、()

....

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。

您可能感興趣的文章:
  • Oracle數(shù)據(jù)庫按時間進(jìn)行分組統(tǒng)計數(shù)據(jù)的方法

標(biāo)簽:南陽 泰安 梧州 酒泉 銀川 焦作 河源 柳州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Oracle分組函數(shù)之ROLLUP的基本用法》,本文關(guān)鍵詞  Oracle,分組,函數(shù),之,ROLLUP,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Oracle分組函數(shù)之ROLLUP的基本用法》相關(guān)的同類信息!
  • 本頁收集關(guān)于Oracle分組函數(shù)之ROLLUP的基本用法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    永丰县| 响水县| 塔河县| 横峰县| 璧山县| 罗平县| 安丘市| 咸丰县| 万载县| 古丈县| 晋江市| 桑日县| 会理县| 咸丰县| 霍邱县| 米易县| 藁城市| 孟津县| 甘肃省| 七台河市| 普定县| 抚宁县| 肥西县| 南召县| 桃源县| 望谟县| 阿拉尔市| 古浪县| 城市| 罗平县| 会理县| 博罗县| 巴彦淖尔市| 克东县| 麦盖提县| 文昌市| 清流县| 宝坻区| 大英县| 华蓥市| 台南县|