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

測試sql語句如下:
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班','張三','語文',60);
insert into @tab(Class,Student,Course,Quantity) values('A班','張三','數(shù)學(xué)',70);
insert into @tab(Class,Student,Course,Quantity) values('A班','張三','英語',80);
insert into @tab(Class,Student,Course,Quantity) values('A班','李四','語文',30);
insert into @tab(Class,Student,Course,Quantity) values('A班','李四','數(shù)學(xué)',40);
insert into @tab(Class,Student,Course,Quantity) values('A班','李四','英語',50);
insert into @tab(Class,Student,Course,Quantity) values('B班','王五','語文',65);
insert into @tab(Class,Student,Course,Quantity) values('B班','王五','數(shù)學(xué)',75);
insert into @tab(Class,Student,Course,Quantity) values('B班','王五','英語',85);
insert into @tab(Class,Student,Course,Quantity) values('B班','趙六','語文',35);
insert into @tab(Class,Student,Course,Quantity) values('B班','趙六','數(shù)學(xué)',45);
insert into @tab(Class,Student,Course,Quantity) values('B班','趙六','英語',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(語文) as 語文
,avg(數(shù)學(xué)) as 數(shù)學(xué)
,avg(英語) as 英語
,avg(總分) as 總分
from (
select Class,Student
,(select isnull(sum(Quantity),0) from @tab where Class=t.Class and Student=t.Student and Course='語文') as '語文'
,(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='英語') as '英語'
,(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,語文,數(shù)學(xué),英語,總分 with rollup
having Grouping(語文)=1
and Grouping(數(shù)學(xué))=1
and Grouping(英語)=1

測試sql語句如下:
復(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班','張三','語文',60);
insert into @tab(Class,Student,Course,Quantity) values('A班','張三','數(shù)學(xué)',70);
insert into @tab(Class,Student,Course,Quantity) values('A班','張三','英語',80);
insert into @tab(Class,Student,Course,Quantity) values('A班','李四','語文',30);
insert into @tab(Class,Student,Course,Quantity) values('A班','李四','數(shù)學(xué)',40);
insert into @tab(Class,Student,Course,Quantity) values('A班','李四','英語',50);
insert into @tab(Class,Student,Course,Quantity) values('B班','王五','語文',65);
insert into @tab(Class,Student,Course,Quantity) values('B班','王五','數(shù)學(xué)',75);
insert into @tab(Class,Student,Course,Quantity) values('B班','王五','英語',85);
insert into @tab(Class,Student,Course,Quantity) values('B班','趙六','語文',35);
insert into @tab(Class,Student,Course,Quantity) values('B班','趙六','數(shù)學(xué)',45);
insert into @tab(Class,Student,Course,Quantity) values('B班','趙六','英語',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(語文) as 語文
,avg(數(shù)學(xué)) as 數(shù)學(xué)
,avg(英語) as 英語
,avg(總分) as 總分
from (
select Class,Student
,(select isnull(sum(Quantity),0) from @tab where Class=t.Class and Student=t.Student and Course='語文') as '語文'
,(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='英語') as '英語'
,(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,語文,數(shù)學(xué),英語,總分 with rollup
having Grouping(語文)=1
and Grouping(數(shù)學(xué))=1
and Grouping(英語)=1
您可能感興趣的文章:
- sql分組后二次匯總(處理表重復(fù)記錄查詢和刪除)的實現(xiàn)方法
- SQL SERVER 分組求和sql語句
- 顯示同一分組中的其他元素的sql語句
- sql獲取分組排序后數(shù)據(jù)的腳本
- SQL進(jìn)行排序、分組、統(tǒng)計的10個新技巧分享
- SQL分組排序去重復(fù)的小實例
- 以數(shù)據(jù)庫字段分組顯示數(shù)據(jù)的sql語句(詳細(xì)介紹)
- SQL中Group分組獲取Top N方法實現(xiàn)可首選row_number
- Sql Server:多行合并成一行,并做分組統(tǒng)計的兩個方法
- Sql Server 分組統(tǒng)計并合計總數(shù)及WITH ROLLUP應(yīng)用
- SQL語句分組獲取記錄的第一條數(shù)據(jù)的方法
- sqlserver巧用row_number和partition by分組取top數(shù)據(jù)
- sql 分組查詢問題
- SQLserver 實現(xiàn)分組統(tǒng)計查詢(按月、小時分組)
- 分組后分組合計以及總計SQL語句(稍微整理了一下)
相關(guān)文章
SQL Server 2014 數(shù)據(jù)庫中文版安裝圖文教程
這篇文章主要介紹了SQL Server 2014 數(shù)據(jù)庫中文版安裝圖文教程,需要的朋友可以參考下2021-05-05
一次SQL查詢優(yōu)化原理分析(900W+數(shù)據(jù)從17s到300ms)
本文主要介紹了一次SQL查詢優(yōu)化原理分析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
SQL恢復(fù)master數(shù)據(jù)庫方法 只有mdf文件的數(shù)據(jù)庫如何恢復(fù)
這篇文章主要為大家詳細(xì)介紹了SQL恢復(fù)master數(shù)據(jù)庫方法,還分析了只有mdf文件的數(shù)據(jù)庫如何進(jìn)行恢復(fù)的情況,也就是沒有l(wèi)og文件的數(shù)據(jù)庫文件恢復(fù)方法,感興趣的小伙伴們可以參考一下2016-05-05
數(shù)據(jù)庫Left join , Right Join, Inner Join 的相關(guān)內(nèi)容,非常實用
Left join , Right Join, Inner Join 的相關(guān)內(nèi)容,非常實用2009-07-07
Sql注入原理簡介_動力節(jié)點Java學(xué)院整理
所謂SQL注入,就是通過把SQL命令插入到Web表單遞交或輸入域名或頁面請求的查詢字符串,最終達(dá)到欺騙服務(wù)器執(zhí)行惡意的SQL命令。下面通過本文給大家分享sql 注入原理解析,感興趣的朋友一起看看吧2017-08-08

