T-SQL数据转换

时间:2011-07-18 15:27:49

标签: sql-server tsql

我有这样的结果集:

ID  Value
1    100
2    50
3    200
4    30
-    -
-    -

我希望它转变为以下内容:

Value1 Value2
100      50
200      30
-        -
-        -

如何使用T-SQL?

2 个答案:

答案 0 :(得分:2)

使用此:

select a.Value, b.Value
from
(
    select row_number() over(order by ID) [rn], Value
    from @t
)a
left join
(
    select row_number() over(order by ID) [rn], Value
    from @t
)b on b.rn = a.rn + 1
where a.rn % 2 = 1

示例数据:

declare @t table (ID int, Value int)

insert @t values (1,100), (2,50), (3,200), (4,30)

输出:

Value       Value
----------- -----------
100         50
200         30

答案 1 :(得分:2)

declare @t table (id int, v int)

insert @t values (1, 10)
insert @t values (2, 20)
insert @t values (3, 30)
insert @t values (4, 40)
insert @t values (5, 50)


select t1.v, t2.v
from @t t1 
left join @t t2 
on t1.id + 1 = t2.id
where t1.id %2 = 1