如何将这两个SQL查询连接到一个SQL查询中

时间:2014-11-21 08:10:26

标签: sql sql-server join

我正在使用SQL Server 2014,我有2个查询,我单独运行,现在我需要加入这些查询,以便我有一个输出。

查询1表示如下:

USE MyDatabase

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


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

ORDER by a.ReservationStayID

第二个问题是:

use MyDatabase

select *

FROM (SELECT 
  ReservationStayID,count(reservationstayid) AS [Nights Spent],
  datename(m,StayDate)+' '+ cast(datepart(yyyy,StayDate) as varchar) as [MTH],
  avg(RateAmount) as [Rate],
  CreatedOn

from ReservationStayDate

GROUP BY ReservationStayID,STAYDATE,RATEAMOUNT,CreatedOn

) AS S

ORDER by ReservationstayID

这应该是INNER JOIN,OUTER JOIN还是完全不同的语法?

其他信息:

好的,更明确一点,这里有一些指示。 ReservationStayDate表按住宿日期(StayDate)列出住宿。因此,如果客人在2014-10-31至2014-11-04之间停留,则ReservationStayDate表将显示每晚花费的4行(即:2014-10-31,2014-11-01,2014-11-02和2014-11-03)。

当我运行第一个查询时,每个guest虚拟机的输出都是一行。现在我需要为超过2个月的夜晚(如上面的例子)出现在2行上的客人输出我的输出,添加的列名称为“MTH”(事实上显示月份和年份)和“Nights Spent”。因此,对于在同一个月内度过所有夜晚的客人以及重叠超过2个月的客户,输出将如下所示:

ReservationStayId     CreatedOn........ DateOfArrival  DateOfDeparture  MTH      Nights
     250                                 2014-09-05    2014-09-07    Sept 2014    2
     285                                 2014-10-31    2014-11-04    Oct 2014     1
     285                                 2014-10-31    2014-11-04    Nov 2014     3

我希望我能让它更清楚一点。如果没有,请发表评论,我会添加更多具体信息。

2 个答案:

答案 0 :(得分:0)

您始终可以使用派生表。这是您加入的查询,就像您加入表格一样。

SELECT 
  ...
  x.[Nights Spent],
  x.[MTH],
  x.[Rate],
  x.CreatedOn,
  x.[DateOfArrival],
  x.[DateOfDeparture]
FROM GuestNameInfo a
 JOIN GuestStaySummary b ON a.ReservationStayID = b.ReservationStayID
 LEFT JOIN ReservationStay c ON c.ReservationStayID = b.ReservationStayID
 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
 ...

正如您对请求的评论中已经提到的那样,您的第二个查询毫无意义编辑:我已根据您编辑的请求更改了第二个查询。

如上所述,关于外连接的第一个查询被打破。还有一个建议:使用可读的别名,而不是a,b,c,但可能是GuestNameInfo的gni,GuestStaySummary的gss等。这样,只要您想知道列属于哪个表,就不必查找from / join子句。

答案 1 :(得分:-1)

尝试这个希望它会起作用....

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',-------change to g.CountryName to obtain Original Markets
c.TAProfileID2,
e2.Name AS 'Booking Origin (1)',
g2.CountryGroup AS 'Booking Origin (2)'

,s.*,

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




 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
 inner join 
  (SELECT 
   ReservationStayID,count(reservationstayid) AS [Nights Spent],
  Convert(varchar,datename(m,StayDate)+' '+ cast(datepart(yyyy,StayDate) ) as [MTH],
   avg(RateAmount) as [Rate],
   CreatedOn

 from ReservationStayDate

 GROUP BY ReservationStayID,STAYDATE,RATEAMOUNT,CreatedOn

 ) AS S on s.ReservationStayID=a.ReservationStayID

 WHERE a.PrimaryGuest='+' and d.rsl_primaryguest='+'
 and  f.ReservationStayID = a.ReservationStayID) AS 'Rate Amount Excl.VAT',

 ORDER by ReservationstayID