激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數(shù)據(jù)庫技術(shù)|

服務(wù)器之家 - 數(shù)據(jù)庫 - Sql Server - table 行轉(zhuǎn)列的sql詳解

table 行轉(zhuǎn)列的sql詳解

2019-11-08 15:31mssql教程網(wǎng) Sql Server

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

一、要求

1 創(chuàng)建數(shù)據(jù)表

CREATE TABLE [dbo].[StuScore](

[stuid] [int] NOT NULL,

[subject] [nvarchar](30) NULL,

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

)

2 插入測試數(shù)據(jù)

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 行轉(zhuǎn)列后的結(jié)果

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 行轉(zhuǎn)列,一個重點就是怎么樣知道有多少列,怎么樣創(chuàng)建這些列?我們可以先把這個問題擱置,而假設(shè)這些列是已知的。 例如示例數(shù)據(jù)中,可以先假設(shè)subject的數(shù)據(jù)[chinese,math]是已知的,這樣問題就簡化了許多

2 當(dāng)已知了chinese,math后,我們至少要先得到轉(zhuǎn)換后的tabel結(jié)構(gòu)

如下;

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

結(jié)果如下

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 接著就需要往這個數(shù)據(jù)集中去填充chinese, math的數(shù)據(jù)

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

結(jié)果如下:

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 細心的讀者會發(fā)現(xiàn)步驟3中的結(jié)果與我們想要的已經(jīng)非常接近了,只需再做一個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

得到的正是我們想要的結(jié)果

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

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

5 要獲取subject的數(shù)據(jù)其實很簡單

select distinct subject from dbo.StuScore

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

可以根據(jù)subject的值去動態(tài)的組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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 一级黄色影片在线观看 | 草久视频在线观看 | 中国美女一级黄色大片 | 欧美国产日韩在线观看成人 | 欧美精品一区二区三区在线 | 欧美视频在线一区二区三区 | 欧美久久一区 | 国产一区日韩精品 | 久久久电影电视剧免费看 | 原来神马影院手机版免费 | 亚洲国产精品久久久久久久久久久 | 在线观看免费视频麻豆 | 久久影院免费观看 | 一区二区免费看 | 激情小说激情电影 | 四季久久免费一区二区三区四区 | 55夜色66夜色国产精品视频 | 国产乱淫a∨片免费视频 | 久久精品免费网站 | 久久亚洲春色中文字幕久久 | 国产亚洲精品久久久久久大师 | 欧美性精品videofree | 精品亚洲午夜久久久久91 | 国产精品wwww| 一级成人欧美一区在线观看 | 免费专区 - 91爱爱 | 欧美视频国产精品 | fc2成人免费人成在线观看播放 | 国产免费一区二区三区最新不卡 | 噜噜噜在线 | 成人免费毛片在线观看 | 欧美在线 | 亚洲 | 久久精品视频亚洲 | 午夜影院日韩 | 精品国产一区二区三区四区在线 | 久久视频在线看 | 成片免费观看大全 | 国产毛片视频 | 免费国产羞羞网站视频 | 国产一级片91 | 成人电影毛片 |