数据表中有一列数据,如图1所示:

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

图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

效果:

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

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

效果:

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

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

 

效果:

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

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

 

相关文章:

  • 2022-12-23
  • 2021-11-24
  • 2021-08-24
  • 2022-12-23
  • 2021-10-26
  • 2021-07-26
  • 2022-02-09
猜你喜欢
  • 2022-02-07
  • 2021-09-21
  • 2022-02-14
  • 2022-12-23
  • 2021-09-22
  • 2022-12-23
  • 2022-02-05
相关资源
相似解决方案