建表语句: create table USERLOGINLOG ( USERID VARCHAR2(40) not null, USERIP VARCHAR2(20), LOGINTIME TIMESTAMP(6) not null, LOGOUTTIME TIMESTAMP(6) )统计每次在线时间:select ul.logouttime-ul.logintime as onlinetime from userloginlog ul 这条语句是OK的。 统计总的在线时间:select sum(ul.logouttime-ul.logintime) from userloginlog ul 这条语句会报错。
确实不能直接用。 用下句吧,统计总的天数: SQL> select sum(extract(day from logouttime-logintime)) alldays from USERLOGINLOG; ALLDAYS ----------
select sum(extract(day from logouttime-logintime)) alldays, sum(extract(hour from logouttime-logintime)) allhours, sum(extract(minute from logouttime-logintime)) allminutes, sum(extract(second from logouttime-logintime)) allseconds from USERLOGINLOG这样返回的值只是把天数、小时数、分钟、秒数做了一个自然加和,如果直接用成“xx小时xx分钟xx秒”这样的数据还需要做进一步处理。
要求精度不是到毫秒的话 可以这样select USERID,cast(sum((to_date(to_char(LOGOUTTIME,'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24:mi:ss') -to_date(to_char(LOGINTIME,'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24:mi:ss'))*24) as number(18,2)) 在线小时总计 from USERLOGINLOG group by USERIDSQL> /USERID 在线小时总计 ---------------------------------------- ------------ wkc 72.33 wkc168 2496.33 select USERID,sum((to_date(to_char(LOGOUTTIME,'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24:mi:ss') -to_date(to_char(LOGINTIME,'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24:mi:ss'))*24*60) 在线分钟总计 from USERLOGINLOG group by USERIDSQL> /USERID 在线分钟总计 ---------------------------------------- ------------ wkc 4340 wkc168 149780
create table USERLOGINLOG
(
USERID VARCHAR2(40) not null,
USERIP VARCHAR2(20),
LOGINTIME TIMESTAMP(6) not null,
LOGOUTTIME TIMESTAMP(6)
)统计每次在线时间:select ul.logouttime-ul.logintime as onlinetime from userloginlog ul
这条语句是OK的。
统计总的在线时间:select sum(ul.logouttime-ul.logintime) from userloginlog ul
这条语句会报错。
用下句吧,统计总的天数:
SQL> select sum(extract(day from logouttime-logintime)) alldays from USERLOGINLOG; ALLDAYS
----------
sum(extract(day from logouttime-logintime)) alldays,
sum(extract(hour from logouttime-logintime)) allhours,
sum(extract(minute from logouttime-logintime)) allminutes,
sum(extract(second from logouttime-logintime)) allseconds
from USERLOGINLOG这样返回的值只是把天数、小时数、分钟、秒数做了一个自然加和,如果直接用成“xx小时xx分钟xx秒”这样的数据还需要做进一步处理。
要求精度不是到毫秒的话
可以这样select USERID,cast(sum((to_date(to_char(LOGOUTTIME,'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24:mi:ss')
-to_date(to_char(LOGINTIME,'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24:mi:ss'))*24) as number(18,2)) 在线小时总计
from USERLOGINLOG
group by USERIDSQL> /USERID 在线小时总计
---------------------------------------- ------------
wkc 72.33
wkc168 2496.33
select USERID,sum((to_date(to_char(LOGOUTTIME,'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24:mi:ss')
-to_date(to_char(LOGINTIME,'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24:mi:ss'))*24*60) 在线分钟总计
from USERLOGINLOG
group by USERIDSQL> /USERID 在线分钟总计
---------------------------------------- ------------
wkc 4340
wkc168 149780