I have a table containing user sessions, i.e. a period a user is logged in on my application. Each session has a start and end timestamp.
Can I, with one query, determine for every hour in the day how many users were online at that time (i.e. how many sessions had startTime <= hour AND endTime > hour)?
I’d like to show the averages of that for the last 2 months, but if that’s not possible I can manage with a query that does 1 day and calculate the averages in a script.
Sample result:
Hour Online
00:00 10
01:00 12
02:00 10
....
16:00 100
17:00 120
....
(hour may also be a simple integer, doesn’t matter much)
The database is MySQL 5.
I was waiting for somebody else to step in, as I am not proficient in mysql, and I believe there must be a better solution.
Main problem here is a way to construct a table of hours in a day. Having no recursive select ability in mysql forced me to create a table by means of union. Dates are easier if we can accept missing days when nobody logged in at that date. If not, similar trick as with hours can be used to extend dates, for example for seven days.
Cross join will produce a table of dates, each having all 24 hours of the day. Now we need to count sessions that were active at this point of time. To do that we need to truncate startTime to hour boundaries and place cross-join combined time inside truncated startTime and endTime (which does not need a truncation). Our data is finally here.
To get average for last two months simply wrap this select in another one grouping hour and calculating avg(Users). If you really must have single query to return both datasets, you might union this query with average query, where average query would return null for date.
Additional disclaimer: as a stated earlier I do not know MySql. I tried to write date and time conversion functions using online manual. Probably failed miserably, but I believe you will correct me. I’m also not sure about reserved keywords.