Sql query with enhancement for where clause

Here is the query which works but need few tweaks

DECLARE @EndDate AS DATE = '20180219';
DECLARE @StartDate AS DATE = '20180212';

SELECT GuestName ,
       SentTo AS Property ,
       ArrivalDate ,
       ISNULL(Extendedto, DepartureDate) AS DepartureDate ,
       Roomno ,
       RoomTypeRouting AS RoomType ,
       ArrivalDate2 AS ArrivalDate2 ,
       ISNULL(Extendedto2, DepartureDate2) AS DepartureDate2 ,
       [Roomno2] ,
       [RoomTypeRouting2] ,
       ArrivalDate3 AS ArrivalDate3 ,
       ISNULL(Extendedto3, DepartureDate3) AS DepartureDate3 ,
       [Roomno3] ,
       [RoomTypeRouting3] ,
       ArrivalDate4 AS ArrivalDate4 ,
       ISNULL(Extendedto4, DepartureDate4) AS DepartureDate4 ,
       [Roomno4] ,
       [RoomTypeRouting4]
FROM   CORR
WHERE  (   DeleteRecord IS NULL
           OR DeleteRecord = 0 )
       AND ArrivalDate >= '2016-01-01'
       AND ArrivalDate <= @EndDate
       AND ISNULL(ExtendedTo, DepartureDate) >= @StartDate;

Open in new window



WHERE  (   DeleteRecord IS NULL
           OR DeleteRecord = 0 )
       AND ArrivalDate2 >= '2016-01-01'
       AND ArrivalDate2 <= @EndDate
       AND ISNULL(ExtendedTo2, DepartureDate2) >= @StartDate;

WHERE  (   DeleteRecord IS NULL
           OR DeleteRecord = 0 )
       AND ArrivalDate3 >= '2016-01-01'
       AND ArrivalDate3 <= @EndDate
       AND ISNULL(ExtendedTo3, DepartureDate3) >= @StartDate;

Open in new window



same for ArrivalDate4 , ExtendedTo4, DepartureDate4

The query needs to find results on these as well and not only

WHERE  (   DeleteRecord IS NULL
           OR DeleteRecord = 0 )
       AND ArrivalDate >= '2016-01-01'
       AND ArrivalDate <= @EndDate
       AND ISNULL(ExtendedTo, DepartureDate) >= @StartDate;

Cheers
RIASAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
combined them all with OR conditions. like:

DECLARE @EndDate AS DATE = '20180219';
DECLARE @StartDate AS DATE = '20180212';

SELECT GuestName ,
       SentTo AS Property ,
       ArrivalDate ,
       ISNULL(Extendedto, DepartureDate) AS DepartureDate ,
       Roomno ,
       RoomTypeRouting AS RoomType ,
       ArrivalDate2 AS ArrivalDate2 ,
       ISNULL(Extendedto2, DepartureDate2) AS DepartureDate2 ,
       [Roomno2] ,
       [RoomTypeRouting2] ,
       ArrivalDate3 AS ArrivalDate3 ,
       ISNULL(Extendedto3, DepartureDate3) AS DepartureDate3 ,
       [Roomno3] ,
       [RoomTypeRouting3] ,
       ArrivalDate4 AS ArrivalDate4 ,
       ISNULL(Extendedto4, DepartureDate4) AS DepartureDate4 ,
       [Roomno4] ,
       [RoomTypeRouting4]
FROM   CORR
WHERE  (   DeleteRecord IS NULL
           OR DeleteRecord = 0 )
       AND 
	   (
		   (
				ArrivalDate >= '2016-01-01'
				AND ArrivalDate <= @EndDate
				AND ISNULL(ExtendedTo, DepartureDate) >= @StartDate
			) OR
			(
				ArrivalDate2 >= '2016-01-01'
				AND ArrivalDate2 <= @EndDate
				AND ISNULL(ExtendedTo2, DepartureDate2) >= @StartDate
			) OR
			(
				ArrivalDate3 >= '2016-01-01'
				AND ArrivalDate3 <= @EndDate
				AND ISNULL(ExtendedTo3, DepartureDate3) >= @StartDate
			)
		)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RIASAuthor Commented:
Thanks Ryan, will try and brb
0
RIASAuthor Commented:
Thanks Ryan!
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Mark WillsTopic AdvisorCommented:
Use between for your dates, and use  isnull(deleterecord,0) = 0
WHERE  isnull(deleterecord,0) = 0
AND 
 (
  (ArrivalDate between '2016-01-01' AND  @EndDate
   AND ISNULL(ExtendedTo, DepartureDate) >= @StartDate)
  OR
  (ArrivalDate2 between '2016-01-01' AND  @EndDate
   AND ISNULL(ExtendedTo2, DepartureDate2) >= @StartDate)
  OR
  (ArrivalDate3 between '2016-01-01' AND  @EndDate
   AND ISNULL(ExtendedTo3, DepartureDate3) >= @StartDate)
  OR
  (ArrivalDate4 between '2016-01-01' AND  @EndDate
   AND ISNULL(ExtendedTo4, DepartureDate4) >= @StartDate)
  )

Open in new window

1
Mark WillsTopic AdvisorCommented:
*laughing* I am going to stop attempting to answer your questions... I am simply too slow to type :)
0
RIASAuthor Commented:
Aw mark , thanks but , can make your solution assisted .
0
RIASAuthor Commented:
Can i ask Moderator to open the question as i need to mark yours as assisted .
0
Mark WillsTopic AdvisorCommented:
RIAS,

That would be very generous, but probably not different enough to warrant.

Besides, I think this is the 3rd or 4th time and I am getting use to it with you *laughing*.

You know what you want, and can adapt, so the first to supply gets the nod.

Completely understandable - and thanks for considering :)

Maybe a thumbs up if you feel it warrants a mention....

Cheers,
Mark
1
RIASAuthor Commented:
Done mark, Better luck next time ;>
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.