码迷,mamicode.com
首页 > 数据库 > 详细

【转】SQL Server将一列拆分成多列

时间:2020-07-15 23:51:28      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:arch   ase   enter   idt   order   join   max   The   技术   

数据表中有一列数据,如图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

 

【转】SQL Server将一列拆分成多列

标签:arch   ase   enter   idt   order   join   max   The   技术   

原文地址:https://www.cnblogs.com/hycms/p/13307602.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!