我现在的数据是这样的,userid表示用户登录系统的代码,time表示登录登出系统的时间,status,1表示登录,2表示登出;
userid   time                     status
user001  2011-06-10 14:57:24.000   1
user001  2011-06-10 15:03:41.000   2
user001  2011-06-10 16:50:00.000   1
user001  2011-06-10 17:03:40.000   2
user001  2011-06-10 17:20:40.000   1
user002  2011-06-10 10:01:20.000   1
user002  2011-06-10 10:20:24.000   1
user002  2011-06-10 10:40:40.000   2
user002  2011-06-10 13:50:00.000   1
user002  2011-06-10 15:03:40.000   2 
我想要的结果是:
用户代码   登录时间区间                                     
user001  2011-06-10 14:57:24.000--2011-06-10 15:03:41.000   6  minutes  
user001  2011-06-10 16:50:00.000--2011-06-10 17:03:40.000   13 minutes
user001  2011-06-10 17:20:40.000                            0 minutes
user002  2011-06-10 10:01:20.000--2011-06-10 10:40:40.000   39 minutes   
user002  2011-06-10 10:20:24.000--                          0 minutes
user002  2011-06-10 13:50:00.000--2011-06-10 15:03:40.000   73minutes      我的目的是统计每个用户登录系统的时间的汇总。

解决方案 »

  1.   

    select a.userid,a.time,b.time,datediff(mi,,a.time,b.time)
    from tb a left join ,tb b 
    on a.userid=b.userid and b.status=2
    and b.time=(select top 1 time from tb c 
                where c.userid=a.userid and c.status=2
                and c.time>a.time order by c.time)
    where a.status=1 
      

  2.   


    declare @t table(iId int identity(1,1),[userid] varchar(20), [time] datetime, [status] int)
    Insert @t([userid], [time], [status])
    select 'user001', '2011-06-10 14:57:24.000', 1 Union All
    select 'user001', '2011-06-10 15:03:41.000', 2 Union All
    select 'user001', '2011-06-10 16:50:00.000', 1 Union All
    select 'user001', '2011-06-10 17:03:40.000', 2 Union All
    select 'user001', '2011-06-10 17:20:40.000', 1 Union All
    select 'user002', '2011-06-10 10:01:20.000', 1 Union All
    select 'user002', '2011-06-10 10:20:24.000', 1 Union All
    select 'user002', '2011-06-10 10:40:40.000', 2 Union All
    select 'user002', '2011-06-10 13:50:00.000', 1 Union All
    select 'user002', '2011-06-10 15:03:40.000', 2  
    select *, ISNULL(DATEDIFF(MINUTE, [time], EndDate), 0) iMINUTE from (
    select *, (select top 1 [time] from @t where A.userid=userid and [status] = 2 and A.iId<=iId order by iId) EndDate from @t A where [status] = 1
    ) A
    iId         userid               time                    status      EndDate                 iMINUTE
    ----------- -------------------- ----------------------- ----------- ----------------------- -----------
    1           user001              2011-06-10 14:57:24.000 1           2011-06-10 15:03:41.000 6
    3           user001              2011-06-10 16:50:00.000 1           2011-06-10 17:03:40.000 13
    5           user001              2011-06-10 17:20:40.000 1           NULL                    0
    6           user002              2011-06-10 10:01:20.000 1           2011-06-10 10:40:40.000 39
    7           user002              2011-06-10 10:20:24.000 1           2011-06-10 10:40:40.000 20
    9           user002              2011-06-10 13:50:00.000 1           2011-06-10 15:03:40.000 73(6 行受影响)
    这个有那么点漏洞,就是有进无出的不能判断出来
      

  3.   

    select
     a.userid,a.time,b.time,isnull(datediff(mi,,a.time,b.time),0)
    from
     tb a left join tb b 
    on
     a.userid=b.userid 
    and
     b.status=2
    and
     b.time=(select top 1 time from tb c where c.userid=a.userid and c.status=2 and c.time>a.time order by c.time)
    where
     a.status=1 
      

  4.   

    select a.userid,a.time,b.time,datediff(mi,,a.time,b.time)
    from tb a left join ,tb b 
    on a.userid=b.userid and b.status=2
    and b.time=(select top 1 time from tb c 
                where c.userid=a.userid and c.status=2
                and c.time>a.time order by c.time)
    where a.status=1