Oracle SQL中的彙總Group by, Rollup 與 Grouping等函數聯用示例

Oracle 中,分組彙總函數 Group by,可以通過Rollup實現自動小結式的彙總,並且可以指定多個列,進行逐級彙總小計。以下整理了簡單示例,以備不時之需。

Group by示例

1、數據示例

select * from (
  select 'A000001' pno, 'Z9' type1, 'Z91' type2, 'Z911' type3, 100 amt from dual union all
  select 'A000001' pno, 'Z9' type1, 'Z91' type2, 'Z912' type3, 100 amt from dual union all
  select 'A000001' pno, 'Z9' type1, 'Z91' type2, 'Z913' type3, 100 amt from dual union all
  select 'A000001' pno, 'Z9' type1, 'Z92' type2, ''     type3, 80  amt from dual union all
  select 'A000001' pno, 'Z8' type1, ''    type2, ''     type3, 60  amt from dual
) a;

查詢結果:
在這裏插入圖片描述

2、按照 type1、type2、type3彙總,其中,對type3進行 rollup

select a.pno, a.type1, a.type2, a.type3, sum(a.amt)
 from (
select 'A000001' pno, 'Z9' type1, 'Z91' type2, 'Z911' type3, 100 amt from dual union all
select 'A000001' pno, 'Z9' type1, 'Z91' type2, 'Z912' type3, 100 amt from dual union all
select 'A000001' pno, 'Z9' type1, 'Z91' type2, 'Z913' type3, 100 amt from dual union all
select 'A000001' pno, 'Z9' type1, 'Z92' type2, ''     type3, 80  amt from dual union all
select 'A000001' pno, 'Z8' type1, ''    type2, ''     type3, 60  amt from dual
) a
group by a.pno, a.type1, A.type2, rollup( A.type3);

查詢結果:
在這裏插入圖片描述

3、對type2, type3進行 rollup

select a.pno, a.type1, a.type2, a.type3, sum(a.amt)
 from (
select 'A000001' pno, 'Z9' type1, 'Z91' type2, 'Z911' type3, 100 amt from dual union all
select 'A000001' pno, 'Z9' type1, 'Z91' type2, 'Z912' type3, 100 amt from dual union all
select 'A000001' pno, 'Z9' type1, 'Z91' type2, 'Z913' type3, 100 amt from dual union all
select 'A000001' pno, 'Z9' type1, 'Z92' type2, ''     type3, 80  amt from dual union all
select 'A000001' pno, 'Z8' type1, ''    type2, ''     type3, 60  amt from dual
) a
group by a.pno, a.type1, rollup(A.type2,  A.type3);

查詢結果:
在這裏插入圖片描述

**4、rollup後使用grouping函數查看彙總行

select a.pno, a.type1, a.type2, a.type3, sum(a.amt), grouping(a.type1) g1, grouping(a.type2) g2, grouping(a.type3) g3
 from (
select 'A000001' pno, 'Z9' type1, 'Z91' type2, 'Z911' type3, 100 amt from dual union all
select 'A000001' pno, 'Z9' type1, 'Z91' type2, 'Z912' type3, 100 amt from dual union all
select 'A000001' pno, 'Z9' type1, 'Z91' type2, 'Z913' type3, 100 amt from dual union all
select 'A000001' pno, 'Z9' type1, 'Z92' type2, ''     type3, 80  amt from dual union all
select 'A000001' pno, 'Z8' type1, ''    type2, ''     type3, 60  amt from dual
) a
group by a.pno, a.type1, rollup(A.type2,  A.type3);

查詢結果:
在這裏插入圖片描述

**5、對比grouping_id()函數和grouping()函數的差別

select a.pno, a.type1, a.type2, a.type3, sum(a.amt), grouping(a.type1) g1, grouping(a.type2) g2, grouping(a.type3) g3
 from (
select 'A000001' pno, 'Z9' type1, 'Z91' type2, 'Z911' type3, 100 amt from dual union all
select 'A000001' pno, 'Z9' type1, 'Z91' type2, 'Z912' type3, 100 amt from dual union all
select 'A000001' pno, 'Z9' type1, 'Z91' type2, 'Z913' type3, 100 amt from dual union all
select 'A000001' pno, 'Z9' type1, 'Z92' type2, ''     type3, 80  amt from dual union all
select 'A000001' pno, 'Z8' type1, ''    type2, ''     type3, 60  amt from dual
) a
group by a.pno, a.type1, rollup(A.type2,  A.type3);

查詢結果:
在這裏插入圖片描述

Group by的配套函數介紹

Grouping()

grouping()函數的參數只有一個,而且必須爲group by中出現的某一列。表示結果集的一行是否對該列做了grouping,若是,則返回1,否則爲0。

Grouping_id()

grouping_id()的參數可以是多個,但必須爲group by中出現的列。Grouping_id()的返回值其實就是參數中的每列的grouping()值的二進制向量,例如如果grouping(A)=1,grouping(B)=0,則grouping_id(A,B)的返回值就是二進制的10,轉成10進制就是2。
本例中,grouping_id爲3的,即對應二進制:11,表示兩列都發生了grouping。

Group_id()

group_id()無參數,group by對某些列的集合會進行重複的grouping,而實際上絕大多數情況下對結果集中的這些重複行是不需要的,那就必須有辦法剔出這些重複grouping的行。當結果集中有n條重複grouping而形成的行時,每行的group_id()分別是0,1,…,n,這樣我們在條件中加入一個group_id()<1就可以剔出這些重複grouping的行了。
對於上述示例,並沒有發生重複行的情況,若使用group_id,得到的結果如下:
在這裏插入圖片描述

延伸用法

Cube

cube的意思是立方,對cube的每個參數,都可以理解爲取值爲參與grouping和不參與grouping兩個值的一個維度,然後所有維度取值組合的集合就是grouping的集合,對於n個參數的cube,有2的n 次方的grouping。
例如,group by cube(A,B,C) ,等效於:首先對(A、B、C)進行GROUP BY,然後依次是(A、B),(A、C),(A),(B、C),(B),©,最後對全表進行GROUP BY操作,一共是2^3=8次grouping。同rollup一樣,也可以用基本的group by加上結果集的union all寫出一個與group by cube結果集相同的sql。

Grouping sets

grouping sets就是對參數中的每個參數做grouping,也就是有幾個參數做幾次grouping,例如使用group by grouping sets(A,B,C),則對(A),(B),©進行group by,如果使用group by grouping sets((A,B),C),則對(A,B),©進行group by。甚至grouping by grouping set(A,A)都是語法允許的,也就是對(A)進行2次group by,grouping sets的參數允許重複。

對比:rollup,N+1個分組方案;cube,2^N個分組方案;grouping sets,自定義羅列出分組方案。

示例待補充。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章