select * from 
(
select * from Table1
 where datediff(mi,Time,getdate())<50
)a where not exists(select * from
(select * from Table1
 where datediff(mi,Time,getdate())<50)b where a.STID=b.STID and datediff(mi,a.Time,b.Time)>10 )

解决方案 »

  1.   

    你这存在前后记录的逐条比较,一条SQL肯定搞不定了
      

  2.   


    SELECT a.* FROM 表 a join 表 b on datediff(mi,a.time,getdate())>50 and a.STID=b.STID and a.time<>b.time  group by a.stid,a.time
    having min(abs(datediff(mi,a.time,b.time)))>=10
      

  3.   

    --看错,可以一个SQL搞定:
    select * from table1 a 
    where   datediff(minute,Time,getdate())<50 and
    not exists (select 1 from table1 where abs(datediff(minute,a.Time,Time))<10)
      

  4.   

    select * from 
    (
    select * from Table1
     where datediff(mi,Time,getdate())<50
    )a where not exists(select * from
    (select * from Table1
     where datediff(mi,Time,getdate())<50)b where a.STID=b.STID and a.Time<b.Time and datediff(mi,a.Time,b.Time)<10 )