三、現在我們假設應用計時分為app應用和web應用,需要考慮如下幾個方面:
(1)多時間段(2)表中有冗雜數據
(3)用戶是在web端和app端都登陸,這種類型的重復時間段只能取其一
?
存在數據:
存在表:應用啟動時間統計表Applic_Sessions:
字段:applic ???platform ????start_time ????end_time ????status ????rowid
???????????? X ???????? web ??????????11:30 ?????????????18:33 ??????????1 ??????????1
???????? ? ? X ???????? app ?????????? 11:10 ? ?????????? 17:50 ??????????1 ??????????2
?????????????X ???????? app ???????? ? 17:55 ?????????????19:55 ??????????1 ??????????3
???????????? X ???????? app ?????????? 20:31 ?????????????22:31 ??????????1 ??????????4
???????????? X ???????? web ?????????? 02:01 ?????????????02:40 ??????????1 ??????????5
?
利用sql語句的方式獲取到開始時間和結束時間,timestampdiff( )相減得到。
(1)取出X用戶兩平臺中啟動的最小時間
(2)取出X用戶兩平臺中結束的最大時間
按照以上步驟,這樣會帶來一個問題:如rowid 為1和5的記錄,相減所得結果明顯大于實際結果。
?
嘗試方案1:將不同平臺的數據區分開單獨計數:
(1)select t.* from Applic_Sessions t where t.platform = “web”;
視圖:Web_View
applic ????platform ?????start_time???? end_time ???? status ????rowid
????X ????????web ??????????????11:30 ?????????????18:33 ??????????1 ??????????1
??? X ???????? web ??????????????02:01 ?????????????02:40 ??????????1 ??????????2
select sum(timestampdiff(second,start_time,end_time)) from Web_View;
?
(2)select t.* from Applic_Sessions t where t.platform = “app”;
視圖:App_View
applic ?????platform ????start_time ????end_time ????status ????rowid
????X ????????????app ??????????11:10 ?????????????17:50 ???????? ??1 ??????????1
????X ????????????app ??????????17:55 ?????????????19:55 ???????? ? 1 ??????????2
????X???????????? app ??????????20:31 ?????????????22:31 ???????? ? 1 ??????????3
select sum(timestampdiff(second,start_time,end_time)) from Web_View;
?
這樣求取出單個平臺的使用時長,這種適用于:app使用時長可以換取雙倍積分 等需求。但是至于中間的重復時間段更加干不掉了,這種方法pass。
方案2:
視圖:Applic_Sessions
applic platform ?start_time end_time status rowid
Xweb??????11:30?18:33??1??1
Xapp??????11:10?17:50??1??2
Xapp??????17:55?19:55??1??3
X app ??????20:31 ?22:31 ??1 ??4
X web ??????02:01 ?02:40 ??1 ??5
?
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
????????????????????????????????????????????????????11:30............................................18:33(web)
????????????????????????????????????11:10....................17:50(app)
????????????????????????????????????????????????????????????????????????????????????????17:55.................19:55(app)
????????????02:01....02:40 ?????????????????????????????????????????????????????????????????????????????????????????????????? ?20:31.......22:31
?
整理一下數據,發現其中的難點就是:web的使用時長段與app的使用時間段夾雜在一起,其他正常數據橫向合并就成,那第一步就是把這些夾雜數據單獨拎出來,然后取出最小啟動值和最大結束值,相減就好拉:
寫下這個sql,越看越別扭,能行么---顯然不行
select t.* from Applic_Sessions t where
????????t.start_time between (select min(t.start_time) from Applic_Sessions t) ????
??????? and (select max(t.end_time) from Applic_Sessions t)
????????????????and t.end_time between t.start_time and t.end_time;
至此,卡住了。。。
?
絞盡了腦汁,搞不定啊,有哪位大神可以幫幫忙啊,謝謝了
?