I am trying to work out the best way that I can find out in which times during the day had the most users in the chat room.
I tried grouping the dates, but it doesn't really work because of the intervals. Is there some sort of window function I can use in either MySQL? Could maybe moving this data into ClickHouse be a more effective way of getting this information?
There are ways that you can find out the total amount of overlapping time ranges - ie, the amount of collisions that happen between enter_time
and leave_time
(see: How to find maximum time range collision occurencies in Mysql) but this does not allow you to find the exact times when there were the most users in the chat room.
A picture describing the overlay of times for different users
What would be the most effective way to build a query that would show the times with the most users was
The outcome that I am trying to achieve
busy_time_start | busy_time_end
--------------------------------------------
2022-09-10T03:10:00Z | 2022-09-10T05:59:00Z
2022-09-10T06:05:00Z | 2022-09-10T09:59:00Z
I have a table that has a list of users that enter a chat room. It describes the user_id, enter_time, and leave_time.
What I would like to try and work out is what are the duration(s) that had the most amount of people in the chat room for a given day.
What does my data look like?
MySQL Schema Setup:
CREATE TABLE `user_log` (
`user_id` int DEFAULT NULL,
`enter_time` datetime DEFAULT NULL,
`leave_time` datetime DEFAULT NULL);
insert into user_log values
(1, '2022-09-10 01:00:00', '2022-09-10 12:00:00'),
(2, '2022-09-10 02:10:00', '2022-09-10 11:59:00'),
(3, '2022-09-10 03:30:00', '2022-09-10 10:59:00'),
(4, '2022-09-10 04:10:00', '2022-09-10 09:59:00'),
(5, '2022-09-10 02:05:00', '2022-09-10 05:59:00'),
(6, '2022-09-10 06:05:00', '2022-09-10 08:59:00');
Query 1:
select * from user_log
| user_id | enter_time | leave_time |
|---------|----------------------|----------------------|
| 1 | 2022-09-10T01:00:00Z | 2022-09-10T12:00:00Z |
| 2 | 2022-09-10T02:10:00Z | 2022-09-10T11:59:00Z |
| 3 | 2022-09-10T03:30:00Z | 2022-09-10T10:59:00Z |
| 4 | 2022-09-10T04:10:00Z | 2022-09-10T09:59:00Z |
| 5 | 2022-09-10T02:05:00Z | 2022-09-10T05:59:00Z |
| 6 | 2022-09-10T06:05:00Z | 2022-09-10T08:59:00Z |