关于时间统计:
一、注意点:
(1)但是考虑到开启的应用并不是一直在运行,有可能是多个时间段。
(2)用户是在web端和app端都登陆,这种类型的时间段只能取其一。
(3)用户在web端和app端都登录,其中之一下线了
(4)考虑到表中有冗杂数据
二、现在我们只假设应用为app应用,那么只需要考虑到三个方面:
(1)多时间段(2)单次运行较长时间(3)表中有冗杂数据
1、利用sql语句的方式获取到开始时间和结束时间,timestampdiff( )相减得到。
存在表:应用启动时间统计表Applic_Sessions:
字段:applic start_timeend_timestatusrowid
X11:3014:33 1 1
X11:3014:33 1 1
Y11:3215:32 0 2
X20:3122:31 1 3
X02:0102:40 1 4
步骤:(1)获取到单次用户开启应用的最早时间start_time1:取出X、Y应用的非重启动时间
select distinct t.applic,t.start_time,rowid from Applic_Sessions t where not in (select x.applic,x.start_time from Applic_Sessions x where t.applic=x.applic and t.start_time>x.start_time and t.start_time<=x.end_time);//查询出X、Y应用的启动时间//现实中肯定没有这么一种情况:应用的启动时间大于实际启动却要比结束时间还小的,也就是说所取得时间段要包含当前时间段的所有时间
create view Start_Time as select distinct t.applic,t.start_time,rowid from Applic_Sessions t where not in (select x.applic,x.start_time from Applic_Sessions x where t.applic=x.applic and t.start_time>x.start_time and t.start_time<=x.end_time); //创建个视图Start_Time
(2)获取到单次用户结束应用的最小时间end_time1:取出X、Y应用的非重结束时间
select distinct t.applic,t.end_time,rowid from Applic_Sessions t where not in (select x.applic,x.end_time from Applic_Sessions x where t.applic=x.applic and t.end_time<x.end_time and t.end_time>=x.start_time) ;//查询出X、Y应用的结束时间//因为分了多个时间段,所以不能单纯的从end_time取某用户的最大结束时间
create view End_Time as select distinct t.applic,t.end_time,rowid from Applic_Sessions t where not in (select x.applic,x.end_time from Applic_Sessions x where t.applic=x.applic and t.end_time<x.end_time and t.end_time>=x.start_time) ;
(3)将两次时间段合并,获得单次启动时长current_time1
(4)统计当天内应用启动的时间段:由于是app应用,直接按行横向合并就可以了。
select applic,sec_to_time(sum(timestampdiff(second,start_time,end_time))) from current_timegroup byapplic;//求出X、Y应用的时间统计
备注:
(1)函数timestampdiff(second,start_time,end_time)以秒为单位,求出两列之差
(2)函数sum() 求和
(3)缺点:当存在大量数据时,sql执行时间过长