查找两个日期之间缺失的日期范围

时间:2021-05-27 07:55:07

标签: sql sql-server date date-range between

我有包含某些日期范围数据的表格。当用户选择开始日期和结束日期时,结果集将类似于这两个日期之间的所有日期范围以及这两个日期之间的所有缺失日期范围。
例如:
日期范围表

ID| fromdate  | todate    |
----------------------------
1 | 5-May-21  | 10-May-21 |
2 | 17-May-21 | 25-May-21 |

这是我的主表,我在下面提到了我想要的所有结果集

如果用户选择:2021 年 5 月 5 日2021 年 5 月 25 日
预期结果:

ID| fromdate  | todate    |
----------------------------
1 | 5-May-21  | 10-May-21 |
0 | 11-May-21 | 16-May-21 |
2 | 17-May-21 | 25-May-21 |

如果用户选择:2021 年 5 月 6 日2021 年 5 月 23 日
预期结果:

ID| fromdate  | todate    |
-----------------------------
1 | 6-May-21  | 10-May-21 |
0 | 11-May-21 | 16-May-21 |
2 | 17-May-21 | 23-May-21 |

如果用户选择:1-May-202128-May-2021
预期结果:

ID| fromdate  | todate    |
----------------------------
1 | 1-May-21  | 4-May-21  |
1 | 5-May-21  | 10-May-21 |
0 | 11-May-21 | 16-May-21 |
2 | 17-May-21 | 25-May-21 |
2 | 26-May-21 | 28-May-21 |

这里有一些不相似但试图找到的问题:

SQL Find missing date ranges

SQL how to write a query that return missing date ranges?

提前致谢。

2 个答案:

答案 0 :(得分:0)

请注意,我在此假设您的最终预期结果的预期结果错误,因为它与其他 2 个不匹配。最后一个和第一个最后一组的预期结果中的行都有一个不是 ID0 值,但没有给出为什么的解释。因此,我假设该值应该是 0,就像“中间”中的行一样。

为此,我使用 Tally 来获取您需要的日期范围之间的所有日期; Tally 限制为 1,000 行,有点少 3 年,但如果您需要更多行,您可以交叉加入 N。然后我使用该计数来创建内联日历表。接下来,我将日历LEFT JOIN 用于您的数据,并使用间隙和孤岛方法将值分组。最后,我对这些组进行汇总,获取每个组中的 MINMAX 日期:

USE Sandbox;
GO

CREATE TABLE dbo.YourTable (ID int,
                            FromDate date,
                            ToDate date);
INSERT INTO dbo.YourTable
VALUES(1,'20210505','20210510'),
      (2,'20210517','20210525');
GO

DECLARE @StartDate date = '20210501',
        @EndDate date = '20210528';

WITH N AS(
    SELECT N
    FROM (VALUES(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL))N(N)),
Tally AS(
    SELECT 0 AS I
    UNION ALL
    SELECT TOP (DATEDIFF(DAY, @StartDate, @EndDate))
           ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS I
    FROM N N1, N N2, N N3), --1000 days
Dates AS(
    SELECT DATEADD(DAY, T.I, @StartDate) AS [Date],
           T.I
    FROM Tally T),
Grps AS(
    SELECT D.[Date],
           YT.ID,
           D.I - ROW_NUMBER() OVER (PARTITION BY ID ORDER BY D.[Date]) AS Grp
    FROM Dates D
         LEFT JOIN dbo.YourTable YT ON D.[Date] >= YT.FromDate AND D.[Date] <= YT.ToDate)
SELECT ISNULL(MAX(G.ID),0) AS ID,
       MIN(G.[Date]) AS FromDate,
       MAX(G.[Date]) AS ToDate
FROM Grps G
GROUP BY G.Grp
ORDER BY FromDate ASC;

GO
DROP TABLE dbo.YourTable;
                          

db<>fiddle

答案 1 :(得分:0)

您可以使用 union all

-- first get the existing rows
select id,
       (case when fromdate < @fromdate then @fromdate else fromdate end) as fromdate,
       (case when todate > @todate then @todate else todate end) as to_date
from t
where fromdate < @todate and
      todate > @fromdate
union all
-- then the in-between rows
select 0, dateadd(day, 1, todate) as fromdate, next_fromdate as todate
from (select t.*,
             dateadd(day, -1, lead(fromdate) over (order by fromdate)) as next_fromdate
      from t
     ) t
where fromdate >= @fromdate and todate <= @todate and
      next_todate is not null
union all
-- then the earliest record, if any
select 0, @fromdate, min(fromdate)
from t
where todate > @fromdate
having @fromdate < min(fromdate) 
union all
-- then the final record, if any
select 0, max(todate), @todate
from t
where fromdate < @todate
having @todate > max(todate);

Here 是一个 db<>fiddle。