Oracle分組函數之ROLLUP

來源:互聯網
上載者:User

標籤:

功能介紹:

首先是進行無欄位的彙總,然後在對欄位進行從左至右依次組合後彙總

 

建立表:

1 Create Table score2 (3   classID Int,4   studentName Varchar2(50),5   subject varchar2(50),6   score Int7 );
View Code

 

插入測試資料:

 1 Insert Into score values (001,‘小徐‘,‘語文‘,87); 2 Insert Into score values (001,‘小徐‘,‘數學‘,98); 3 Insert Into score values (001,‘小徐‘,‘外語‘,99); 4 Insert Into score values (002,‘小吳‘,‘語文‘,80); 5 Insert Into score values (002,‘小吳‘,‘數學‘,74); 6 Insert Into score values (002,‘小吳‘,‘外語‘,65); 7 Insert Into score values (003,‘小張‘,‘語文‘,89); 8 Insert Into score values (003,‘小張‘,‘數學‘,78); 9 Insert Into score values (003,‘小張‘,‘外語‘,84);10 Insert Into score values (004,‘小孫‘,‘語文‘,100);11 Insert Into score values (004,‘小孫‘,‘數學‘,100);12 Insert Into score values (004,‘小孫‘,‘外語‘,100);13 Insert Into score values (001,‘小彭‘,‘語文‘,87);14 Insert Into score values (001,‘小彭‘,‘數學‘,99);15 Insert Into score values (001,‘小彭‘,‘外語‘,65);16 Insert Into score values (004,‘小葉‘,‘語文‘,100);17 Insert Into score values (004,‘小葉‘,‘數學‘,100);18 Insert Into score values (004,‘小葉‘,‘外語‘,100);19 Insert Into score values (003,‘小劉‘,‘語文‘,79);20 Insert Into score values (003,‘小劉‘,‘數學‘,90);21 Insert Into score values (003,‘小劉‘,‘外語‘,65);22 Insert Into score values (002,‘小童‘,‘語文‘,96);23 Insert Into score values (002,‘小童‘,‘數學‘,93);24 Insert Into score values (002,‘小童‘,‘外語‘,97);
View Code

 

普通分組函數,統計每個班級的總分:

Select t.Classid, Sum(t.Score) From Score t Group By t.Classid;

 

查詢結果:

 

加上Rollup,統計每個班級的總分和所有班級的總分:

Select t.Classid, Sum(t.Score) From Score t Group By Rollup(t.Classid);

 

查詢結果:

 

先進行無欄位的彙總(1),再對Classid彙總(3),相當於:

1 Select Null, Sum(t.Score) From Score t2 Union All3 Select t.Classid, Sum(t.Score) From Score t Group By t.Classid;

 

在看看兩個欄位的,統計每個班級的總分、所有班級的總分和每個學生的總成績:

Select t.classid,t.studentname,Sum(t.score) From Score t Group By Rollup(t.classid,t.studentname); 

 

查詢結果:

 

先進行無欄位的彙總(1),再對Classid彙總(3),在對Classid和Studentname組合彙總,相當於:

1 Select Null, Null, Sum(t.Score) From Score t2 Union All3 Select t.Classid, Null, Sum(t.Score) From Score t Group By t.Classid4 Union All5 Select t.Classid, t.Studentname, Sum(t.Score) From Score t Group By t.Classid, t.Studentname

 

Oracle分組函數之ROLLUP

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.