获取日期范围之间的可用日期
Get available dates between range of dates
我有一个 属性 出租网站
所以我想知道 属性 是否适用于某个日期范围
首先我将日期范围定义为:
DECLARE @AvailableRentalStartingDate DATETIME = '2022-04-11'
, @AvailableRentalEndingDate DATETIME = '2022-04-24'
现在 属性 租金如下:
DECLARE @Rentals AS TABLE
(
[PropertyId] UNIQUEIDENTIFIER,
[StartingDate] DATE,
[EndingDate] DATE
)
INSERT INTO @Rentals ([PropertyId], [StartingDate], [EndingDate])
VALUES ('A5B2B505-EC6F-EC11-A004-00155E014807','2022-04-11 16:47:20.897', '2022-04-14 16:47:20.897'),
('A5B2B505-EC6F-EC11-A004-00155E014807','2022-04-16 16:47:20.897','2022-04-21 16:47:20.897')
如您所见,我们有可用日期 2022-04-15
日期table
DECLARE @Dates AS TABLE
(
DateName DATE
)
DECLARE @TotalDays INT = 365;
WHILE @TotalDays > 0
BEGIN
INSERT INTO @Dates ([DateName])
SELECT DATEADD(DAY, @TotalDays, '2021-12-31');
SELECT @TotalDays = @TotalDays - 1;
END
然后select
SELECT [R].[PropertyId], [D].[DateName], CASE WHEN [R].[StartingDate] IS NULL THEN 1 ELSE 0 END AS [IsAvailable]
FROM @Dates AS D
LEFT JOIN @Rentals R ON [D].[DateName] >= [R].[StartingDate] AND [D].[DateName] <= [R].[EndingDate]
WHERE [D].[DateName] BETWEEN @AvailableRentalStartingDate AND @AvailableRentalEndingDate
AND [R].[PropertyId] = 'A5B2B505-EC6F-EC11-A004-00155E014807'
ORDER BY [D].[DateName]
问题是它无法在可用日期 2022-04-15
上识别空值,它只是 return 不可用日期。
我只想知道 propertyId
是否可用,在这种情况下它应该可用,因为 2022-04-15
可用。我怎样才能让一行显示可用的真实?此致
你的问题是propertyid的where子句;
您应该将其作为 JOIN 条件。
WHERE 子句和 ON 条件可以在 INNER JOIN 中互换使用,但在 OUTER JOIN 中它们会影响含义。
SELECT [R].[PropertyId], [D].[DateName], CASE WHEN [R].[StartingDate] IS NULL THEN 1 ELSE 0 END AS [IsAvailable]
FROM @Dates AS D
LEFT JOIN @Rentals R ON [D].[DateName] >= [R].[StartingDate] AND [D].[DateName] <= [R].[EndingDate]
AND [R].[PropertyId] = 'A5B2B505-EC6F-EC11-A004-00155E014807'
WHERE [D].[DateName] BETWEEN @AvailableRentalStartingDate AND @AvailableRentalEndingDate
ORDER BY [D].[DateName]
我有一个 属性 出租网站
所以我想知道 属性 是否适用于某个日期范围
首先我将日期范围定义为:
DECLARE @AvailableRentalStartingDate DATETIME = '2022-04-11'
, @AvailableRentalEndingDate DATETIME = '2022-04-24'
现在 属性 租金如下:
DECLARE @Rentals AS TABLE
(
[PropertyId] UNIQUEIDENTIFIER,
[StartingDate] DATE,
[EndingDate] DATE
)
INSERT INTO @Rentals ([PropertyId], [StartingDate], [EndingDate])
VALUES ('A5B2B505-EC6F-EC11-A004-00155E014807','2022-04-11 16:47:20.897', '2022-04-14 16:47:20.897'),
('A5B2B505-EC6F-EC11-A004-00155E014807','2022-04-16 16:47:20.897','2022-04-21 16:47:20.897')
如您所见,我们有可用日期 2022-04-15
日期table
DECLARE @Dates AS TABLE
(
DateName DATE
)
DECLARE @TotalDays INT = 365;
WHILE @TotalDays > 0
BEGIN
INSERT INTO @Dates ([DateName])
SELECT DATEADD(DAY, @TotalDays, '2021-12-31');
SELECT @TotalDays = @TotalDays - 1;
END
然后select
SELECT [R].[PropertyId], [D].[DateName], CASE WHEN [R].[StartingDate] IS NULL THEN 1 ELSE 0 END AS [IsAvailable]
FROM @Dates AS D
LEFT JOIN @Rentals R ON [D].[DateName] >= [R].[StartingDate] AND [D].[DateName] <= [R].[EndingDate]
WHERE [D].[DateName] BETWEEN @AvailableRentalStartingDate AND @AvailableRentalEndingDate
AND [R].[PropertyId] = 'A5B2B505-EC6F-EC11-A004-00155E014807'
ORDER BY [D].[DateName]
问题是它无法在可用日期 2022-04-15
上识别空值,它只是 return 不可用日期。
我只想知道 propertyId
是否可用,在这种情况下它应该可用,因为 2022-04-15
可用。我怎样才能让一行显示可用的真实?此致
你的问题是propertyid的where子句; 您应该将其作为 JOIN 条件。 WHERE 子句和 ON 条件可以在 INNER JOIN 中互换使用,但在 OUTER JOIN 中它们会影响含义。
SELECT [R].[PropertyId], [D].[DateName], CASE WHEN [R].[StartingDate] IS NULL THEN 1 ELSE 0 END AS [IsAvailable]
FROM @Dates AS D
LEFT JOIN @Rentals R ON [D].[DateName] >= [R].[StartingDate] AND [D].[DateName] <= [R].[EndingDate]
AND [R].[PropertyId] = 'A5B2B505-EC6F-EC11-A004-00155E014807'
WHERE [D].[DateName] BETWEEN @AvailableRentalStartingDate AND @AvailableRentalEndingDate
ORDER BY [D].[DateName]