日期:2014-05-17 浏览次数:20376 次
if OBJECT_ID('tb') is not null
drop table tb
go
create table tb
(
stid int,
nexttm datetime
)
insert into tb
select 1015,'2014-01-12 20:33:00.000' union all
select 1015,'2014-01-12 20:33:00.000' union all
select 1001,'2013-10-10 13:47:00.000' union all
select 1001,'2013-10-10 13:47:00.000' union all
select 1015,'2013-10-19 20:33:00.000' union all
select 1015,'2013-11-12 20:33:00.000' union all
select 1015,'2014-01-12 20:33:00.000'
select stid,
nexttm as rtdid
from
(
select *,
ROW_NUMBER() over(partition by stid
order by nexttm asc) as rownum
from tb
)t
where rownum = 1
/*
stid rtdid
1001 2013-10-10 13:47:00.000
1015 2013-10-19 20:33:00.000
*/