隔5分钟取一条数据,在5分钟内没有的就取下一条数据,表结构如下:2013-04-01 09:29:39.310
2013-04-01 09:31:41.890
2013-04-01 09:56:24.310
2013-04-01 10:13:25.903
2013-04-01 10:19:43.357
2013-04-01 10:21:25.827想要的结果: 日期时间
2013-04-01 09:29:39.310
2013-04-01 09:56:24.310
2013-04-01 10:13:25.903
2013-04-01 10:19:43.357
if OBJECT_ID('test') is not null
drop table test
go
create table test(dtime datetime)
insert into test
select '2013-04-01 09:29:39.310' union
select '2013-04-01 09:31:41.890' union
select '2013-04-01 09:56:24.310' union
select '2013-04-01 10:13:25.903' union
select '2013-04-01 10:19:43.357' union
select '2013-04-01 10:21:25.827'
declare @dtime datetime,@dtime2 datetime
declare mycursor cursor for select dtime from test order by dtime
open mycursor
fetch next from mycursor into @dtime
print @dtime
while @@FETCH_STATUS=0
begin
fetch next from mycursor into @dtime2
if @dtime2>DATEADD(MINUTE,5,@dtime)
begin
print @dtime2
set @dtime=@dtime2
end
else
begin
set @dtime=@dtime2
end
end
close mycursor
deallocate mycursor
/*(6 row(s) affected)
04 1 2013 9:29AM
04 1 2013 9:56AM
04 1 2013 10:13AM
04 1 2013 10:19AM
*/