27,579
社区成员
发帖
与我相关
我的任务
分享
select * from tb t where not exists(select 1 from tb where col=t.col and 时间<t.时间)
select * from tb a where
not exists(select 1 from tb where col=a.col and 时间<a.时间)
select
*
from
tb t
where
not exists(select 1 from tb where col=t.col and 时间<t.时间)
declare @tb table (name nvarchar(10),时间 datetime)
insert into @tb select 'a','2009-5-21'
union all select 'a','2009-5-22'
union all select 'a','2009-5-23'
union all select 'b','2009-5-24'
union all select 'c','2009-5-25'
union all select 'c','2009-5-26'
--select *,ID=ROW_NUMBER()over( partition by name order by 时间 desc) from @tb order by 时间 desc
select top 2 name,时间 from(
select *,ID=ROW_NUMBER()over( partition by name order by 时间) from @tb) a where ID=1
union
select top 2 name,时间 from (
select top 100 *,ID=ROW_NUMBER()over( partition by name order by 时间 desc) from @tb order by 时间 desc) b where ID=1
name 时间
---------- -----------------------
a 2009-05-21 00:00:00.000
b 2009-05-24 00:00:00.000
c 2009-05-26 00:00:00.000
(3 行受影响)