一个表里记录着用户登进、登出的时间,现在要统计出每个用户在线的总时间。userID   类型         时间点
  1      登进   20090911 10:00:00
  2      登进   20090911 10:01:00
  1      登出   20090911 10:50:00
  2      登出   20090911 10:55:00
  1      登进   20090912 11:00:00
  1      登出   20090912 11:10:00
统计所有用户的用户在线的时间。根据上面的数据得到的结果应该是:
userID  时间
  1     60分钟
  2     54分钟

解决方案 »

  1.   

    sql不可能,用游标应该可以统计出来
      

  2.   

    select t1.userID, sum((t2.sdate - t1.sdate) * 24 * 60)
    from 
    (select userID, sdate,row_number(partition by userID order by sdate) rn from t where stype = '登进') t1,
    (select userID, sdate,row_number(partition by userID order by sdate) rn from t where stype = '登出') t2
    where t1.userID = t2.userID
    and  t1.rn = t2.rn
    group by ti.userID;
      

  3.   

    select userid,sum(nvl(ld,sysdate)-时间点) 时间 from( 
      select userid,类型,时间点,lead(时间点)over(partition by userid order by 时间点)ld
      from table1)
    where 类型='登进'
    group by userid
      

  4.   

    select userid,sum(nvl(ld,sysdate)-时间点)*24*60||'分钟' 时间 from( 
      select userid,类型,时间点,lead(时间点)over(partition by userid order by 时间点)ld 
      from table1) 
    where 类型='登进' 
    group by userid
      

  5.   

    select userid,round(sum(nvl(ld,sysdate)-时间点)*24*60)||'分钟' 时间 from( 
      select userid,类型,时间点,lead(时间点)over(partition by userid order by 时间点)ld 
      from table1) 
    where 类型='登进' 
    group by useridok
      

  6.   

    ---用游标可以写出来,用SQL语句也可以,
    ---但是数据量大的话,其SQL语句还不如游标的效率高!