一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數據庫技術|

服務器之家 - 數據庫 - Sql Server - table 行轉列的sql詳解

table 行轉列的sql詳解

2019-11-08 15:31mssql教程網 Sql Server

tabele行轉列的資料,網上搜一下很多。大家照著網上copy就可以實現自己想要的功能。但是大家在實現功能后是否想過行轉列為什么要這樣寫?下面就以一個實例來分析, 希望對初學者有所幫助。

一、要求

1 創建數據表

CREATE TABLE [dbo].[StuScore](

[stuid] [int] NOT NULL,

[subject] [nvarchar](30) NULL,

[score] [decimal](5, 1) NULL

)

2 插入測試數據

stuid subject score

3 chinese 76.0

3 math 73.0

4 chinese 82.0

5 chinese 66.0

5 math 93.0

6 chinese 67.0

7 math 83.0

8 chinese 77.0

8 math 84.0

3 行轉列后的結果

stuid chinese math

3 76.0 73.0

4 82.0 0.0

5 66.0 93.0

6 67.0 0.0

7 0.0 83.0

8 77.0 84.0

二 、分析

1 行轉列,一個重點就是怎么樣知道有多少列,怎么樣創建這些列?我們可以先把這個問題擱置,而假設這些列是已知的。 例如示例數據中,可以先假設subject的數據[chinese,math]是已知的,這樣問題就簡化了許多

2 當已知了chinese,math后,我們至少要先得到轉換后的tabel結構

如下;

select stuid, 0 as chinese, 0 as math from dbo.StuScore

結果如下

stuid chinese math

3 0 0

3 0 0

4 0 0

5 0 0

5 0 0

6 0 0

7 0 0

8 0 0

8 0 0

3 接著就需要往這個數據集中去填充chinese, math的數據

select stuid,

case subject when 'chinese' then score else 0 end as chinese,

case subject when 'math' then score else 0 end as math

from dbo.StuScore

結果如下:

stuid chinese math

3 76.0 0.0

3 0.0 73.0

4 82.0 0.0

5 66.0 0.0

5 0.0 93.0

6 67.0 0.0

7 0.0 83.0

8 77.0 0.0

8 0.0 84.0

4 細心的讀者會發現步驟3中的結果與我們想要的已經非常接近了,只需再做一個sum()處理,就OK了

select stuid,

sum(case subject when 'chinese' then score else 0 end ) as chinese,

sum(case subject when 'math' then score else 0 end ) as math

from dbo.StuScore group by stuid

得到的正是我們想要的結果

stuid chinese math

3 76.0 73.0

4 82.0 0.0

5 66.0 93.0

6 67.0 0.0

7 0.0 83.0

8 77.0 84.0

是不是現在就已經完成了呢?答案是否定的。前面我們已經說過,是為了簡化問題,在假設已經知道了subject數據的情況下,這么處理的,實際上subject的數據是可變的,未知的,接下來就是要解決這個問題了

5 要獲取subject的數據其實很簡單

select distinct subject from dbo.StuScore

獲取以后怎樣得到case subject when 'chinese' then score else 0 end 這種語句?

可以根據subject的值去動態的組sql語句

看下面的一段代碼

declare @sql varchar(2000)

set @sql=''

select @sql =@sql+ ',case subject when '''+subject+''' then 1 else 0 end as ' + subject

from (select distinct subject from dbo.StuScore) as sub

print @sql

message打印的信息如下:

,case subject when 'chinese' then 1 else 0 end as chinese,case subject when 'math' then 1 else 0 end as math

6 最后我們就需要將前面步驟綜合起來,得到最終的sql

declare @sql varchar(2000)

set @sql='select stuid'

select @sql =@sql+ ',sum(case subject when '''+subject+''' then score else 0 end) as ' + subject

from (select distinct subject from dbo.StuScore) as sub

set @sql=@sql + ' from dbo.StuScore group by stuid'

exec(@sql)

stuid chinese math

3 76.0 73.0

4 82.0 0.0

5 66.0 93.0

6 67.0 0.0

7 0.0 83.0

8 77.0 84.0

至此,整個分析過程和結果就都出來了。

初試寫文章, 多包涵,指正。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 校园刺激全黄H全肉细节文 校草让我脱了内裤给全班看 | 91人成尤物在线 | 国产精品四虎在线观看免费 | 大又大又黄又爽免费毛片 | 亚洲 欧美 国产 综合 播放 | 久久免费看少妇高潮A片特爽 | 新版孕妇bbwbbwbbw | 亚洲AV蜜桃永久无码精品红樱桃 | 国产清纯91天堂在线观看 | xxxxx性中国hd | 国产成人精品在线 | 免费观看a毛片一区二区不卡 | 欧美精品成人a多人在线观看 | 催眠白丝舞蹈老师小说 | 秋霞黄色片 | 日本中文字幕在线视频 | 亚洲午夜久久久久影院 | 国产精品51麻豆cm传媒 | 国产视频自拍一区 | 国产高清在线观看 | 日韩精品福利视频一区二区三区 | 日韩精品中文字幕视频一区 | 欧美亚洲一区二区三区 | porono日本动漫 | 国产精品福利一区二区亚瑟 | 门房秦大爷在线阅读 | 国产xxx在线| 国产码一区二区三区 | 国产成人一区二区三区在线视频 | 精品视频在线观看 | 男人jj视频 | 国产第一福利视频导航在线 | 91免费永久国产在线观看 | 紧身短裙女教师波多野 | 极品妖艳许清赵丽全文免费阅读 | 欧美理论片手机在线观看片免费 | 色戒完整版 | 日韩网站在线观看 | brazzers欧美教师 | 百合漫画咱啪全彩抚慰 | 情人梁家辉在线 |