62,267
社区成员
发帖
与我相关
我的任务
分享--Code
select cardid,
recordA=max(case px when 1 then record else '' end),
recordB=max(case px when 2 then record else '' end)
from (select *,px=(select count(1)+1 from @t where cardid=a.cardid and record<a.record)
from @t a) t
group by cardid
--Drop
--Result
/*
cardid recordA recordB
------ ---------- ----------
001 7:30 7:35
002 7:31 7:34
003 7:32 7:33
*/
declare @datekey table ([key] int)
insert into @datekey
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 union all
select 9 union all
select 10 union all
select 11
select * from @datekey
/*
key
-----------
1
2
3
4
5
6
7
8
9
10
11
*/
declare @table1 table(id int,[key] int)
declare @table2 table(id int,[key] int)
insert into @table1
select row_number() over (order by [key] ) as row ,[key] as id from @datekey where [key]%2<>0
insert into @table2
select row_number() over (order by [key] ) as row ,[key] as id from @datekey where [key]%2=0
select a.[key] as 单数, b.[key] as 双数 from @table1 a full outer join @table2 b on a.id=b.id
/*
单数 双数
----------- -----------
1 2
3 4
5 6
7 8
9 10
11 NULL
*/