简体   繁体   English

MySQL公式确定最大并发事件数

[英]MySQL formula to determine number of maximum concurrent events

I'm trying to think of a way to take two fields entries in a MySQL table (One being a timestamp eg, '2013-07-31 11:59:46' and the other being a duration of time in seconds eg, '55' and find all the records that overlap each other and how many of them are overlapped during that period of time. I already have a headache but I'm sure it can be done somehow? What would be a good way to get the return value? 我正在尝试一种方法来获取MySQL表中的两个字段条目(一个是时间戳,例如“ 2013-07-31 11:59:46”,另一个是以秒为单位的持续时间,例如,“ 55',然后找到所有相互重叠的记录,以及这段时间内有多少重叠的记录。我已经很头疼,但是我敢肯定,可以通过某种方式来完成它?值?

So for instance, say I have a total of 5 entries from Jan 1st 例如,从1月1日起,我总共有5条条目

2013-01-01 09:00:00 | 30     (an event that started at 9:00am and lasted 30 seconds)
2013-01-01 09:02:00 | 360    (an event that started at 9:02am and lasted 6 minutes)
2013-01-01 09:03:00 | 600    (an event that started at 9:03am and lasted 10 minutes)
2013-01-01 09:11:00 | 10    (an event that started at 9:11am and lasted 10 seconds)
2013-01-01 09:12:00 | 30    (an event that started at 9:12am and lasted 30 seconds)

Running against these entries I'd get a return value of "2" since that is the maximum amount of concurrent events. 针对这些条目运行,我将获得“ 2”的返回值,因为这是并发事件的最大数量。 (Events #2 & #3 overlap. Then, Event #2 ends before Event #3 and #4 start, which in turn, overlap. This does not change our return value as there are still only two concurrent events at any given time.) (事件#2和#3重叠。然后,事件#2在事件#3和#4开始之前结束,而事件#3和#4重叠。这不会改变我们的返回值,因为在任何给定时间只有两个并发事件。 )

Consider the following... 考虑以下...

 DROP TABLE IF EXISTS my_table;

 CREATE TABLE my_table
 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
 ,dt DATETIME NOT NULL
 ,duration INT NOT NULL
 );

 INSERT INTO my_table (dt,duration) VALUES
 ('2013-01-01 09:00:00',30),
 ('2013-01-01 09:02:00',360),
 ('2013-01-01 09:03:00',600),
 ('2013-01-01 09:11:00',10),
 ('2013-01-01 09:12:00',30);


  SELECT *
    FROM my_table x
    JOIN my_table y
      ON y.id <> x.id
     AND y.dt < (x.dt + INTERVAL x.duration SECOND)
     AND (y.dt + INTERVAL y.duration SECOND) > x.dt;
 +----+---------------------+----------+----+---------------------+----------+
 | id | dt                  | duration | id | dt                  | duration |
 +----+---------------------+----------+----+---------------------+----------+
 |  3 | 2013-01-01 09:03:00 |      600 |  2 | 2013-01-01 09:02:00 |      360 |
 |  2 | 2013-01-01 09:02:00 |      360 |  3 | 2013-01-01 09:03:00 |      600 |
 |  4 | 2013-01-01 09:11:00 |       10 |  3 | 2013-01-01 09:03:00 |      600 |
 |  5 | 2013-01-01 09:12:00 |       30 |  3 | 2013-01-01 09:03:00 |      600 |
 |  3 | 2013-01-01 09:03:00 |      600 |  4 | 2013-01-01 09:11:00 |       10 |
 |  3 | 2013-01-01 09:03:00 |      600 |  5 | 2013-01-01 09:12:00 |       30 |
 +----+---------------------+----------+----+---------------------+----------+

 or, if you prefer...

  SELECT *
    FROM my_table x
    JOIN my_table y
      ON y.id < x.id
     AND y.dt < (x.dt + INTERVAL x.duration SECOND)
     AND (y.dt + INTERVAL y.duration SECOND) > x.dt;
 +----+---------------------+----------+----+---------------------+----------+
 | id | dt                  | duration | id | dt                  | duration |
 +----+---------------------+----------+----+---------------------+----------+
 |  3 | 2013-01-01 09:03:00 |      600 |  2 | 2013-01-01 09:02:00 |      360 |
 |  4 | 2013-01-01 09:11:00 |       10 |  3 | 2013-01-01 09:03:00 |      600 |
 |  5 | 2013-01-01 09:12:00 |       30 |  3 | 2013-01-01 09:03:00 |      600 |
 +----+---------------------+----------+----+---------------------+----------+

For 'event 1', 2 things are overlapping (item 2 & item 3). 对于“事件1”,有2件事是重叠的(项目2和项目3)。 For 'event 2', 2 things are overlapping (item 3 & item 4). 对于“事件2”,有2件事是重叠的(项目3和项目4)。 For 'event 3', 2 things are overlapping (item 3 & item 5). 对于“事件3”,有2件事是重叠的(项目3和项目5)。

   9.00  9.01  9.02  9.03  9.04  9.05  9.06  9.07  9.08  9.09  9.10  9.11  9.12  9.13
 1   |-|
 2               |-----------------------------------|
 3                     |-----------------------------------------------------------|
 4                                                                     |-|
 5                                                                           |-|

If you like, we can say that: 如果您愿意,我们可以这样说:

nothing overlaps item 1
1 thing overlaps item 2 (item 3), 
3 things overlap item 3 (items 2, 4, & 5), and
1 thing (item 3) overlaps each of items 4 & 5!


 SELECT x.id
      , COUNT(y.id) overlaps
    FROM my_table x
    LEFT
    JOIN my_table y
      ON y.id <> x.id
     AND y.dt < (x.dt + INTERVAL x.duration SECOND)
     AND (y.dt + INTERVAL y.duration SECOND) > x.dt
   GROUP
      By x.id;

 +----+----------+
 | id | overlaps |
 +----+----------+
 |  1 |        0 |
 |  2 |        1 |
 |  3 |        3 |
 |  4 |        1 |
 |  5 |        1 |
 +----+----------+   

A simple ORDER BY and LIMIT will you get you the highest of these. 一个简单的ORDER BY和LIMIT将使您获得最高收益。

I won't accept payment here - but some points would be nice! 我在这里不接受付款-但有些要点会不错!

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

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