IIM
Practically a Master Poster
638 posts since Jun 2011
Reputation Points: 127
Solved Threads: 136
Skill Endorsements: 7
adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11
Why would you join and then include the same table in a (NOT) IN statement ? This is just a huge waste of resources.
SELECT Facility.[Facility Name] FROM Facility
LEFT OUTER JOIN Reservation ON (Facility.FacilityID = [Reservation].FacilityID
AND [Check-in time] = '" & cboHour.Text & "'
AND [Check-in date] = '" & lstDate.SelectedItem.ToString & "')
WHERE ([Reservation].ReservationID Is NULL or [Reservation].CheckedIn = 2 )
AND Facility.[Facility Type] = '" & lstType.SelectedItem.ToString & "'
AND [Reservation].[Check-in time] BETWEEN DATEADD(hour, [Reservation].Duration, '" & cboHour.Text & "' ) AND '" & cboHour.Text & "')
If the rest of the query works, this should cover your cancellation needs, but again I think that you are not checking or the possible variations with time. I believe it should be - like the post I've linked to - something like :
select f.[Facility Name] from Facility f
left join
(Select FacilityId, CheckedIn
from Reservation
where [Check-In Date] = @CheckInDate
and ([Check-in Time] <= @CheckInTime and [Check-out Time] >= @CheckInTime
or [Check-In Time] <= @CheckOutTime and [Check-out Time] >= @CheckOutTime
or [Check-In Time] <= @CheckInTime and [Check-out Time] >= @CheckOutTime
or [Check-In Time] between @CheckInTime and @CheckOutTime
or [Check-out Time] between @CheckInTime and @CheckOutTime)
) R
on f.FacilityId = r.FacilityID
where (r.ReservationID IS Null or R.CheckedIn = 2 )
and f.[Facility Type] = @FacilityType
What this second query should do is check if there is a reservation in the specific date, where it starts before your desired checkin time and finishes during your desired period
or starts after your desired period has started and finishes after your period has ended
or starts before your period and ends after it
or is during your period. I'm using variables to keep everything simple, and I'm using a derived table instead of the actual table, in order to reduce the number of records returned and the resources used.
Please note that I haven't tested any of these 2, so they might contain errors.
adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11
For the second query, is it I have to pass in the value via parameters?
No, but is makes it easier to keep track of things when you use parameters, so I use them to give you an understanding of what's going on.
adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11