I have booking sample data as follows:
-----------------------------------------------------------------------------------
hotel_id | roomtype_id | status | booking_timestamp | checkin_date | checkout_date
-----------------------------------------------------------------------------------
1 | 1 | new | 2023-02-02 00:15:30 | 2023-02-03 | 2023-02-05
1 | 1 | new | 2023-02-04 09:10:15 | 2023-02-04 | 2023-02-04
1 | 1 | new | 2023-02-07 03:00:10 | 2023-02-09 | 2023-02-10
1 | 1 | new | 2023-02-08 04:10:10 | 2023-02-09 | 2023-02-10
1 | 1 | cancelled| 2023-02-08 10:15:30 | 2023-02-09 | 2023-02-10
2 | 2 | new | 2023-02-03 06:40:14 | 2023-02-06 | 2023-02-07
2 | 2 | new | 2023-02-05 08:10:14 | 2023-02-06 | 2023-02-08
Each row represent 1 room of a particular room type. I’m looking for effective way to calculate the total number of room sold exclude cancellation for each date in a particular date range of each room type in each hotel. Let’s say the date range that I want is started from 2023-02-01
to 2023-02-12
. The expected output and explanation should be the following.
--------------------------------------------------------------
hotel_id | roomtype_id | date | room_sold
--------------------------------------------------------------
1 | 1 | 2023-02-01 | 0 No new check-in yet
1 | 1 | 2023-02-02 | 0 No new check-in yet
1 | 1 | 2023-02-03 | 1 one room got checked-in (1st row of data)
1 | 1 | 2023-02-04 | 2 another room got check-in (2nd row of data)
1 | 1 | 2023-02-05 | 0 all rooms of this room type got check-out
1 | 1 | 2023-02-06 | 0 No new check-in yet
1 | 1 | 2023-02-07 | 0 No new check-in yet
1 | 1 | 2023-02-09 | 1 2 checked-in but 1 cancelled (row 3 to 5)
1 | 1 | 2023-02-10 | 0 all rooms of this room type got check-out
2 | 2 | 2023-02-01 | 0 No new check-in yet
2 | 2 | 2023-02-02 | 0 No new check-in yet
2 | 2 | 2023-02-03 | 0 No new check-in yet
2 | 2 | 2023-02-04 | 0 No new check-in yet
2 | 2 | 2023-02-05 | 0 No new check-in yet
2 | 2 | 2023-02-06 | 2 2 rooms got checked-in (row 6 and 7)
2 | 2 | 2023-02-07 | 1 1 room checked-out (row 6)
2 | 2 | 2023-02-08 | 0 1 room checked-out (row 7)
2 | 2 | 2023-02-09 | 0 No new check-in yet
2 | 2 | 2023-02-10 | 0 No new check-in yet
2 | 2 | 2023-02-11 | 0 No new check-in yet
2 | 2 | 2023-02-12 | 0 No new check-in yet
May I have your advise how to solve this problem?
3
Answers
fiddle
If there exists a calendar table which contains the dates range which you need the output to obtain for then use it instead of
cte1
(maybe with according WHERE). Accordingly if you have a table with hotels and rooms identifiers then use it instead ofcte2
.You should generate a series of dates for the given date range using a
RECURSIVE
and join this series of dates with your booking data. After that, group the joined data by hotel_id, roomtype_id, and date. Lastly, use conditional aggregation to count the number of rooms sold for each date, excluding cancellations:Generate a date range list using recursive first, and then join them with the check-in date.
Notes: According to your expected output, I don’t calculate the check-out date to the amount, unless the check-in date and check-out date are on the same date.