2014-11-21 172 views
0

我正在使用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 

我希望我做了它一個更清晰一點了解。如果沒有,請評論,我會添加更多的具體信息。

+2

表結構?預期結果?等等...更具體的你是在你的問題,你有更多的機會得到適當的幫助。 – bksi 2014-11-21 08:13:57

+0

創建SQL小提琴http://www.sqlfiddle.com/。 – 2014-11-21 08:17:33

+2

首先:您的第二個查詢沒有意義。您通過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

回答

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子句。

+0

我編輯了我的問題,並提供了更多信息。謝謝。 – user3115933 2014-11-21 10:31:47

+0

好的,我已經編輯了我的答案,所以派生表查詢現在應該做你想做的事情。等待。我將不得不作出另一項改變... – 2014-11-21 10:41:25

+0

完成。我不得不添加DateOfArrival和DateOfDeparture,你也希望在結果中看到它。 – 2014-11-21 10:48:43

-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 
+0

不起作用。我收到此消息: 「Msg 156,Level 15,State 1,Line 44 關鍵字'from'附近的語法不正確 Msg 156,Level 15,State 1,Line 63 'AS'關鍵字附近的語法不正確。 「 – user3115933 2014-11-21 10:11:05

+0

刪除逗號(,)後「AS'率金額Excl.VAT'」聲明 – Dhaval 2014-11-21 10:25:52

+0

檢查我編輯答案... – Dhaval 2014-11-21 10:26:42