0
SELECT ID, Name
FROM dbo.AmenitiesList 
WHERE TypeID=@Type and Status = 'Available'
    AND ID NOT IN
    (SELECT AmenitiesID FROM dbo.ReservationList
     WHERE Status = 'Cancelled' AND StartDate between @Arrival and @Departure
     or EndDate between @Arrival and @Departure
     or @Arrival between StartDate and EndDate
     or @Departure between StartDate and EndDate)

This is my query, I want to display all the Available Amenities that the status is 'Cancelled' and the Arrival Date and Departure date is not between in the ArrivalDate and Departure date in Database. But when retrieving data, I didn't get the Amenities available because when the Status is cancelled it triggers the other condition about those in between dates. How to Avoid that?

I want to display Amenities that is Cancelled and also not between ArrivalDate and Departure Date

Thank you in advance guys!

1
  • all that date logic could be simplified down to (@Arrival <= EndDate) AND (@Departure >= StartDate) Commented Mar 4, 2013 at 16:33

3 Answers 3

1

For performance and optimization, consider table variables.

DECLARE @TABLE AS TABLE
(
  AmenityID INT PRIMARY KEY CLUSTERED
)
INSERT INTO @TABLE
SELECT AmenitiesID FROM dbo.ReservationList
--Your conditions

--Get values that exist
SELECT * FROM AmentitiesList al
INNER JOIN @Table t on al.AmenityID = t.AmenityID

--Get values that don't
SELECT * FROM AmentitiesList al
LEFT JOIN @Table t on al.AmenityID = t.AmenityID
WHERE t.AmenityID IS NULL

Just because code is shorter, doesn't mean it scales. Left Joins are also a pain...

You could easily get rid of that too by using a table variable.

DECLARE @AmenityList AS TABLE
(
  AmenityID INT PRIMARY KEY CLUSTERED
  Matched BIT DEFAULT(0)
)
INSERT INTO @AmenityList
Select AmenityID FROM AmentitiesList

UPDATE @AmenityList SET Matched = 1
FROM @AmenitityList al
INNER JOIN @Table t on t.AmentityID = al.AmentityID

SELECT * FROM @AmentityList WHERE Matched = 0
Sign up to request clarification or add additional context in comments.

4 Comments

I don't understand this :/ sorry @Luke i'm an amateur in SQL.
It's okay :) I have put some comments with the code, it's more about researching what you don't know in the above code. What I have posted makes SQL optimize things so much better, giving you better perforkmance and scalability in the future.
okay thank you i'll research more about SQL thank you again @Luke
What you are doing here is getting a list of ID based on your condition (Like in the NOT IN), you can then manipulate that list to do fast things such as joins NOT IN is usually a last resort in most cases :)
1

How about this version?

SELECT ID, Name
FROM dbo.AmenitiesList 
WHERE TypeID=@Type and Status = 'Available'
    AND ID NOT IN
    (SELECT AmenitiesID FROM dbo.ReservationList
     WHERE Status = 'Cancelled' AND (StartDate between @Arrival and @Departure
     or EndDate between @Arrival and @Departure
     or @Arrival between StartDate and EndDate
     or @Departure between StartDate and EndDate))

Comments

0
SELECT ID, Name
FROM dbo.AmenitiesList 
WHERE TypeID=@Type and Status = 'Available'
    AND ID NOT IN
    (SELECT AmenitiesID FROM dbo.ReservationList
 WHERE Status != 'Cancelled' AND Status != 'Check Out' AND  ((@Arrival <= EndDate) AND (@Departure >= StartDate)))

This solve my problem! By the way thank you guys for spending time giving information to me! God Bless!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.