日期:2014-05-18  浏览次数:20523 次

找出时间间隔小的记录。
表格如下:
time  
9:05
9:10
9:11
9:15
...

找出和上面记录间隔 <2分钟的记录

9:11

------解决方案--------------------
declare @t table (t datetime )

insert into @t
select '9:05 ' union all
select '9:11 ' union all
select '9:15 ' union all
select '9:16 ' union all
select '9:25 ' union all
select '9:26 '

select * from @t

select t
from @t a
where exists (select 1 from @t where a.t <t and datediff(mi,a.t,t) <=2)


t
------------------------------------------------------
1900-01-01 09:05:00.000
1900-01-01 09:11:00.000
1900-01-01 09:15:00.000
1900-01-01 09:16:00.000
1900-01-01 09:25:00.000
1900-01-01 09:26:00.000

(所影响的行数为 6 行)

t
------------------------------------------------------
1900-01-01 09:15:00.000
1900-01-01 09:25:00.000

(所影响的行数为 2 行)