最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2019年07月05日 17:19:08   作者:smileNicky  
這篇文章主要給大家介紹了關(guān)于Oracle分組函數(shù)之ROLLUP的基本用法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Oracle數(shù)據(jù)庫具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

rollup函數(shù)

本博客簡單介紹一下oracle分組函數(shù)之rollup的用法,rollup函數(shù)常用于分組統(tǒng)計(jì),也是屬于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)計(jì)工資之和,然后在統(tǒng)計(jì)全部部門的工資之和

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)計(jì),再加需求,現(xiàn)在要看看每個(gè)部門崗位對應(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);

假如再加個(gè)時(shí)間統(tǒng)計(jì)的,可以用下面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)計(jì),語法和rollup類似

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

rollup統(tǒng)計(jì)列

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

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

....

cube統(tǒng)計(jì)列

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

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

....

總結(jié)

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

相關(guān)文章

最新評論

大同县| 宁波市| 安阳市| 峨边| 合作市| 郎溪县| 巢湖市| 铁力市| 微山县| 南开区| 大理市| 呼和浩特市| 威海市| 景洪市| 宁城县| 运城市| 五家渠市| 六安市| 阿拉善左旗| 梁平县| 什邡市| 龙川县| 长乐市| 阿克| 宁夏| 汝州市| 南昌市| 泸西县| 沽源县| 封开县| 海宁市| 中西区| 即墨市| 凤城市| 怀集县| 灌云县| 绵阳市| 横山县| 将乐县| 璧山县| 高邮市|