22,209
社区成员
发帖
与我相关
我的任务
分享
declare @score table (score int)
insert into @score select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
select cast(a.score as nvarchar(10))+','+cast(b.score as nvarchar(10)) from @score a join @score b on a.score=b.score-1
where a.score%2!=0
(8 行受影响)
---------------------
1,2
3,4
5,6
7,8
(4 行受影响)
DECLARE @tb TABLE(score int)
insert @tb
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6 UNION ALL
SELECT 7 UNION ALL
SELECT 8
select identity(int,1,1) as id,score AS '1' ,null as '2' into #k1 from @tb where Score%2=0 and Score<>2
select identity(int,1,1) as id,null as '1',score as '2' into #k2 from @tb where Score%2=1 and Score<>1
go
select max([2]) as '1' ,MAX([1]) as '2' from (select * from #k1 union all select * from #k2 ) t group by id
/*
3 4
5 6
7 8
*/
DECLARE @tb TABLE(Score int)
insert @tb
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6 UNION ALL
SELECT 8
;with cte
as
(
select num=row_number() over(order by Score),Score
from @tb
)
select a.Score Score1,b.Score Score2
from cte a join cte b on a.num+1=b.num
where a.num%2=1 and b.num%2=0
Score1 Score2
----------- -----------
1 2
3 3
4 5
6 8
(4 行受影响)
按lz给的数据的结果DECLARE @tb TABLE(Score int)
insert @tb
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6 UNION ALL
SELECT 7 UNION ALL
SELECT 8
;with cte
as
(
select num=row_number() over(order by Score),Score
from @tb
)
select a.Score Score1,b.Score Score2
from cte a join cte b on a.num+1=b.num
where a.num%2=1 and b.num%2=0
Score1 Score2
----------- -----------
1 2
3 4
5 6
7 8
(4 行受影响)