Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 How to write this JOIN query with my CTE table?

Author  Topic 

zicitron
Starting Member

5 Posts

Posted - 2014-11-29 : 12:42:29
I have a table called dbo.Budget1415 and I need to extract data from it and join it with an existing CTE. However the JOIN should be done based on multiple conditions. I just can't figure out what I'm missing but the codes I've put down do not seem to work. Here is where I've reached so far with my query:



USE MyDatabase

WITH CTE
AS
(

SELECT
a.ReservationStayID,
c.Createdon,
a.FirstName + ' ' + a.LastName AS 'Name',
b.PropertyCode AS'Property',
b.ReservationStatus AS 'Status',
d.rsl_rateplan AS 'Rate Plan Code',
d.rsl_mealplan,
d.rsl_roomtype,
b.MarketSegmentCode AS 'Market Segment',
e.TravelAgencyTypeCode AS 'Source of Business',
a.ArrivalDate AS 'Date of Arrival',
a.DepartureDate AS 'Date of Departure',
(SELECT DATEDIFF (day,ArrivalDate,DepartureDate)
FROM ReservationStay c
WHERE c.ReservationStayID = a.ReservationStayID) AS 'Nights',
c.AdultCount AS 'Adult',
c.ChildCount AS 'Child',
b.GuestCount AS 'Guest',
d.rsl_reasonstay,
c.TaProfileID,
c.PMSConfirmationNumber,
c.CurrencyCode As 'Currency',
d.rsl_nationality AS 'Nationality',
d.rsl_country AS 'Country of Residence',
d.rsl_totalroomrate,
e.Name AS 'Tour Operator',
e.CountryCode AS 'Market Code',
g.CountryGroup AS 'Market',


(SELECT avg(RateAmount)/1.15
FROM ReservationStayDate f
where f.ReservationStayID = a.ReservationStayID) AS 'Rate Amount Excl.VAT',

c.TAProfileID2,
e2.Name AS 'Booking Origin (1)',
g2.CountryGroup AS 'Booking Origin (2)'



FROM GuestNameInfo a
JOIN GuestStaySummary b ON a.ReservationStayID = b.ReservationStayID
LEFT JOIN ReservationStay c ON c.ReservationStayID = b.ReservationStayID
LEFT JOIN P5RESERVATIONLIST d ON d.rsl_code = b.ReservationStayID
LEFT JOIN TravelAgency e ON e.TravelAgencyID = c.TAProfileID
LEFT JOIN Market g ON e.CountryCode = g.CountryCode
LEFT JOIN TravelAgency e2 ON e2.TravelAgencyID = c.TAProfileID2
LEFT JOIN Market g2 ON e2.CountryCode = g2.CountryCode

LEFT JOIN
(
SELECT
ReservationStayID,
datename(m,StayDate) + ' ' + cast(datepart(yyyy,StayDate) as varchar) as [MTH],
count(*) AS [Nights Spent],
avg(RateAmount) as [Rate],
min(CreatedOn) as CreatedOn,
min(StayDate) as [DateOfArrival],
max(StayDate) as [DateOfDeparture]
FROM ReservationStayDate
GROUP BY ReservationStayID, datename(m,StayDate) + ' ' + cast(datepart(yyyy,StayDate) as varchar)
) x ON x.ReservationStayID = b.ReservationStayID

WHERE a.PrimaryGuest='+' and d.rsl_primaryguest='+'
)

SELECT
m.*,
p.PkgRev,
p.GADR
FROM CTE AS m

INNER JOIN dbo.Budget1415 AS p ON m.PropertyCode = p.PropertyCode AND m.[MTH] = p.Mth AND m.countrygroup = p.Mkt

zicitron
Starting Member

5 Posts

Posted - 2014-11-29 : 13:51:57
Just an update. I have progressed a bit towards my goal with the following code. Also, I had semi-colon missing before the "WITH CTE".




SELECT
m.*,
p.PkgRev,
p.GADR
FROM CTE AS m

INNER JOIN dbo.Budget1415 AS p ON m.PropertyCode = p.PropertyCode AND m.[MTH] = p.Mth AND m.[Market] = p.Mkt



My problem is that it it seems I need a LEFT JOIN because with the codes above, the output is replicating itself on several rows for the same record. Basically, I should end up with around 16,000 but I'm getting over 100,000 records in my output. CTE accepts only INNER JOIN...so, where do I go from here?
Go to Top of Page
   

- Advertisement -