用户登录
用户注册

分享至

SQL 将一列拆分成多列的三种方法

  • 作者: 比巴卜泡泡糖47114701
  • 来源: 51数据库
  • 2021-08-11

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

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

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) c
from hlr151
group by (f1-1)/3

效果

第二种

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

效果

第三种

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 将一列拆分成多列的三种方法的详细内容,更多关于sql 一列拆分成多列的资料请关注其它相关文章!

软件
前端设计
程序设计
Java相关