34,838
社区成员




DECLARE @ta TABLE (balance INT)
INSERT INTO @ta
SELECT 100 UNION ALL
SELECT -199 UNION ALL
SELECT 188 UNION ALL
SELECT -234
select t1.dibit,t2.crd from
(select row_number() over (order by getdate()) as r1 ,balance as dibit from @ta where balance >=0) t1
full join
(select row_number() over (order by getdate()) as r2 ,balance as crd from @ta where balance <0) t2
on t1.r1 = t2.r2
/*
dibit crd
----------- -----------
100 -199
188 -234
*/
;with t
as
(select balance as dibit,row_number() over (order by getdate()) from ta where balance > 0
union all
select balance as crd,row_number() over (order by getdate()) from ta where balance <= 0 )
select * from t;