校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁(yè) > 知識(shí)庫(kù) > 一句Sql把縱向表轉(zhuǎn)為橫向表,并分別分組求平均和總平均值

一句Sql把縱向表轉(zhuǎn)為橫向表,并分別分組求平均和總平均值

熱門標(biāo)簽:臨沂智能電銷機(jī)器人加盟哪家好 一個(gè)導(dǎo)航軟件能用幾個(gè)地圖標(biāo)注點(diǎn) 申請(qǐng)400電話在哪辦理流程 外呼運(yùn)營(yíng)商線路收費(fèi) 貴州房產(chǎn)智能外呼系統(tǒng)供應(yīng)商 鎮(zhèn)江網(wǎng)路外呼系統(tǒng)供應(yīng)商 百度地圖標(biāo)注改顏色 小e電話機(jī)器人 電銷外呼有錄音系統(tǒng)有哪些
效果如圖所示:

測(cè)試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ù)記錄查詢和刪除)的實(shí)現(xiàn)方法
  • SQL SERVER 分組求和sql語句
  • 顯示同一分組中的其他元素的sql語句
  • sql獲取分組排序后數(shù)據(jù)的腳本
  • SQL進(jìn)行排序、分組、統(tǒng)計(jì)的10個(gè)新技巧分享
  • SQL分組排序去重復(fù)的小實(shí)例
  • 以數(shù)據(jù)庫(kù)字段分組顯示數(shù)據(jù)的sql語句(詳細(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語句分組獲取記錄的第一條數(shù)據(jù)的方法
  • sqlserver巧用row_number和partition by分組取top數(shù)據(jù)
  • sql 分組查詢問題
  • SQLserver 實(shí)現(xiàn)分組統(tǒng)計(jì)查詢(按月、小時(shí)分組)
  • 分組后分組合計(jì)以及總計(jì)SQL語句(稍微整理了一下)

標(biāo)簽:澳門 晉城 三明 嘉興 延邊 保定 日照 合肥

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《一句Sql把縱向表轉(zhuǎn)為橫向表,并分別分組求平均和總平均值》,本文關(guān)鍵詞  一句,Sql,把,縱向,表,轉(zhuǎn)為,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《一句Sql把縱向表轉(zhuǎn)為橫向表,并分別分組求平均和總平均值》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于一句Sql把縱向表轉(zhuǎn)為橫向表,并分別分組求平均和總平均值的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 兴安盟| 银川市| 独山县| 昌图县| 榆社县| 连江县| 临西县| 云霄县| 黄梅县| 温州市| 桐乡市| 武乡县| 台江县| 花垣县| 阜康市| 金川县| 苍南县| 石城县| 山东| 沐川县| 九江市| 阿合奇县| 离岛区| 马山县| 华坪县| 凌源市| 安庆市| 崇明县| 乌什县| 邹平县| 宜丰县| 榆中县| 扎赉特旗| 广平县| 固镇县| 延长县| 安平县| 宁南县| 诸暨市| 宁德市| 萨嘎县|