将一列数据拆成两列

yubofighting 2011-03-03 11:27:31
a
1
2
3
4


现在要将a列拆成b,c两列

b c
1 3

2

4
...全文
195 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jayjiang0910 2011-03-03
  • 打赏
  • 举报
回复
不明白LZ的意思
Xiao_Ai_Mei 2011-03-03
  • 打赏
  • 举报
回复
什么规律啊,??????????????????
AcHerat 元老 2011-03-03
  • 打赏
  • 举报
回复
拆分的依据是什么?
feixianxxx 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 josy 的回复:]

SQL code
select a.a as b,b.a as c
from
(select rn=row_number() over(order by getdate()),a from tb where id=0) a
full join
(select rn=row_number() over(order by getdate()),a from tb where id=1) b
on ……
[/Quote]
...树哥 很久不见你答题...
gw6328 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 josy 的回复:]
SQL code
select a.a as b,b.a as c
from
(select rn=row_number() over(order by getdate()),a from tb where id=0) a
full join
(select rn=row_number() over(order by getdate()),a from tb where id=1) b
……
[/Quote]
up.
楼主分的时候行的对应关系应该要注意一下。
AcHerat 元老 2011-03-03
  • 打赏
  • 举报
回复

create table tb(a int,id int)
insert into tb
select 1,0 union all
select 2,1 union all
select 3,0 union all
select 4,1 union all
select 5,0
go

select a.a,b.a
from (select a,id as b from tb where id = 0)a
full join
(select a,id as c from tb where id = 1)b
on a.a + 1 = b.a

drop table tb

/*
a a
----------- -----------
1 2
3 4
5 NULL

(3 行受影响)

--小F-- 2011-03-03
  • 打赏
  • 举报
回复
select
max(case id when 1 then a else '' end ) as b,
max(case id when 0 then a else '' end ) as c
from
tb
百年树人 2011-03-03
  • 打赏
  • 举报
回复
select a.a as b,b.a as c
from
(select rn=row_number() over(order by getdate()),a from tb where id=0) a
full join
(select rn=row_number() over(order by getdate()),a from tb where id=1) b
on a.rn=b.rn
syhxj 2011-03-03
  • 打赏
  • 举报
回复
case when就可以实现
yubofighting 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 acherat 的回复:]
楼主你的问题没有描述清楚!你要说明b列和c列是怎么取值的!
[/Quote]

有一个标志列,与a列对应

比如:
a id
1 0
2 1
3 0
4 1

同一个表中将id为1的变为b列,将id为0的变为c列

如果两列行数不一样,少的那列用null补足
AcHerat 元老 2011-03-03
  • 打赏
  • 举报
回复
楼主你的问题没有描述清楚!你要说明b列和c列是怎么取值的!
yubofighting 2011-03-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 acherat 的回复:]
拆分的依据是什么?
[/Quote]

有id标志的

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧