如何使用多个条件编写此SQL JOIN查询

时间:2014-11-27 11:08:39

标签: sql sql-server pivot

我有以下查询:

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='+'

我需要从名为dbo.Budget的表中添加一个JOIN语句,该表将从'PkgRevenue'和'GADR'列中提取值,并在上述查询的输出中的2个新列中添加这2个值。但是,应根据以下条件连接这两列的值:PropertyCode,Month和Market应匹配。

dbo.Budget具有以下结构:

PropertyCode    Month          Market    PkgRevenue      GADR
   xxx          November 2014   UK         1,251,000      3,256

1 个答案:

答案 0 :(得分:0)

您可以简单地将该查询括在子查询中,并将其与预算表连接,如下所示:

SELECT
  ... -- Here you can put whatever the columns you want to select
FROM
(
   -- your query here
) AS t1 
INNER JOIN Budget AS b ON ....

或者,以这样的方式使用CTE:

WITH CTE
AS
(
   -- your query here
)
SELECT 
  c.*,
  b.PkgRevenue,
  b.GADR
FROM CTE AS c
INNER JOIN dbo.Budget AS b  ON c.PropertyCode = b.PropertyCode
                           AND c.Month        = b.Month
                           AND c.Market       = c.Market;

如下:

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)',
       x.MTH    -- <======== You need to select the MTH column 
    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 
  c.*,
  b.PkgRevenue,
  b.GADR
FROM CTE AS c
INNER JOIN dbo.Budget AS b  ON c.PropertyCode = b.PropertyCode
                           AND c.MTH          = b.Month         -- <=== Here you put MTH
                           AND c.Market       = c.Market