我们如何在mysql中找到多次的交叉时间?我有这张桌子:
id | circuit_id | downtime | uptime | duration | Location
---+------------+-----------------+-----------------+----------+----------
1 | 1 | 6/12/2019 11:22 | 6/12/2019 11:27 | 0:05:00 | Bhopal
2 | 2 | 6/12/2019 04:55 | 6/12/2019 07:19 | 2:24:00 | Bhopal
3 | 3 | 6/04/2019 17:59 | 6/06/2019 18:57 | 48:58:00 | Bhopal
4 | 4 | 6/02/2019 03:06 | 6/02/2019 03:12 | 0:06:00 | Bhopal
5 | 1 | 6/01/2019 19:49 | 6/01/2019 20:00 | 0:11:00 | Bhopal
6 | 2 | 5/24/2019 14:59 | 5/24/2019 15:03 | 0:04:00 | Bhopal
7 | 3 | 5/19/2019 03:15 | 5/19/2019 03:54 | 0:39:00 | Bhopal
8 | 4 | 5/15/2019 19:47 | 5/15/2019 19:50 | 0:03:00 | Bhopal
9 | 1 | 5/10/2019 01:21 | 5/10/2019 03:52 | 0:31:00 | Bhopal
10 | 2 | 5/10/2019 02:55 | 5/10/2019 03:37 | 2:42:00 | Bhopal
11 | 3 | 5/10/2019 03:18 | 5/10/2019 04:37 | 1:19:00 | Bhopal
12 | 4 | 5/10/2019 03:20 | 5/10/2019 03:30 | 1:25:00 | Bhopal
13 | 1 | 5/09/2019 05:06 | 5/09/2019 14:17 | 9:11:00 | Bhopal
14 | 2 | 5/09/2019 04:31 | 5/09/2019 04:34 | 0:03:00 | Bhopal
15 | 3 | 4/17/2019 18:37 | 4/17/2019 18:44 | 0:07:00 | Bhopal
16 | 6 | 4/17/2019 17:23 | 4/17/2019 18:05 | 0:42:00 | Delhi
17 | 7 | 4/17/2019 17:00 | 4/17/2019 17:05 | 0:05:00 | Delhi
18 | 8 | 4/10/2019 06:16 | 4/10/2019 06:22 | 0:06:00 | Delhi
19 | 9 | 4/10/2019 02:20 | 4/10/2019 02:45 | 0:25:00 | Delhi
20 | 6 | 4/08/2019 23:01 | 4/08/2019 23:05 | 0:04:00 | Delhi
21 | 7 | 4/05/2019 11:20 | 4/05/2019 11:27 | 0:07:00 | Delhi
22 | 8 | 4/04/2019 01:16 | 4/04/2019 01:27 | 0:11:00 | Delhi
23 | 9 | 4/02/2019 16:30 | 4/02/2019 16:31 | 0:01:00 | Delhi
如何检查按日期和按地点排列的时间重叠时间?如果所有电路在同一时间范围内均处于关闭状态,则将其视为关闭,否则视为打开。我只需要停机数据。
在2019年5月10日,博帕尔在10:00休息了十分钟,因为那一天的所有四个巡回赛在03:20到03:30之间都停了下来。
Location | downtime | date
---------+----------+-----------
Bhopal | 00:10 | 2019-05-10
答案 0 :(得分:0)
您可以使用自联接匹配重叠项,并使用having
子句过滤包含所有链接的子项。
要获取电路总数,您可以执行另一个自联接以找到每个位置在表中出现过的所有电路:
select a.location,
num_circuits,
a.downtime,
min(b.uptime) uptime,
timediff(min(b.uptime), a.downtime) duration,
count(distinct b.circuit_id) num_circuits_down
from tbl a
inner join tbl b
on a.location = b.location
and a.downtime between b.downtime and b.uptime
inner join (
select location, count(distinct circuit_id) num_circuits
from tbl
group by location
) c
on a.location = c.location
group by a.location, num_circuits, a.downtime
having count(distinct b.circuit_id) = num_circuits;
最好有一个单独的参考表,列出每个位置的电路(在您的情况下,每个位置4条记录)。 然后,上面的查询将从该引用表中进行选择,所以代替:
select location, count(distinct circuit_id) num_circuits
from tbl
group by location
...您会这样做:
select location, count(distinct circuit_id) num_circuits
from reftable
group by location
这将使上面的查询更加有效和可靠。