简体   繁体   English

计算 MySQL 中重叠日期范围的最大数量

[英]Count maximum number of overlapping date ranges in MySQL

I've having a big headache on the following situation.我对以下情况感到非常头疼。 In MySQL I have a table with more than 40000 entries that look like that:在 MySQL 中,我有一个包含 40000 多个条目的表,如下所示:

create table if not exists sessions
(
    startt datetime null,
    endt datetime null,
    id int auto_increment
        primary key
);

INSERT INTO sessions (startt, endt, id) VALUES 
('2020-02-06 10:33:55', '2020-02-06 10:34:41', 20356),
('2020-02-06 10:33:14', '2020-02-06 10:33:57', 20355),
('2020-02-06 10:32:55', '2020-02-06 10:33:32', 20354),
('2020-02-06 10:33:03', '2020-02-06 10:33:12', 20353),
('2020-02-06 10:31:38', '2020-02-06 10:32:41', 20352),
('2020-02-06 09:48:44', '2020-02-06 09:50:37', 20351);

SELECT * FROM sessions;
+---------------------+---------------------+-------+
| startt              | endt                | id    |
+---------------------+---------------------+-------+
| 2020-02-06 10:33:55 | 2020-02-06 10:34:41 | 20356 |
| 2020-02-06 10:33:14 | 2020-02-06 10:33:57 | 20355 |
| 2020-02-06 10:32:55 | 2020-02-06 10:33:32 | 20354 |
| 2020-02-06 10:33:03 | 2020-02-06 10:33:12 | 20353 |
| 2020-02-06 10:31:38 | 2020-02-06 10:32:41 | 20352 |
| 2020-02-06 09:48:44 | 2020-02-06 09:50:37 | 20351 |
+---------------------+---------------------+-------+
6 rows in set (0.00 sec)

fiddle https://www.db-fiddle.com/f/49bNZ7863gv6RThoPpuiid/0小提琴https://www.db-fiddle.com/f/49bNZ7863gv6RThoPpuiid/0

The date and time ranges are sessions.日期和时间范围是会话。 What I want to find out is: what is the maximum number of sessions that existed at one time?我想知道的是:一次存在的最大会话数是多少?

I found a lot of things like how to find out if a date is in the range of other dates etc. which didn't really help as I want to find out how many users there were at the maximum peak.我发现了很多事情,比如如何确定某个日期是否在其他日期的范围内等等。这并没有真正帮助,因为我想知道在最大峰值时有多少用户。

Here is one option using window functions (available in MySQL 8.0):这是使用窗口函数的一种选择(在 MySQL 8.0 中可用):

select dt, sum(nb) over(order by dt) sum_nb
from (
    select starttt dt, 1 nb from mytable 
    union all select endt, -1 from mytable 
) t
order by sum_nb desc
limit 1

The idea is to unpivot the dataset;这个想法是对数据集进行反透视; the count of concurrent sessions increases by 1 at the beginning of each session, and decreses by 1 at its end.并发会话数在每个会话开始时增加 1,在结束时减少 1。

You can then compute the number of concurrent sessions at each point in time with a window sum.然后,您可以使用窗口总和计算每个时间点的并发会话数。

The last step is ordering by session count and keeping the first row only.最后一步是按会话计数排序并仅保留第一行。

I would phrase this as an aggregation with a window function:我会将其表述为带有窗口函数的聚合:

select dt, sum(sum(inc)) over (order by dt) as overlapping
from (select starttt as dt, 1 as inc
      from mytable union all
      select endt, -1  as inc
      from mytable 
     ) t
group by dt
order by overlapping desc
limit 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM