我正在处理预订申请,我正在创建一个按月计算酒店入住率的查询。要做到这一点,我必须检查所有预订的签入和结帐日期,并增加一些变量并将它们与相应的月份相关联。 我只需要衡量当年的这个比率。 如果预订的结账日期或签到日期是月份是3月(例如),我需要增加与该月的ocupation相对应的变量。我需要在那一年的每个预订中执行此操作。
预订有签入日期和结帐日期,其他属性与此查询无关,输入参数为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'
答案 0 :(得分:2)
假设您想要计算CheckIn和CheckOut在该月的1个职业的同一个月内发生的预订,以及每月将CheckIn和CheckOut在不同月份中作为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