Sql query amendenment with more fields

Hello,
I have a query which works perfectly,  but recently have added few more fields in the table.
Need to amend the query , any help is appreciated .

SELECT
      GuestName
    , format(ArrivalDate,'yyyy-MM-dd') ArrivalDate
    , format(CASE
                  WHEN Extendedto IS NULL THEN DepartureDate
                  ELSE Extendedto
           END,'yyyy-MM-dd') AS DepartureDate
                                   , SentTo AS Property, Roomno, RoomTypeRouting AS 'RoomType' 
FROM CORR
WHERE ( DeleteRecord is null or DeleteRecord = 0) 
AND ArrivalDate >= '2016-01-01'
AND ArrivalDate <= @EndDate
AND COALESCE(ExtendedTo,DepartureDate) >= @StartDate

Open in new window


New Columns are :

        [RoomTypeRouting2] [nvarchar](max) NULL,
      [RoomTypeRouting3] [nvarchar](max) NULL,
      [RoomTypeRouting4] [nvarchar](max) NULL,
      [ArrivalDate2] [date] NULL,
      [ArrivalDate3] [date] NULL,
      [ArrivalDate4] [date] NULL,
      [DepartureDate2] [date] NULL,
      [DepartureDate4] [date] NULL,
      [DepartureDate3] [date] NULL,
      [Extendedto2] [date] NULL,
      [Extendedto3] [date] NULL,
      [Extendedto4] [date] NULL,
      [Roomno2] [nvarchar](max) NULL,
      [Roomno3] [nvarchar](max) NULL,
      [Roomno4] [nvarchar](max) NULL


Regards
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.

Éric MoreauSenior .Net ConsultantCommented:
Add your new columns before the FROM clause (and don't forget comas):

SELECT
      GuestName
    , format(ArrivalDate,'yyyy-MM-dd') ArrivalDate
    , format(CASE
                  WHEN Extendedto IS NULL THEN DepartureDate
                  ELSE Extendedto
           END,'yyyy-MM-dd') AS DepartureDate
                                   , SentTo AS Property, Roomno, RoomTypeRouting AS 'RoomType' 
 ,       [RoomTypeRouting2] 
 ,     [RoomTypeRouting3] 
,      [RoomTypeRouting4] 
,      [ArrivalDate2] 
,      [ArrivalDate3] 
,      [ArrivalDate4] 
,      [DepartureDate2] 
,      [DepartureDate4] 
,      [DepartureDate3] 
,      [Extendedto2] 
,      [Extendedto3] 
,      [Extendedto4] 
,      [Roomno2] 
,      [Roomno3] 
,      [Roomno4] 
FROM CORR
WHERE ( DeleteRecord is null or DeleteRecord = 0) 
AND ArrivalDate >= '2016-01-01'
AND ArrivalDate <= @EndDate
AND COALESCE(ExtendedTo,DepartureDate) >= @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 Eric! Will try and brb
0
RIASAuthor Commented:
Thank you Sir! Worked like a charm.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

RIASAuthor Commented:
Hello Eric,
Are we not adding these to the query for new columns  :
 , format(ArrivalDate,'yyyy-MM-dd') ArrivalDate
    , format(CASE
                  WHEN Extendedto IS NULL THEN DepartureDate
                  ELSE Extendedto
           END,'yyyy-MM-dd') AS DepartureDate
                                   , SentTo AS Property, Roomno, RoomTypeRouting AS 'RoomType'

Thanks
0
RIASAuthor Commented:
Also Eric,

Display ' Roomno2' and 'RoomTypeRouting2 ' only when
AND ArrivalDate2 >= '2016-01-01'
AND ArrivalDate2 <= @EndDate
AND COALESCE(ExtendedTo2,DepartureDate2) >= @StartDate

Cheers
0
Éric MoreauSenior .Net ConsultantCommented:
replace:
,       [RoomTypeRouting2]
,      [Roomno2] 

Open in new window


With:
,  case when ArrivalDate2 >= '2016-01-01' AND ArrivalDate2 <= @EndDate AND ExtendedTo2,DepartureDate2 >= @StartDate then [RoomTypeRouting2]  else null end as RoomTypeRouting2
,  case when ArrivalDate2 >= '2016-01-01' AND ArrivalDate2 <= @EndDate AND ExtendedTo2,DepartureDate2 >= @StartDate then [Roomno2]  else null end as Roomno2

Open in new window

0
RIASAuthor Commented:
Thanks Eric,
Still the query is not complete , will post a new question related to this as its a new requirement.
0
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
Microsoft SQL Server 2008

From novice to tech pro — start learning today.