標籤:
功能介紹:
首先是進行無欄位的彙總,然後在對欄位進行從左至右依次組合後彙總
建立表:
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