Hotel Room Booking Statement - sql

Essentially i'm making a basic hotel booking system for my computing a-level coursework, and have hit a stumbling block (doesn't help having a migraine either!)
I'm in the process of checking if a room is available for then the user to book (the application is all run and used by staff by the way) but trying to get my head around how to do it.
At the moment I was thinking I could just search the current booking database for the room number and dates I want to book for and if a match came back that meant that certain room was booked. That's all fine and dandy, but how do I tell it the range of dates? (sorry bad question) I was thinking an pseudo code version of the sql statement on the lines of:
If 'check in date of booking already on the database' is before
'check in date of new booking' AND 'check out date of booking already
on the database' is after 'check in date of new booking' then room is
already booked.
Basically if it returns a record then I know that room is booked. But I just need to figure out how to use SQL to search for records based on my check in/out dates.
But that'll allow double bookings, agh it's driving me mad :/ Any ideas?

It may not seem totally obvious at first, but you can actually simplify the testing of a full or partial date overlap with just two conditions.
Let's say you've got BOOKING table with existing bookings and a new booking with check in date #NEW_CHECK_IN and check out date #NEW_CHECK_OUT. To find all bookings that overlap these dates (assuming the end points are inclusive) you just need this kind of where clause:
--Select overlapping bookings
-- ...
WHERE BOOKING.CHECK_IN <= #NEW_CHECK_OUT
AND BOOKING.CHECK_OUT >= #NEW_CHECK_IN
-- ...
I know it looks too simple, but try it out with the scenarios that drf illustrated (plus the three that were missed, (1) where the new booking is after the existing booking, (2) the new booking is strictly within the existing booking and (3) the new booking is equal to the existing booking). You'll see that this simple set of conditions covers all of these scenarios.

The pseudocode suggested only considers one possibility of conflict between an existing booking and a new booking. To illustrate, consider a room with one booking.
|======================|
Check-in date Check out date
Suppose we want to create a new booking, and have 4 potential new bookings.
|======================| Existing booking
|-----------------------------| New booking (Scenario 1)
|----------| New booking (Scenario 2)
|-------------| New booking (Scenario 3)
|---| New booking (Scenario 4)
Of these, only Scenario 4 does not overlap an existing booking; the others conflict with the existing booking. While your pseudocode addresses Scenario 1, it does not detect Scenarios 2 or 3 and will thus allow double-bookings.
Effectively, your pseudocode might look something like this.
Let E = booking already on the database
N = new booking,
CID = check-in date,
COD = check-out date
For a new booking N, N conflicts with an existing booking iff there exists a record E where:
(CID of E is between CID of N and COD of N), or
(COD of E is between CID of N and COD of N), or
(CID of N < CID of E and COD of N > COD of E)
In SQL, depending on your schema, the query might resemble something like this:
-- assume #new_cid is the new checkin date and #new_cod is the new checkout date
select count(*) from bookings
where
#new_cid between bookings.checkindate and bookings.checkoutdate or
#new_cod between bookings.checkindate and bookings.checkoutdate or
(#new_cid <= bookings.checkindate AND #new_cod > bookings.checkoutdate)

Related

Finding Conflicting Schedules with PostgreSQL Range Types

Problem
We have a table dealing with scheduling vacation for users and some business rules saying that certain groups of people can't all be out at the same time. The relevant pieces of the scheduling table look like
user_id (text)
out_date_range (daterange)
1
["2021-12-27", "2021-12-30")
2
["2021-12-24", "2021-12-30")
3
["2022-01-24", "2022-01-27")
1
["2022-01-18", "2022-01-23")
2
["2022-01-25", "2022-01-30")
The goal is to find out if a given user, x, can create a new vacation schedule with date_range = [A,B) given a list of users that cannot be out at the same time as x.
Example
For example, suppose user 1 wants to create a new vacation schedule with date_range ["2022-01-26", "2022-01-29") and we know users 1, 2, and 3 cannot all be out at the same time. Our query should indicate it's not possible to schedule this since users 2 and 3 are both already out 2022-01-26 (see point 2 in Additional Notes).
Additional Notes
The number of users in the groups that cannot all be out at the same time is dynamic, it will be passed in as a list of user_id's
The query only needs to indicate if scheduling is possible (true/false); if we can additionally indicate what date range causes conflicts that would be nice, but that's not a requirement.
We're using version 12.6 of PostgreSQL (so no multirange functionality)
(Naive) Attempted Solutions
Create an intersection aggregator and intersect schedules for all users in the "cannot be out together group". This would work (I think) if each user only had one schedule, but since the users have multiple schedules that won't overlap the intersection is always empty.
Try to cross join (one for each user in the group) to generate the possible date ranges that could conflict and then intersect on those. I'm confused here both because the number of cross joins would be dynamic and even if we end up with the correct tuples not sure how the intersection would really work.
With PostgreSQL v14, you could run the following query to find out if a new entry would conflict with previous ones:
SELECT range_intersect_agg(mr)
FROM (SELECT range_agg(out_date_range) AS mr
FROM mytable
WHERE user_id <> '1' GROUP BY user_id) AS q
WHERE mr && '["2022-01-26", "2022-01-29")'::daterange;
This requires the new multirange data types.
First we build a multirange of the off time for each user other than user 1, then we see if there is any date in all of these multiranges by intersecting them. If the result is not empty, there is a conflict.
This cannot be done with a database constraint, so either use locking (SELECT ... FOR NO KEY UPDATE) or SERIALIZABLE transactions to prevent anomalies.

Hotel reservation Occupied Room Query Sql Server

Entity Room and Reservation
Room=Room_id,Rtype,Occupied(Bit column)
Reservation =Res_id,Room_id,CheckinDate,CheckoutDate.
How to update Occupied bit column to 1 when a room is reserved in Reservations table?I think its a trigger.but how?code snippet as I am new to this.
Someone suggested using check out date as Null.If null then update Occupied to 1.
HElp!!
The problem with using a static placement field like the Occupied one you have is that the value will change depending on the date you are reviewing. For example: Room A is unoccupied now, but is reserved two days from now for a 3 night stay, if you change the Occupied field to 1 then it will incorrectly show up as Occupied if someone tries to check-in today.
You will also have the problem of changing the Occupied field back to 0 when the room isn't in use - this is creating more work than necessary.
As mentioned in the comments it would be best to not have the Occupied field at all and instead create queries that will show when a room is available or not. The query can really vary depending on what you want, below is a sample query that creates a stored procedure that can be used to find available rooms during a date range:
CREATE PROCEDURE CheckRoomAvailability
#NewCheckInDate DATETIME
#NewCheckOutDate DATETIME
AS
SELECT r.Room_ID, r.Rtype
FROM room AS r
INNER JOIN reservation as re
ON r.room_id = re.room_id
WHERE
(#NewCheckINDate < re.CheckinDate
OR
#NewCheckInDate > re.CheckoutDate)
AND
(#NewCheckOutDate < re.CheckinDate
OR
#NewCheckOutDate > re.CheckoutDate)
GO
Then to execute this stored procedure:
EXEC CheckRoomAvailability([Check-In Date],[Check-Out Date])
Just enter the desired dates into the statement.
select checkoutdate from Reservation;
Put checkoutdate on a variable.
if checkoutdate == null then
update Room set occupied = 1;

Avoiding clashing appointments Access

I have three tables:
Employee(Id,name etc)
Appointment(Id,date,time,employee id, clientid etc)
Client(Id,name etc)
The Employee table has a 1 to many with the Appointment table as does the Client table.
However what I'm trying to achieve is to allow for the system to prevent duplicate or conflicting appointments but cant quite get my head around how to go about this. Would I need an additional table with say available time slots and some how link it all together? Or for example an employee availability table in addition?
Or could I achieve what I need with what I already have and just by manipulation of queries?
Many thanks
I think an appointments table is going to be necessary. It will allow you to include only available slots and also to analyse employees workload and availability. The table would include 15 minute slots for each day for each employee. You may wish to add a further table for holidays / sick days / generally unavailable for a chunk of time.
EDIT
I had envisioned something on the lines of:
Timeslots:
EmployeeID ) Primary key
TimeSlot )
JobID - Foreign key
Status ) And so forth
Notes )
"I want an early appointment with E1"
SELECT TimeSlots.EmployeeID, TimeSlots.TimeSlot, TimeSlots.JobID
FROM TimeSlots
WHERE TimeSlots.EmployeeID=1
AND TimeSlots.TimeSlot Between #2/9/2012 9:0:0# And #2/9/2012 11:30:0#
"I want an appointment at 9:00am"
SELECT TimeSlots.EmployeeID, TimeSlots.TimeSlot, TimeSlots.JobID
FROM TimeSlots
WHERE TimeSlots.TimeSlot Between #2/9/2012 9:0:0# And #2/9/2012 9:30:0#
To prevent collisions, the logic here is quite simple:
A collision occurs when:
RequestStartDate <= EndDate
and
RequestEndDate >= StartDate
The above is thus a rather simply query. If any collision occurs, the
above will return records and you simply don't allow the booking.
The above of course can EASY be extended for time, and even a particular room.
Eg:
RequestStartTime <= EndTime
and
RequestEndTime >= StartTime
And
RequestDate = BookingDate
And in fact in access since you can store a date + time column, then we are quite much back to the first example above (and as such a booking can span multiple days if you do this). And as noted, if this was for a particular room, then just add a room condition to the above.
Access 2010 does have table triggers and store procedures, but since you need a UI for the user, then code like this normally does the trick:
dim strWhere as string
dim dtRequeestStartDate as date
dim dtRequestEndDate as date
dtRequestStartDate = inputbox("Enter start Date")
dtRequestEndDate = inputbox("Enter end date")
strWhere="#" & format(dtRequestStartDate,"mm/dd/yyyy") & "# <= EndDate" & _
" and #" & format(dtRequestEndDate,"mm/dd/yyyy") & "# >= StartDate"
if dcount("*","tableBooking",strWhere) > 0 then
msgbox "sorry, you cant book
...bla bla bla....
The above is just an example, and I am sure you would build a nice form that
prompts the user for the booking dates.
So the above simple conditions above does return ANY collisions. And having written so many reservation systmes, I strongly recommend you do NOT create blank records ahead of time, but use the above correct logic and thus ONLY add records to the booking system and not have to write tons of code to create a bunch of blank records with time slots etc.
The above simply query will prevent collisions.

Splitting one table based on criteria and comparing

I'm not quite sure on the best way to phrase this particular query, so I hope the title is adequate, however, I will attempt to describe what it is I need to be able to understand how to do. Just to clarify, this is for oracle sql.
We have a table called assessments. There are different kinds of assessments within this table, however, some assessments should follow others in a logical order and within set time frames. The problems come in when a client has multiple assessments of the same type, as we have to use a fairly inefficient array formula in excel to identify which 'full' assessment corresponds with the 'initial' assessment.
I have an earlier query that was resolved on this site (Returning relevant date from multiple tables including additional table info) which I believe includes a lot of the logic for what is required (particularly in identifying a corresponding event which has occurred within a specified timeframe). However, whilst that query pulls data from 3 seperate tables (assessments, events, responsiblities), I now need to create a query that generates a similar outcome but pulling from 1 main table and a 2nd table to return worker information. I thought the most logical way would be be to create a query that looks at the assessment table with one type of assessment, and then joins to the assessment table again (possibly a temporary table?) with assessment type that would follow the initial one.
For example:
Table 1 (Assessments):
Client ID Assessment Type Start End
P1 1 Initial 01/01/2012 05/01/2012
Table 2 (Assessments temp?):
Client ID Assessment Type Start End
P1 2 Full 12/01/2012
Table 3:
ID Worker Team
1 Bob Team1
2 Lyn Team2
Result:
Client ID Initial Start Initial End Initial Worker Full Start Full End
P1 1 01/01/2012 05/01/2012 Bob 12/01/2012
So table 1 and table 2 draw from the same table, except it's bringing back different assessments. Ideally, there'd be a check to make sure that the 'full' assessment started within X days of the end of the 'initial' assessment (similar to the 'likely' check in the previous query mentioned earlier). If this can be achieved, it's probably worth mentioning that I'd also be interested in expanding this to look at multiple assessment types, as roughly in the cycle a client could be expected to have between 4 or 5 different types of assessment. Any pointers would be appreciated, I've already had a great deal of help from this community which is very valuable.
Edit:
Edited to include solution following MBs advice.
Select
*
From(
Select
I.ASM_SUBJECT_ID as PNo,
I.ASM_ID As IAID,
I.ASM_QSA_ID as IAType,
I.ASM_START_DATE as IAStart,
I.ASM_END_DATE as IAEnd,
nvl(olm_bo.get_ref_desc(I.ASM_OUTCOME,'ASM_OUTCOME'),'') as IAOutcome,
C.ASM_ID as CAID,
C.ASM_QSA_ID as CAType,
C.ASM_START_DATE as CAStart,
C.ASM_END_DATE as CAEnd,
nvl(olm_bo.get_ref_desc(C.ASM_OUTCOME,'ASM_OUTCOME'),'') as CAOutcome,
ROUND(C.ASM_START_DATE -I.ASM_START_DATE,0) as "Likely",
row_number() over(PARTITION BY I.ASM_ID
ORDER BY
abs(I.ASM_START_DATE - C.ASM_START_DATE))as "Row Number"
FROM
O_ASSESSMENTS I
left join O_ASSESSMENTS C
on I.ASM_SUBJECT_ID = C.ASM_SUBJECT_ID
and C.ASM_QSA_ID IN ('AA523','AA1326') and
ROUND(C.ASM_START_DATE - I.ASM_START_DATE,0) >= -2
AND
ROUND(C.ASM_START_DATE - I.ASM_START_DATE,0) <= 25
and C.ASM_OUTCOME <>'ABANDON'
Where I.ASM_QSA_ID IN ('AA501','AA1323')
AND I.ASM_OUTCOME <> 'ABANDON'
AND
I.ASM_END_DATE >= '01-04-2011') WHERE "Row Number" = 1
You can access the same table multiple times in a given query in SQL, simply by using table aliases. So one way of doing this would be:
select i.client,
i.id initial_id,
i.start initial_start,
i.end initial_end,
w.worker initial_worker,
f.id full_id,
f.start full_start,
f.end full_end
from assessments i
join workers w on i.id = w.id
left join assessments f
on i.client = f.client and
f.assessment_type = 'Full' and
f.start between i.end and i.end + X
/* replace X with appropriate number of days */
where i.assessment_type = 'Initial'
Note: column names such as end (that are reserved words in Oracle SQL) should normally be double-quoted, but from the previous question it looks as though these are simplified versions of the actual column names.
From your post, I assume that you're using Oracle here (as I see "Oracle" in the question).
In terms of "temp" tables, Views come right to mind. An Oracle View can give you different looks of a table which is what it sounds like you're looking for with different kinds of assessments.
Don Burleson is a good source for anything Oracle related and he gives some tips on Oracle Views at http://www.dba-oracle.com/concepts/views.htm

SQL Scheduling - Overbooked Report

I need a way to view a given resource (in this case rooms/beds) that are overbooked. Here's my table structure. Sorry about the Hungarian notation:
tblRoom
--RoomID
tblBooking
--BookingID
--BeginDate
--EndDate
--AssignedRoomID (foreign key)
I don't have any non-working SQL to post here because I really don't know where to start. I'm using MS Access but I'm looking for a database agnostic solution if possible. It's OK to have to have to change some of the keywords to match the dialect of a given SQL engine but I'd like avoid using other features that are proprietary or only available in one RDBMS.
I realize that it's best to avoid overbooking from the beginning but that's not the point of this question.
In case it's helpful, I posted a related question a couple days ago about how to find resources that are not yet booked for a given data range. You can see that question here.
Edit1:
In reply to the answer below, I've modified your SQL slightly to make it work in Access as well as to be more accurate when it comes to detecting conflicts. If I err not your solution posted below allows some conflicts to go unnoticed but also shows conflicts when a given Booking's EndDate and a different Booking's BeginDate fall on the same day, which is actually allowable and should not show as a conflict. Am I understanding this correctly or am I missing something here?
SELECT
*
FROM
tblBooking AS booking
INNER JOIN
tblBooking AS conflict
ON [conflict].AssignedRoomID = [booking].AssignedRoomID
AND (([conflict].BeginDate >= DateAdd("d", -1, [booking].BeginDate) AND [conflict].BeginDate < [booking].EndDate)
OR ([conflict].EndDate > [booking].BeginDate AND [conflict].EndDate < [booking].EndDate))
AND [conflict].BookingID <> [booking].BookingID
So, what you're looking for is any record in tblBooking for which there is another record with the same AssignRoomID for an overlapping period?
A naive solution would be...
SELECT
*
FROM
tblBooking [booking]
INNER JOIN
tblBooking [conflict]
ON [conflict].AssignedRoomID = [booking].AssignedRoomID
AND [conflict].BeginDate <= [booking].EndDate
AND [conflict].EndDate >= [booking].BeginDate
AND [conflict].BookingID != [booking].BookingID
The last condition stops a booking from being it's own conflict. It can also be changed to AND [conflict].BookingID > [booking].BookingID so that you don't get the conflict repeating. (If A conflicts with B, you only get A,B and not B,A.)
EDIT
The issue with the above solution is that it does not scale very well. When searching for a Conflict, all bookings for that room Before the booking's EndDate are found, then filtered based on the EndDate. After a few years, that first search (hopefully using an Index) will return many, many records.
One optimisation is to have a maximum booking length, and only look that many days back in time for a conflict...
INNER JOIN
tblBooking [conflict]
ON [conflict].AssignedRoomID = [booking].AssignedRoomID
AND [conflict].BeginDate <= [booking].EndDate
AND [conflict].BeginDate >= [booking].BeginDate - 7 -- Or however long the max booking length is
AND [conflict].EndDate >= [booking].BeginDate
AND [conflict].BookingID != [booking].BookingID
By having wrapped a >= AND a <= around the [conflict].BeginDate, an index search can now quickly return a reasonably limitted number of records.
For bookings longer than the maximum booking length, they can be entered into the database as multiple bookings. That's where the art of optimisation comes in, it's often all about trade-offs and compromises :)
EDIT
Another option, giving different details, would be to join the bookings against a calendar table. (Having, for example, one record per day.)
SELECT
[room].RoomID,
[calendar].Date,
COUNT(*) AS [total_bookings],
MIN([booking].BookingID) AS [min_booking_id],
MAX([booking].BookingID) AS [max_booking_id]
FROM
[calendar]
CROSS JOIN
tblRoom [room]
INNER JOIN
tblBooking [booking]
ON [booking].AssignedRoomID = [room].RoomID
AND [booking].BeginDate <= [calendar].Date
AND [booking].EndDate >= [calendar].Date
GROUP BY
[room].RoomID,
[calendar].Date
HAVING
COUNT(*) > 1