我有一个包含不同用户的登录和注销的表:
login event_date event
------------------------------------------
user1 2016-09-16 20:14:28.412830 LOGIN
user1 2016-09-16 20:34:38.937436 LOGOUT
user1 2016-09-16 20:36:34.363710 LOGIN
user1 2016-09-16 21:07:39.470257 LOGOUT
user1 2016-09-16 21:14:37.849903 LOGIN
...
user2 2016-09-16 00:05:02.937110 LOGOUT
user2 2016-09-16 15:00:36.478966 LOGIN
user2 2016-09-16 16:25:01.294783 LOGOUT
user2 2016-09-16 16:30:19.965582 LOGIN
user2 2016-09-16 20:35:04.926386 LOGOUT
...
user3 2016-09-16 11:05:12.818579 LOGIN
user3 2016-09-16 14:42:02.487888 LOGOUT
user3 2016-09-16 14:54:50.574649 LOGIN
user3 2016-09-16 16:00:01.106796 LOGOUT
user3 2016-09-16 17:09:31.366645 LOGIN
user3 2016-09-16 17:25:39.470333 LOGOUT
注意:此表中的数据自然按event_date
排序(或者更确切地说是id
);为方便起见,我们提供login
排序。
我需要计算每个用户每个时间间隔的在线时间(例如每30分钟,小时,天等)和每周的每一天。
我需要的输出是这样的:
login hour seconds_online
-------------------------------------------
user1 2016-09-16 20:00:00 1920
user1 2016-09-16 21:00:00 420
...
user2 2016-09-16 00:00:00 302
user2 2016-09-16 15:00:00 3564
user2 2016-09-16 16:00:00 1490
...
user3 2016-09-16 11:00:00 3285
user3 2016-09-16 12:00:00 3600 <- Please note how these two lines
user3 2016-09-16 13:00:00 3600 <- relate to the actual log above.
user3 2016-09-16 14:00:00 1078
...
或者:
login day_of_week seconds_online
-----------------------------------
user1 Monday 17280
user1 Tuesday 16540
...
user2 Monday 16990
user2 Tuesday 17014
...
请帮忙!