22,199
社区成员
发帖
与我相关
我的任务
分享
select stid,
UTID,
UTNM
nexttm as rtdid
from
(
select *,
ROW_NUMBER() over(partition by stid
order by nexttm asc) as rownum
from tb
)t
where rownum = 1
select stid,min(rtdid) as rtdid
from tb
group by stid
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
*/