【轉】SQL Server將一列拆分成多列


數據表中有一列數據,如圖1所示:

圖1數據表

現在需要將該列數據分成三列。

SQL 代碼如下所示:

1、

select 
max(case when F1%3=1 then F1 else 0 end) a,
max(case when F1%3=2 then F1 else 0 end) b,
max(case when F1%3=0 then F1 else 0 end) c
from HLR151
group by (F1-1)/3

效果:

2、

select 
c1=a.F1,c2=b.F1,c3=c.F1
from HLR151 a
left join HLR151 b on b.F1=a.F1+1 
left join HLR151 c on c.F1=a.F1+2
where (a.F1-1)%3=0

效果:

3、

select 
max(case when (F1-1)/8=0 then F1 else 0 end) a,
max(case when (F1-1)/8=1 then F1 else 0 end) b,
max(case when (F1-1)/8=2 then F1 else 0 end) c
from HLR151
group by (F1-1)%8

 

效果:

 

 轉自:https://www.cnblogs.com/shuai/archive/2011/02/16/1956123.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM