这篇文章主要讲解了SQL中如何将一列拆分成多列,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

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

现在需要将该列数据分成三列。

SQL 代码如下所示:

第一种

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) cfrom HLR151group by (F1-1)/3

效果

第二种

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

效果

第三种

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) cfrom HLR151group by (F1-1)%8

效果

看完上述内容,是不是对SQL中如何将一列拆分成多列有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。