假设:
2011-02-28 01:45
2011-02-28 01:46
2011-02-28 01:47
2011-02-28 01:48
2011-02-28 01:49
2011-02-28 01:50
2011-02-28 01:55
2011-02-28 02:01
2011-02-28 02:07
2011-02-28 02:12
2011-02-28 02:22
2011-02-28 02:21那么结果:
2011-02-28 01:45
2011-02-28 01:50
2011-02-28 02:22
2011-02-28 01:45
2011-02-28 01:46
2011-02-28 01:47
2011-02-28 01:48
2011-02-28 01:49
2011-02-28 01:50
2011-02-28 01:55
2011-02-28 02:01
2011-02-28 02:07
2011-02-28 02:12
2011-02-28 02:22
2011-02-28 02:21那么结果:
2011-02-28 01:45
2011-02-28 01:50
2011-02-28 02:22
(
select row_number() over(partition by
((row_number() over(order by getdate()))-1)/5 order by getdate) no ,* from tb
)select * from where no=1
(
select row_number() over(partition by no order by getdate()) as no1,* from #tb
)select * from cte where no1=1
drop table #tb
(
[date] datetime
)
insert #
select '2011-02-28 01:45' union all
select '2011-02-28 01:46' union all
select '2011-02-28 01:47' union all
select '2011-02-28 01:48' union all
select '2011-02-28 01:49' union all
select '2011-02-28 01:50' union all
select '2011-02-28 01:55' union all
select '2011-02-28 02:01' union all
select '2011-02-28 02:07' union all
select '2011-02-28 02:12' union all
select '2011-02-28 02:22' union all
select '2011-02-28 02:21'--#1. 隔5条数据取一条
select [date] from
(select rowno = ROW_NUMBER() over(order by getdate()), * from #) a
where a.rowno % 5 = 1--#2. 按时间隔5分钟(或>5分钟)取一条
select [date], falg = null into #temp from # order by [date] --插入临时表declare @StartDate datetime
select @StartDate = dateadd(minute, -6, MIN([date])) from #
select @StartDate --初始化开始时间update #temp --把时间间隔>=5分钟的记录筛选出来
set falg = case when [date] = @StartDate then 1 else 0 end,
@StartDate = case when [date] >= dateadd(minute, 5, @StartDate) then [date] else @StartDate endselect [date] from #temp where falg = 1 --显示结果
/*
2011-02-28 01:45:00.000
2011-02-28 01:50:00.000
2011-02-28 01:55:00.000
2011-02-28 02:01:00.000
2011-02-28 02:07:00.000
2011-02-28 02:12:00.000
2011-02-28 02:21:00.000
*/