我正在尝试计算今天两侧 2 天的假期预订 (B.ID)。
它有效,但我的结果是分开的,因为我也必须介绍假期的结束日期,每个开始日期都会有所不同(假期有不同的持续时间)。
分开我的计数。我需要的是每个日期的计数。有没有办法解决这个问题?我只是想从组中排除 vwReturnDate ,但必须把它放在那里,因为我已经在我的计数中使用了它。
用英语我想要 - 对于每个 [date] 计算 [B.id] 的数量,其中 [B.Depart] <= [date] 和 [vwReturnDate] > [date]
DECLARE @startDate DATE
DECLARE @endDate DATE
SET @startDate = Getdate()-2
SET @endDate = Getdate()+2;
WITH dates(Date) AS
( SELECT @startdate as Date
UNION ALL
SELECT DATEADD(d,1,[Date])
FROM dates
WHERE DATE < @enddate )
SELECT
[Date] as 'Calendar Date',
--CONVERT(VARCHAR(10), [Date],103) AS 'Date'
-- ,CONVERT(CHAR(2), [Date], 113) AS 'Day'
-- ,CONVERT(CHAR(4), [Date], 100) AS 'Month'
-- ,CONVERT(CHAR(4), [Date], 120) AS 'Year',
Case when B.Depart <= [date] AND vwR.ReturnDate >=[date] then count (B.ID) end AS 'Number of holidays live on date'
FROM [dates]
left join Booking B on B.depart=[Date]
inner join Quote Q on Q.ID=B.QuoteID
inner join vwReturnDate vwR on vwR.ID=B.ID
Group by [date], B.depart, vwR.ReturnDate
order by [date]
OPTION (MAXRECURSION 0)
GO