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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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 ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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

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
RIASAuthor Commented:
Thanks Ryan!
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

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

Mark WillsTopic AdvisorCommented:
*laughing* I am going to stop attempting to answer your questions... I am simply too slow to type :)
RIASAuthor Commented:
Aw mark , thanks but , can make your solution assisted .
RIASAuthor Commented:
Can i ask Moderator to open the question as i need to mark yours as assisted .
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
RIASAuthor Commented:
Done mark, Better luck next time ;>
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.