Reputation: 44104
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.
Upvotes: 1
Views: 1374
Reputation: 44104
I couldn't manage to get Nikola's query to work, but using some of the techniques from it to create this query, which can get the average per hour for an entire period, which is good enough.
SELECT hour, AVG(dayHourCount) FROM
(
SELECT hour, day, dayHour, COUNT(*) AS dayHourCount FROM
(
SELECT hour, day, day + INTERVAL hour HOUR AS dayHour FROM
(
select 0 AS hour
union
select 1 AS hour
union
select 2 AS hour
union
select 3 AS hour
union
select 4 AS hour
union
select 5 AS hour
union
select 6 AS hour
union
select 7 AS hour
union
select 8 AS hour
union
select 9 AS hour
union
select 10 AS hour
union
select 11 AS hour
union
select 12 AS hour
union
select 13 AS hour
union
select 14 AS hour
union
select 15 AS hour
union
select 16 AS hour
union
select 17 AS hour
union
select 18 AS hour
union
select 19 AS hour
union
select 20 AS hour
union
select 21 AS hour
union
select 22 AS hour
union
select 23 AS hour
) AS hours
INNER JOIN (SELECT DISTINCT DATE(start) AS day FROM PlayerSession ds WHERE ds.start > NOW() - INTERVAL 1 MONTH) AS days
) AS dayHours
LEFT JOIN PlayerSession s ON (s.start < dayHour AND s.lastActivity > dayHour)
LEFT JOIN Player p ON (s.player_id = p.id)
GROUP BY dayHour
) AS perDayHour
GROUP BY hour
Upvotes: 0
Reputation: 19356
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.
select days.date,
hour,
count (s.startTime) Users
from
(
(
select 0 hour
union
select 1 hour
union
select 2 hour
union
select 3 hour
union
select 4 hour
union
select 5 hour
union
select 6 hour
union
select 7 hour
union
select 8 hour
union
select 9 hour
union
select 10 hour
union
select 11 hour
union
select 12 hour
union
select 13 hour
union
select 14 hour
union
select 15 hour
union
select 16 hour
union
select 17 hour
union
select 18 hour
union
select 19 hour
union
select 20 hour
union
select 21 hour
union
select 22 hour
union
select 23 hour
) hours
cross join
(
-- We need date portion only
select distinct date(startTime) date from s
union select distinct date(endTime) from s
) days
)
left join s
-- date+hour, hopefully
on date_add(date, interval hour HOUR)
-- startTime is truncated to hour, hopefully
between date_sub(s.startTime interval minutes(s.startTime) MINUTE)
and s.endTime
-- last two months
where days.date between date_sub (now() interval 2 MONTH) and now()
group by days.date, hour
order by 1, 2
Upvotes: 1