我正在使用SQL Server 2014,并且我当前查询的摘要如下所示。我的问题涉及我的查询的最后一个SELECT语句。现在它工作正常,输出如下:
ReservationStayID ........... XBEV01 XBEV02 XBEV03 XFOOD1 XFOOD2
215 ........... 1500 0 0 300 100
356 ........... 500 200 0 600 700
620 ........... 300 0 600 0 200
现在我创建了一个名为“Extras”的表,它看起来像这样:
Code Description RevenueType
XBEV01 Beverage Bar A Beverage Charge
XBEV01 Beverage Bar A Beverage Charge
XBEV01 Beverage Bar A Beverage Charge
XFOOD1 Food A Food Charge
XFOOD2 Food B Food Charge
我想修改我的查询,以便输出如下所示:
ReservationStayID ........... Beverage Charge Food Charge
215 ........... 1500 400
356 ........... 700 1300
620 ........... 900 200
我知道我需要使用“Extras”表的“Code”列与“P5FolioCharge”表的“FOC_TRANSACTIONCODE”列进行LEFT JOIN,但我无法弄清楚如何在我的查询。 我想我需要修改查询的最后一个SELECT语句中的SUM语句。
以下是我当前查询的摘录:
USE MyDatabase
SELECT
x.[ReservationStayID],
b.PropertyCode,
c.PMSConfirmationNumber,
j.FOH_PMSCONFIRMATIONNUMBER,
y.FOC_ACCOUNT,
.......
b.GuestCount AS 'Total Guest',
x.[Nights Spent] AS 'Room Nights',
x.[MTH],
x.[Rate] AS 'Room Rate WITH VAT',
c.CurrencyCode, --added
h.ROE AS 'Rate of Exchange',
(x.[Nights Spent]*x.[Rate]*h.[ROE])/NULLIF(1.15,0) AS 'PkgRevenue, Excl. VAT',
((x.[Nights Spent]*x.[Rate]*h.[ROE])/1.15)/NULLIF((b.GuestCount*x.[Nights Spent]),0) AS 'GADR, Excl. VAT',
x.CreatedOn,
x.[DateOfArrival],
x.[DateOfDeparture],
e.TravelAgencyTypeCode AS 'Source of Business',
c.TAProfileID,
e.Name AS 'Tour Operator',
g.CountryGroup AS 'Market',
c.TAProfileID2, --added
e2.Name AS 'Booking Origin (1)',
g2.CountryGroup AS 'Booking Origin (2)',
y.[NetAmount(XBEV01)],
y.[NetAmount(XBEV02)],
y.[NetAmount(XBEV03)],
y.[NetAmount(XFOOD1)],
y.[NetAmount(XFOOD2)]
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 --added
LEFT JOIN TravelAgency e2 ON e2.TravelAgencyID = c.TAProfileID2
LEFT JOIN Market g2 ON e2.CountryCode = g2.CountryCode --added
LEFT JOIN Exrate h ON h.Ccode = c.CurrencyCode -- added
LEFT JOIN RoomCat i ON b.PropertyCode = i.Property AND d.rsl_roomtype = i.RoomType
LEFT JOIN P5FOLIOHEADER j ON J.FOH_PMSCONFIRMATIONNUMBER = C.PMSConfirmationNumber
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
LEFT JOIN
(
SELECT
FOC_ACCOUNT,
datename(m,FOC_DATE) + ' ' + cast(datepart(yyyy,FOC_DATE) as varchar) as [FOCMTH],
SUM(case FOC_TRANSACTIONCODE when 'XBEV01' then foc_netamount else 0 end) as 'NetAmount(XBEV01)',
SUM(case FOC_TRANSACTIONCODE when 'XBEV02' then foc_netamount else 0 end) as 'NetAmount(XBEV02)',
SUM(case FOC_TRANSACTIONCODE when 'XBEV03' then foc_netamount else 0 end) as 'NetAmount(XBEV03)'
SUM(case FOC_TRANSACTIONCODE when 'XFOOD1' then foc_netamount else 0 end) as 'NetAmount(XFOOD1)',
SUM(case FOC_TRANSACTIONCODE when 'XFOOD2' then foc_netamount else 0 end) as 'NetAmount(XFOOD2)'
FROM P5FOLIOCHARGE
GROUP BY FOC_ACCOUNT, datename(m,FOC_DATE) + ' ' + cast(datepart(yyyy,FOC_DATE) as varchar)
) y ON y.FOC_ACCOUNT = j.FOH_ACCOUNT
更新: 这是我的Pivot查询(基于Merely Useful提出的解决方案)并且它有效。我创建了一个VIEW表(HOLDINGS3)并进行了一个数据透视查询以进入VIEW:
(SELECT ReservationStayID,
PropertyCode,
[Beverage Charge],
[Food Charge]
FROM (SELECT
ReservationStayID,
PropertyCode,
RevenueType,
FOC_NETAMOUNT
FROM HOLDINGS3
)ab
PIVOT (SUM(foc_netamount)
FOR RevenueType IN ([Beverage Charge], [Food Charge]
)) as pvt)
答案 0 :(得分:0)
我们走了。你的查询很长,所以这可能不起作用,但希望它会给你一个想法。这一部分阻碍了你:
SELECT
FOC_ACCOUNT,
datename(m,FOC_DATE) + ' ' + cast(datepart(yyyy,FOC_DATE) as varchar) as [FOCMTH],
SUM(case FOC_TRANSACTIONCODE when 'XBEV01' then foc_netamount else 0 end) as 'NetAmount(XBEV01)',
SUM(case FOC_TRANSACTIONCODE when 'XBEV02' then foc_netamount else 0 end) as 'NetAmount(XBEV02)',
SUM(case FOC_TRANSACTIONCODE when 'XBEV03' then foc_netamount else 0 end) as 'NetAmount(XBEV03)'
SUM(case FOC_TRANSACTIONCODE when 'XFOOD1' then foc_netamount else 0 end) as 'NetAmount(XFOOD1)',
SUM(case FOC_TRANSACTIONCODE when 'XFOOD2' then foc_netamount else 0 end) as 'NetAmount(XFOOD2)'
你最好离开桌子,离开你的新额外表,然后转出你需要的列。
USE MyDatabase
SELECT <<ALL THE DESCRIPTIVE COLUMNS YOU NEED>>,
[Beverage Charge],
[Food Charge]
FROM
(
SELECT
x.[ReservationStayID],
b.PropertyCode,
c.PMSConfirmationNumber,
j.FOH_PMSCONFIRMATIONNUMBER,
y.FOC_ACCOUNT,
.......
b.GuestCount AS 'Total Guest',
x.[Nights Spent] AS 'Room Nights',
x.[MTH],
x.[Rate] AS 'Room Rate WITH VAT',
c.CurrencyCode, --added
h.ROE AS 'Rate of Exchange',
(x.[Nights Spent]*x.[Rate]*h.[ROE])/NULLIF(1.15,0) AS 'PkgRevenue, Excl. VAT',
((x.[Nights Spent]*x.[Rate]*h.[ROE])/1.15)/NULLIF((b.GuestCount*x.[Nights Spent]),0) AS 'GADR, Excl. VAT',
x.CreatedOn,
x.[DateOfArrival],
x.[DateOfDeparture],
e.TravelAgencyTypeCode AS 'Source of Business',
c.TAProfileID,
e.Name AS 'Tour Operator',
g.CountryGroup AS 'Market',
c.TAProfileID2, --added
e2.Name AS 'Booking Origin (1)',
g2.CountryGroup AS 'Booking Origin (2)',
RevenueType,
foc_netamount
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 --added
LEFT JOIN TravelAgency e2 ON e2.TravelAgencyID = c.TAProfileID2
LEFT JOIN Market g2 ON e2.CountryCode = g2.CountryCode --added
LEFT JOIN Exrate h ON h.Ccode = c.CurrencyCode -- added
LEFT JOIN RoomCat i ON b.PropertyCode = i.Property AND d.rsl_roomtype = i.RoomType
LEFT JOIN P5FOLIOHEADER j ON J.FOH_PMSCONFIRMATIONNUMBER = C.PMSConfirmationNumber
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
LEFT JOIN PSFOLIOCHARGE
ON PSFOLIOCHARGE.FOC_ACCOUNT = j.FOH_ACCOUNT
LEFT JOIN Extras
on Extras.Code = PSFOLIOCHARGE.FOC_TRANSACTION_CODE
)vals
PIVOT ( SUM( foc_netamount) FOR (RevenueType IN([Beverage Charge],[Food Charge]) AS pvt
如果在没有修改数据库的情况下运行,我会感到非常惊讶,因为我没有办法测试它没有重新创建你的表,但它应该让你知道如何继续。