如何修改现有查询以考虑自定义 table 中的特定列?
How to modify my existing query to take into account a particular column from a custom made table?
我正在使用 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" 的 table,它看起来像这样:
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 table 但我不知道如何在我的查询中实现它。
我想我需要修改查询的最后一个 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
更新:
这是我的数据透视查询(基于 Merely Useful 提出的解决方案)并且它有效。我创建了一个视图 Table (HOLDINGS3) 并进行了一个数据透视查询以进入视图:
(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)
我们开始吧。您的查询很长,所以这可能行不通,但希望它能给您一个想法。此栏目打扰您了:
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)'
您最好只在 table 上离开加入,在新的额外 table 上离开加入,然后转出您需要的列。
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
如果这个 运行 没有在您的数据库上进行修改,我会感到非常惊讶,因为除了重新创建您的 table 之外我无法测试它,但它应该让您了解如何继续。
我正在使用 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" 的 table,它看起来像这样:
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 table 但我不知道如何在我的查询中实现它。 我想我需要修改查询的最后一个 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
更新: 这是我的数据透视查询(基于 Merely Useful 提出的解决方案)并且它有效。我创建了一个视图 Table (HOLDINGS3) 并进行了一个数据透视查询以进入视图:
(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)
我们开始吧。您的查询很长,所以这可能行不通,但希望它能给您一个想法。此栏目打扰您了:
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)'
您最好只在 table 上离开加入,在新的额外 table 上离开加入,然后转出您需要的列。
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
如果这个 运行 没有在您的数据库上进行修改,我会感到非常惊讶,因为除了重新创建您的 table 之外我无法测试它,但它应该让您了解如何继续。