表格如下:
time 
9:05
9:10
9:11
9:15
...找出和上面记录间隔<2分钟的记录9:11

解决方案 »

  1.   

    select * from tbl where datediff(mi,'9:11:00',dt)<2
      

  2.   

    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 行)
      

  3.   

    oh 搞错了!
    sorry
    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,t,a.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:16:00.000
    1900-01-01 09:26:00.000(所影响的行数为 2 行)