一句Sql把縱向表轉(zhuǎn)為橫向表,并分別分組求平均和總平均值
更新時(shí)間:2010年06月29日 09:04:19 作者:
一句Sql把縱向表轉(zhuǎn)為橫向表,并分別分組求平均和總平均值,需要的朋友可以參考下。
效果如圖所示:

測(cè)試sql語(yǔ)句如下:
declare @tab table(Class varchar(20),Student varchar(20),Course varchar(50),Quantity decimal(7,2));
insert into @tab(Class,Student,Course,Quantity) values('A班','張三','語(yǔ)文',60);
insert into @tab(Class,Student,Course,Quantity) values('A班','張三','數(shù)學(xué)',70);
insert into @tab(Class,Student,Course,Quantity) values('A班','張三','英語(yǔ)',80);
insert into @tab(Class,Student,Course,Quantity) values('A班','李四','語(yǔ)文',30);
insert into @tab(Class,Student,Course,Quantity) values('A班','李四','數(shù)學(xué)',40);
insert into @tab(Class,Student,Course,Quantity) values('A班','李四','英語(yǔ)',50);
insert into @tab(Class,Student,Course,Quantity) values('B班','王五','語(yǔ)文',65);
insert into @tab(Class,Student,Course,Quantity) values('B班','王五','數(shù)學(xué)',75);
insert into @tab(Class,Student,Course,Quantity) values('B班','王五','英語(yǔ)',85);
insert into @tab(Class,Student,Course,Quantity) values('B班','趙六','語(yǔ)文',35);
insert into @tab(Class,Student,Course,Quantity) values('B班','趙六','數(shù)學(xué)',45);
insert into @tab(Class,Student,Course,Quantity) values('B班','趙六','英語(yǔ)',55);
select * from @tab
select
(case when Grouping(Class)=1 then '總平均' when Grouping(Student)=1 then '' else Class end ) as Class
,(case when Grouping(Class)=1 then '' when Grouping(Student)=1 then '平均' else Student end) as Student
,avg(語(yǔ)文) as 語(yǔ)文
,avg(數(shù)學(xué)) as 數(shù)學(xué)
,avg(英語(yǔ)) as 英語(yǔ)
,avg(總分) as 總分
from (
select Class,Student
,(select isnull(sum(Quantity),0) from @tab where Class=t.Class and Student=t.Student and Course='語(yǔ)文') as '語(yǔ)文'
,(select isnull(sum(Quantity),0) from @tab where Class=t.Class and Student=t.Student and Course='數(shù)學(xué)') as '數(shù)學(xué)'
,(select isnull(sum(Quantity),0) from @tab where Class=t.Class and Student=t.Student and Course='英語(yǔ)') as '英語(yǔ)'
,(select isnull(sum(Quantity),0) from @tab where Class=t.Class and Student=t.Student) as '總分'
from @tab as t
group by Class,Student
) as tempTab
group by Class,Student,語(yǔ)文,數(shù)學(xué),英語(yǔ),總分 with rollup
having Grouping(語(yǔ)文)=1
and Grouping(數(shù)學(xué))=1
and Grouping(英語(yǔ))=1

測(cè)試sql語(yǔ)句如下:
復(fù)制代碼 代碼如下:
declare @tab table(Class varchar(20),Student varchar(20),Course varchar(50),Quantity decimal(7,2));
insert into @tab(Class,Student,Course,Quantity) values('A班','張三','語(yǔ)文',60);
insert into @tab(Class,Student,Course,Quantity) values('A班','張三','數(shù)學(xué)',70);
insert into @tab(Class,Student,Course,Quantity) values('A班','張三','英語(yǔ)',80);
insert into @tab(Class,Student,Course,Quantity) values('A班','李四','語(yǔ)文',30);
insert into @tab(Class,Student,Course,Quantity) values('A班','李四','數(shù)學(xué)',40);
insert into @tab(Class,Student,Course,Quantity) values('A班','李四','英語(yǔ)',50);
insert into @tab(Class,Student,Course,Quantity) values('B班','王五','語(yǔ)文',65);
insert into @tab(Class,Student,Course,Quantity) values('B班','王五','數(shù)學(xué)',75);
insert into @tab(Class,Student,Course,Quantity) values('B班','王五','英語(yǔ)',85);
insert into @tab(Class,Student,Course,Quantity) values('B班','趙六','語(yǔ)文',35);
insert into @tab(Class,Student,Course,Quantity) values('B班','趙六','數(shù)學(xué)',45);
insert into @tab(Class,Student,Course,Quantity) values('B班','趙六','英語(yǔ)',55);
select * from @tab
select
(case when Grouping(Class)=1 then '總平均' when Grouping(Student)=1 then '' else Class end ) as Class
,(case when Grouping(Class)=1 then '' when Grouping(Student)=1 then '平均' else Student end) as Student
,avg(語(yǔ)文) as 語(yǔ)文
,avg(數(shù)學(xué)) as 數(shù)學(xué)
,avg(英語(yǔ)) as 英語(yǔ)
,avg(總分) as 總分
from (
select Class,Student
,(select isnull(sum(Quantity),0) from @tab where Class=t.Class and Student=t.Student and Course='語(yǔ)文') as '語(yǔ)文'
,(select isnull(sum(Quantity),0) from @tab where Class=t.Class and Student=t.Student and Course='數(shù)學(xué)') as '數(shù)學(xué)'
,(select isnull(sum(Quantity),0) from @tab where Class=t.Class and Student=t.Student and Course='英語(yǔ)') as '英語(yǔ)'
,(select isnull(sum(Quantity),0) from @tab where Class=t.Class and Student=t.Student) as '總分'
from @tab as t
group by Class,Student
) as tempTab
group by Class,Student,語(yǔ)文,數(shù)學(xué),英語(yǔ),總分 with rollup
having Grouping(語(yǔ)文)=1
and Grouping(數(shù)學(xué))=1
and Grouping(英語(yǔ))=1
您可能感興趣的文章:
- sql分組后二次匯總(處理表重復(fù)記錄查詢和刪除)的實(shí)現(xiàn)方法
- SQL SERVER 分組求和sql語(yǔ)句
- 顯示同一分組中的其他元素的sql語(yǔ)句
- sql獲取分組排序后數(shù)據(jù)的腳本
- SQL進(jìn)行排序、分組、統(tǒng)計(jì)的10個(gè)新技巧分享
- SQL分組排序去重復(fù)的小實(shí)例
- 以數(shù)據(jù)庫(kù)字段分組顯示數(shù)據(jù)的sql語(yǔ)句(詳細(xì)介紹)
- SQL中Group分組獲取Top N方法實(shí)現(xiàn)可首選row_number
- Sql Server:多行合并成一行,并做分組統(tǒng)計(jì)的兩個(gè)方法
- Sql Server 分組統(tǒng)計(jì)并合計(jì)總數(shù)及WITH ROLLUP應(yīng)用
- SQL語(yǔ)句分組獲取記錄的第一條數(shù)據(jù)的方法
- sqlserver巧用row_number和partition by分組取top數(shù)據(jù)
- sql 分組查詢問題
- SQLserver 實(shí)現(xiàn)分組統(tǒng)計(jì)查詢(按月、小時(shí)分組)
- 分組后分組合計(jì)以及總計(jì)SQL語(yǔ)句(稍微整理了一下)
相關(guān)文章
SQL Server一個(gè)字符串拆分多行顯示或者多行數(shù)據(jù)合并成一個(gè)字符串
這篇文章介紹了SQL Server一個(gè)字符串拆分多行顯示或者多行數(shù)據(jù)合并成一個(gè)字符串的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05SqlServer創(chuàng)建自動(dòng)收縮事務(wù)日志任務(wù)的圖文教程
SQL Server數(shù)據(jù)庫(kù)存在一個(gè)問題,如果你限制了它的日志文件的大小,那么當(dāng)數(shù)據(jù)庫(kù)日志達(dá)到這個(gè)大小的時(shí)候,數(shù)據(jù)庫(kù)就會(huì)停止寫入日志,下面這篇文章主要給大家介紹了關(guān)于SqlServer創(chuàng)建自動(dòng)收縮事務(wù)日志任務(wù)的相關(guān)資料,需要的朋友可以參考下2022-09-09SQL?Server開發(fā)智能提示插件SQL?Prompt介紹
這篇文章介紹了SQL?Server開發(fā)智能提示插件SQL?Prompt,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05數(shù)據(jù)庫(kù)常用的sql語(yǔ)句匯總
這篇文章主要介紹了數(shù)據(jù)庫(kù)常用的sql語(yǔ)句匯總,需要的朋友可以參考下2020-02-02sqlserver數(shù)據(jù)庫(kù)大型應(yīng)用解決方案經(jīng)驗(yàn)總結(jié)
對(duì)于一個(gè)大型的互聯(lián)網(wǎng)應(yīng)用,每天百萬(wàn)級(jí)甚至上億的PV無(wú)疑對(duì)數(shù)據(jù)庫(kù)造成了相當(dāng)高的負(fù)載。對(duì)于系統(tǒng)的穩(wěn)定性和擴(kuò)展性造成了極大的問題2013-10-10