简体   繁体   English

在 Mysql 中未排序的具有多个日期范围的给定月份的日期范围间隙查询

[英]Query for gaps in date ranges for given month with multiple date ranges which are not sorted in Mysql

I have a pretty tricky calendar gap project that I could use some assistance.我有一个非常棘手的日历间隙项目,我可以使用一些帮助。 I hope I have outlined my issue well enough and really appreciate any assistance in solving this.我希望我已经很好地概述了我的问题,并且非常感谢解决这个问题的任何帮助。

I need to find calendar gaps between multiple date ranges.我需要找到多个日期范围之间的日历间隔。 I have a mysql DB with car rentals.我有一个带汽车租赁的 mysql DB。 I need a query that shows all cars that have a gap during a given month.我需要一个查询来显示在给定月份内有间隙的所有汽车。 I am using mysql 5.7, so I CANNOT use recursive queries.我使用的是 mysql 5.7,所以我不能使用递归查询。

Here is what I have in my DB (mysql):这是我的数据库(mysql)中的内容:

TABLE:桌子:

Car   ||  from_date   || to_date
Ford  ||  2021-05-01  || 2021-05-09 (10 day gap)
Ford  ||  2021-05-20  || 2021-05-30 (0 day gap till next)
Ford  ||  2021-06-01  || 2021-06-07 (7 day gap till next)
Ford  ||  2021-06-15  || 2021-06-20 (5 day gap till next)
Ford  ||  2021-06-26  || 2021-06-30 (0 day gap till next)
Chevy ||  2021-04-20  || 2021-04-29 (7 day gap till next)
Chevy ||  2021-05-07  || 2021-05-12 (7 day gap till next)
Chevy ||  2021-05-20  || 2021-05-23 (8 day gap till next)
Chevy ||  2021-06-02  || 2021-06-10 (13 day gap till next)
Tesla ||  2021-04-15  || 2021-04-30 
Tesla ||  2021-05-01  || 2021-05-30 

SCENARIOS: In my table above I have created the following scenarios: Ford: has gaps completely within May and June.场景:在我上面的表格中,我创建了以下场景: 福特:在 5 月和 6 月之间完全有差距。 Chevy: has overlapping gaps at start and end of month Tesla: has all June open雪佛兰:在月初和月底有重叠的差距特斯拉:整个六月都开放

WARNING: These records are sorted by from_date in this example, but they may not be in real life, so you cannot assume their order.警告:在本例中,这些记录按 from_date 排序,但它们可能不在现实生活中,因此您不能假设它们的顺序。 This is why the "show gaps" example referenced below did not work for me.这就是为什么下面引用的“显示差距”示例对我不起作用的原因。

EXPECTED/ACCEPTABLE RESULTS: The tables below shows what I expect, but I am fine with just knowing if a car has ANY gap during the given month.预期/可接受的结果:下表显示了我的预期,但只要知道汽车在给定月份内是否有任何差距,我就可以了。 So, for 05, I do not need Chevy listed 3 times.所以,对于 05,我不需要雪佛兰上市 3 次。 If we can build this faster by just pulling the first occurrence.如果我们可以通过拉动第一次出现来更快地构建它。 Just showing the first match of each car would be fine.只显示每辆车的第一场比赛就可以了。

TRUE RESULT FOR: month = 05, gap = 7正确结果:月 = 05,差距 = 7

+------------+------------+------------+------+
|    Car     |  gap_start |  gap_end   | gap  |
+------------+------------+------------+------+
|    Ford    | 2021-05-09 | 2021-05-20 |  10  |
|    Chevy   | 2021-04-29 | 2014-05-07 |   7  |
|    Chevy   | 2021-05-12 | 2014-05-20 |   7  |
|    Chevy   | 2021-05-23 | 2014-06-02 |   8  |
+------------+------------+------------+------+

ACCEPTABLE RESULTS FOR: month = 05, gap = 7可接受的结果:月 = 05,差距 = 7

+------------+------------+
|    Car     |  has_gap   |
+------------+------------+
|    Ford    |     1      |
|    Chevy   |     1      |
+------------+------------+

TRUE RESULT FOR: month = 06, gap = 5正确结果:月 = 06,差距 = 5

+------------+------------+------------+------+
|    Car     |  gap_start |  gap_end   | gap  |
+------------+------------+------------+------+
|    Ford    | 2021-06-07 | 2021-06-15 |   7  |
|    Ford    | 2021-06-20 | 2021-06-26 |   5  |
|    Chevy   | 2021-06-10 | 2021-06-31 |  20  |
|    Tesla   | 2021-06-01 | 2021-06-30 |  30  |
+------------+------------+------------+------+

ACCEPTABLE RESULTS FOR: month = 06, gap = 5可接受的结果:月 = 06,差距 = 5

+------------+----------+
|    Car     |  has_gap |
+------------+----------+
|    Ford    |     1    |
|    Chevy   |     1    |
|    Tesla   |     1    |
+------------+----------+

NOTE: I realize my math is off for the Tesla because I have had to fake the dates.注意:我意识到我的数学对特斯拉来说是错误的,因为我不得不伪造日期。 I am not as concerned about how that one is returned, as long as Tesla is included in the results.只要特斯拉包含在结果中,我并不关心如何返回。

NEXT STEPS: I will probably build a calendar (horizontal strip display) for each car with their booked dates, so I am thinking once I get the results I would loop through them and pull all the reservations for that given month to build that calendar.下一步:我可能会为每辆车及其预订日期建立一个日历(水平条形显示),所以我想一旦我得到结果,我会遍历它们并提取该给定月份的所有预订来建立那个日历。 Unless someone can figure how to do all that in one query and keep it fast.除非有人能想出如何在一个查询中完成所有这些操作并保持快速。

I have spend some time trying to cover all my bases in asking this question and hope I haven't made any mistakes.我花了一些时间试图涵盖我提出这个问题的所有基础,并希望我没有犯任何错误。 Please feel free to ask questions or for clarification.请随时提出问题或进行澄清。 AND, THANKS IN ADVANCE FOR ANY HELP.并且,提前感谢您的帮助。

Similar question, but not close enough to work.类似的问题,但不够接近工作。 Show gaps between dates in MySQL 在 MySQL 中显示日期之间的间隔

Basically, I think you can use not exists and aggregation.基本上,我认为你可以使用not exists和聚合。 To get the cars during a time period that have a gap:要在有间隙的时间段内获得汽车:

select car, min(from_date)
from t
where from_date >= $month_start and from_date < $month_end and
      not exists (select 1
                  from t t2
                  where t2.car = t.car and
                        t2.from_date > t.to_date and
                        t2.from_date < t.to_date + interval $gap day
                 )
group by car;

Here is a db<>fiddle. 是一个 db<>fiddle。

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

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