日期:2014-05-17 浏览次数:20708 次
create table tb(id int,dt datetime)
insert into tb select 1,'2011-09-05 00:00:01.000'
insert into tb select 2,'2011-09-05 00:00:41.000'
insert into tb select 3,'2011-09-05 00:00:51.000'
insert into tb select 4,'2011-09-05 00:01:09.000'
insert into tb select 5,'2011-09-05 00:01:39.000'
go
select * into # from tb where id=1
while exists(select 1 from tb where datediff(s,(select max(dt) from #),dt)>30)
insert into # select top 1 * from tb where datediff(s,(select max(dt) from #),dt)>30 order by id
select * from # order by id
/*
id dt
----------- -----------------------
1 2011-09-05 00:00:01.000
2 2011-09-05 00:00:41.000
5 2011-09-05 00:01:39.000
(3 行受影响)
*/
go
drop table tb,#