表内容如下
-----------------------------
ID            LogTime
1            2008/10/10 10:00:00
1            2008/10/10 10:03:00
1            2008/10/10 10:09:00
2    2008/10/10 10:10:00
2    2008/10/10 10:11:00
......
-----------------------------请问各位高手,如何查询登陆时间间隔不超过5分钟的所有记录.  万分感谢.感谢感谢感谢感谢感谢...

解决方案 »

  1.   

    --按全部记录计算
    select m.id , m.logtime from
    (select t.* , px = (select count(*) from tb where logtime < t.logtime) + 1 from tb t) m,
    (select t.* , px = (select count(*) from tb where logtime < t.logtime) + 1 from tb t) n
    where m.px = n.px - 1 and datediff(mi , m.logtime,n.logtime) <= 5--分ID计算
    select m.id , m.logtime from
    (select t.* , px = (select count(*) from tb where id = t.id and logtime < t.logtime) + 1 from tb t) m,
    (select t.* , px = (select count(*) from tb where id = t.id and logtime < t.logtime) + 1 from tb t) n
    where m.id = n.id and m.px = n.px - 1 and datediff(mi , m.logtime,n.logtime) <= 5
      

  2.   

    select *
    from tb a
    where not exists(select 1 from tb where logtime>a.logtime and datediff(minute,a.logtime,logtime)<5)
      

  3.   

    --> 测试数据: @T
    declare @T table (ID int,LogTime datetime)
    insert into @T
    select 1,'2008/10/10 10:00:00' union all
    select 1,'2008/10/10 10:03:00' union all
    select 1,'2008/10/10 10:09:00' union all
    select 2,'2008/10/10 10:10:00' union all
    select 2,'2008/10/10 10:11:00'-->取小的
    select * from @T a where exists (select 1 from @T b where ID = a.ID and LogTime>a.LogTime and datediff(minute,a.LogTime,LogTime)<5)
    /*
    1 2008-10-10 10:00:00.000
    2 2008-10-10 10:10:00.000
    */-->取大的
    select * from @T a where exists (select 1 from @T b where ID = a.ID and LogTime<a.LogTime and datediff(minute,LogTime,a.LogTime)<5)
    /*
    1 2008-10-10 10:03:00.000
    2 2008-10-10 10:11:00.000
    */
      

  4.   

    这个我试了.好象不行啊.查到的数据如下:
    ---- 按全部记录id          logtime
    ----------- -----------------------
    1           2008-10-10 10:00:00.000
    1           2008-10-10 10:09:00.000
    2           2008-10-10 10:10:00.000
    2           2008-10-10 10:11:00.000
    1           2008-10-11 09:15:00.000
    2           2008-10-11 09:19:00.000(6 行受影响)---- 分ID计算
    id          logtime
    ----------- -----------------------
    1           2008-10-10 10:00:00.000
    2           2008-10-10 10:10:00.000
    1           2008-10-10 10:09:00.000
    2           2008-10-11 09:19:00.000(4 行受影响)
      

  5.   

    select * from tb a,tb b 
    where datediff(n,a.logtime,b.logtime)<5 and a.logtime<b.logtime and a.id=b.id
    第一次回帖哦