如何通过考虑PostgreSQL中的重叠天数来扩大日期范围?
给定下表使用以下SQL语句在PostgreSQL数据库中创建的表格:
CREATE TABLE reservations (
reservation_id INT PRIMARY KEY,
check_in_date DATE NOT NULL,
check_out_date DATE NOT NULL
);
INSERT INTO reservations VALUES
(1, '2021-01-04', '2021-01-05'),
(2, '2021-01-05', '2021-01-07'),
(3, '2021-01-06', '2021-01-07'),
(4, '2021-01-06', '2021-01-08'),
(5, '2021-01-08', '2021-01-09'),
(6, '2021-01-08', '2021-01-09'),
(7, '2021-01-08', '2021-01-10'),
(8, '2021-01-13', '2021-01-17'),
(9, '2021-01-13', '2021-01-14'),
(10, '2021-01-14', '2021-01-15'),
(11, '2021-01-15', '2021-01-16'),
(12, '2021-01-16', '2021-01-17');
该表表示床位的预订列表,并带有签入日期和退房日期。
要求检索每周应该设置多少个房间。为了减少维护费用,需要最大程度地减少提供的房间数量。此外,始终可以在结帐日期的同一天提供房间。例如,如果在 2021-01-08 上检查了一个房间,并且在同一天检查了另一个房间,则需要用于 2021-01的房间计数-08 只有1 ,而不是2 。
结果应为以下内容:
nek | number_of_rooms |
---|---|
1 | 3 |
2 | 2 |
我的方法是:
WITH reservations_with_expanded_dates AS (
SELECT
reservation_id,
Generate_series(check_in_date::DATE,
check_out_date::DATE,
'1day')::DATE AS stay
FROM
reservations
)
SELECT
week,
max(number_of_rooms)
FROM (
SELECT
Date_part('week', stay) AS week,
stay,
count(*) AS number_of_rooms
FROM
reservations_with_expanded_dates
GROUP BY
stay) AS reservations_per_day
GROUP BY
week
给出以下结果:
nek | number_of_rooms |
---|---|
1 | 4 |
2 | 3 |
结果不正确,因为我的方法没有考虑到房间的事实始终可以在结帐日期的同一天提供。如何提高查询以考虑到这一点?
此外,在绩效方面有更好的方法可以实现我要做的事情吗?
Given the following table created in a PostgreSQL database using the following SQL statements:
CREATE TABLE reservations (
reservation_id INT PRIMARY KEY,
check_in_date DATE NOT NULL,
check_out_date DATE NOT NULL
);
INSERT INTO reservations VALUES
(1, '2021-01-04', '2021-01-05'),
(2, '2021-01-05', '2021-01-07'),
(3, '2021-01-06', '2021-01-07'),
(4, '2021-01-06', '2021-01-08'),
(5, '2021-01-08', '2021-01-09'),
(6, '2021-01-08', '2021-01-09'),
(7, '2021-01-08', '2021-01-10'),
(8, '2021-01-13', '2021-01-17'),
(9, '2021-01-13', '2021-01-14'),
(10, '2021-01-14', '2021-01-15'),
(11, '2021-01-15', '2021-01-16'),
(12, '2021-01-16', '2021-01-17');
The table represents the list of reservations of a Bed&Breakfast with check-in date and check-out date.
It is requested to retrieve how many rooms should be set up and available for each week. In order to reduce maintenance expenses, it is required to minimise the number of offered rooms. Additionally a ,room can always be made available the same day as the check-out date. For example, if a room is checked out on the 2021-01-08 and another one is checked in on the same day, the count of rooms that needs to available for the 2021-01-08 will be only 1, and not 2.
The result should be the following:
week | number_of_rooms |
---|---|
1 | 3 |
2 | 2 |
My approach is the following:
WITH reservations_with_expanded_dates AS (
SELECT
reservation_id,
Generate_series(check_in_date::DATE,
check_out_date::DATE,
'1day')::DATE AS stay
FROM
reservations
)
SELECT
week,
max(number_of_rooms)
FROM (
SELECT
Date_part('week', stay) AS week,
stay,
count(*) AS number_of_rooms
FROM
reservations_with_expanded_dates
GROUP BY
stay) AS reservations_per_day
GROUP BY
week
Which gives the following result:
week | number_of_rooms |
---|---|
1 | 4 |
2 | 3 |
The result is not correct because my approach doesn't take into account the fact that a room can always be made available the same day as the check-out date. How can I improve my query to take this into account?
Moreover, is there a better way in terms of performance to achieve what I am trying to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以做:
结果:
请参见
You can do:
Result:
See running example at db<>fiddle.
这样的事情会起作用,它每天都会向您显示您需要多少个房间以及房间是什么房间。这只是一个可以通过延迟检查,不同类型的房间等进行扩展
。
示例
的 您需要多少个房间:
Something like this would work, it shows you per day how many rooms you need and for what reservation the room is. It's just an example that could be extended with late-check-out, different types of rooms, etc. etc.
A late checkout will change the number of rooms needed:
Unless somebody else checks in late:
And you check for 18:00 hours how many rooms you need: