我正在开发一个预订应用程序,我正在创建一个查询,用于按月计算酒店的入住率。为此,我必须检查所有预订的入住和退房日期,并增加一些变量并将其与相应的月份相关联。我只需要在当年衡量这个比率。如果预订的退房日期或入住日期在月份为三月(例如),我需要增加与该月的入住率相对应的变量。我需要为该年间隔内的每次预订执行此操作。
预订具有入住日期和退房日期,其他属性与此查询无关,输入参数为 BeginDate (2015-01-01) 和 EndDate (2015-12-31)。
Month Ocupation
[JANUARY] | 29
[FEBRUARY] | 20
[MARCH] | 30
....... | ...
这就是我到目前为止所做的,此刻我有点迷茫。任何帮助将不胜感激。
Select
SUM(CASE WHEN datename(month, [CheckIn]) = 'January' or datename(month, [CheckOut]) = 'January' THEN 1 ELSE 0 END) January,
SUM(CASE WHEN datename(month, [CheckIn]) = 'February' or datename(month, [CheckOut]) = 'February' THEN 1 ELSE 0 END) February,
SUM(CASE WHEN datename(month, [CheckIn]) = 'March' or datename(month, [CheckOut]) = 'March' THEN 1 ELSE 0 END) March,
SUM(CASE WHEN datename(month, [CheckIn]) = 'April' or datename(month, [CheckOut]) = 'April' THEN 1 ELSE 0 END) April,
SUM(CASE WHEN datename(month, [CheckIn]) = 'May' or datename(month, [CheckOut]) = 'May' THEN 1 ELSE 0 END) May,
SUM(CASE WHEN datename(month, [CheckIn]) = 'June' or datename(month, [CheckOut]) = 'June' THEN 1 ELSE 0 END) June,
SUM(CASE WHEN datename(month, [CheckIn]) = 'July' or datename(month, [CheckOut]) = 'July' THEN 1 ELSE 0 END) July,
SUM(CASE WHEN datename(month, [CheckIn]) = 'August' or datename(month, [CheckOut]) = 'August' THEN 1 ELSE 0 END) August,
SUM(CASE WHEN datename(month, [CheckIn]) = 'September' or datename(month, [CheckOut]) = 'September' THEN 1 ELSE 0 END) September,
SUM(CASE WHEN datename(month, [CheckIn]) = 'October' or datename(month, [CheckOut]) = 'October' THEN 1 ELSE 0 END) October,
SUM(CASE WHEN datename(month, [CheckIn]) = 'November' or datename(month, [CheckOut]) = 'November' THEN 1 ELSE 0 END) November,
SUM(CASE WHEN datename(month, [CheckIn]) = 'December' or datename(month, [CheckOut]) = 'December' THEN 1 ELSE 0 END) December
FROM {Booking} INNER JOIN {Status} ON {Booking}.[StatusId] = {Status}.[Id]
WHERE {Booking}.[CheckIn] >= @BeginDate AND {Booking}.[CheckOut] <= @EndDate AND {Status}.[Label] <> 'Canceled'
假设您要将签入和签出发生在该月的 1 个职业同一月份的预订计入和退房,并将不同月份的签入和签出预订计为每月 1 个,则此查询应有效:
SELECT
Month,
'Occupation' = COUNT(MonthNo)
FROM Booking
OUTER APPLY (
SELECT MonthNo, Month FROM (
VALUES (1, 'January'), (2, 'February'), (3, 'March'),(4, 'April'), (5, 'May')
) AS Months (MonthNo, Month)
WHERE MonthNo BETWEEN MONTH(CheckIn) AND MONTH(CheckOut)
) c
-- WHERE CheckIn >= @BeginDate
-- AND CheckOut <= @EndDate
-- AND [Status].[Label] <> 'Canceled'
GROUP BY Month, MonthNo ORDER BY MonthNo
我使用表值构造函数使带有月份数字和名称的表内联,但是如果您已经有一个合适的日历表(您应该这样做),请改用它。
给定一个输入表,如下所示:
Id CheckIn CheckOut
1 2015-01-30 2015-03-31 -- 1 for Jan, Feb, Mar
2 2015-01-03 2015-01-05 -- 1 for Jan
3 2015-01-30 2015-02-05 -- 1 for Jan, Feb
4 2015-01-30 2015-01-31 -- 1 for Jan
5 2015-02-01 2015-02-03 -- 1 for Feb
6 2015-03-22 2015-04-01 -- 1 for Mar, Apr
7 2015-03-23 2015-04-03 -- 1 for Mar, Apr
8 2015-03-30 2015-03-31 -- 1 for Mar
9 2015-04-01 2015-04-03 -- 1 for Apr
10 2015-04-28 2015-05-01 -- 1 for Apr, May
11 2015-05-01 2015-05-03 -- 1 for May
它将产生如下输出:
Month Occupation
January 4
February 3
March 4
April 4
May 2