我正在使用SQL Server 2014,並且我有兩個分別運行的查詢,現在我需要連接這些查詢,以便擁有一個輸出。如何將這2個SQL查詢合併爲一個SQL查詢
查詢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月4日之間逗留,ReservationStayDate表格每晚將顯示4行(即:2014-10-31,2014-11-01,2014-11-02和2014-11-03)。
當我運行第一個查詢時,我的輸出對於每個來賓都是一行。現在,我需要爲2個月以上(如上例所示)夜間出現的客人輸出我的輸出,其中添加的列名稱爲「MTH」(實際上顯示月份和年份)和「已用夜間」。因此,輸出將看起來像這樣的客人誰花所有的夜晚在同一個月,併爲那些誰超過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
我希望我做了它一個更清晰一點了解。如果沒有,請評論,我會添加更多的具體信息。
表結構?預期結果?等等...更具體的你是在你的問題,你有更多的機會得到適當的幫助。 – bksi 2014-11-21 08:13:57
創建SQL小提琴http://www.sqlfiddle.com/。 – 2014-11-21 08:17:33
首先:您的第二個查詢沒有意義。您通過reservationstayid進行分組。所以'count(reservationstayid)'總是1,而不是「度過的夜晚」。而且,reservationstayid可能是桌子的關鍵。所以'avg(RateAmount)'總是一個記錄的平均值,因此avg(RateAmount)= RateAmount。你的第一個查詢是關於你的外連接的。你外連接'P5RESERVATIONLIST d',但是你的where子句中有'd.rsl_primaryguest ='+'',因此刪除了任何外連接的記錄(因爲它的rsl_primaryguest將爲null)。 – 2014-11-21 08:30:40