感谢我之前提问的答案。我希望这样做可以,但我现在有另一个相关的问题:
我现在被要求延长我正在做的事情,包括将以当地货币持有的GrossCost转换为GBP的基础货币。
我单独为两个部分提供SQL,但不知道如何将它们组合在一起。这两部分是:
获取每家公司的GrossCost总和:
Select Cast(ROW_NUMBER() OVER (ORDER BY Sum(JobCosting.PurchaseOrderItems.GrossCost) Desc) AS Int) As 'RowNum',
Admin.Companies.UniqueName As 'UN',
Sum(JobCosting.PurchaseOrderItems.GrossCost) As 'DonutValue'
From Admin.Companies
Inner Join JobCosting.PurchaseOrders On Admin.Companies.CompanyId =
JobCosting.PurchaseOrders.SupplierCompanyId
Inner Join JobCosting.PurchaseOrderItems
On JobCosting.PurchaseOrders.PurchaseOrderId =
JobCosting.PurchaseOrderItems.PurchaseOrderId
Where UniqueName Like 'HH %'
Group By UniqueName
Order By 'DonutValue' Desc
获取每种货币的货币兑换率。转换为相关货币的GrossCost *汇率。这是Exchange Rate SQL(由SQLGRL提供 - 再次感谢):
SELECT *
FROM (SELECT CompanyId, UniqueName,CurrencyCode,ExchangeRate,FromCurrencyID, ToCurrencyId, ActiveDate,
rank() OVER
(partition BY UniqueName,Admin.Currencies.CurrencyId
ORDER BY ActiveDate DESC) AS Rank
FROM Admin.Companies
INNER JOIN Admin.Currencies
ON Admin.Companies.CurrencyId = Admin.Currencies.CurrencyId
INNER JOIN Admin.CurrencyRates
ON Admin.Currencies.CurrencyId = Admin.CurrencyRates.FromCurrencyId
WHERE Admin.CurrencyRates.ToCurrencyId = 47) ci
WHI ci.Rank = 1
我需要做的是最终得到一个组合的SQL,它返回所有相同的行并计算每个公司的GrossSales总和的转换值。
再次,非常感谢任何帮助,因为我真的被困住了。
提前致谢
答案 0 :(得分:0)
使用类似的东西 -
WITH cte AS (
SELECT c.*, --column_list
ROW_NUMBER(ORDER BY cur_from.ActiveDate desc) row_num
FROM Company c
INNER JOIN Currencies cu ON c.CurrencyId = cu.CurrencyId
INNER JOIN CurrencyRates cu_from ON cu.CurrencyId = cu_from.CurrencyFromId
-- WHERE CONDITION if any
)
SELECT * FROM cte WHERE row_num = 1
答案 1 :(得分:0)
这将为您提供每家公司的每种货币代码的最新汇率:
SELECT *
FROM (SELECT CompanyName,CurrencyCode,ExchangeRate3,ActiveDate,
rank() OVER
(partition BY CompanyName,dbo.Currencies.CurrencyId
ORDER BY ActiveDate DESC) AS Rank
FROM dbo.company
INNER JOIN dbo.Currencies
ON dbo.company.CurrencyId = dbo.Currencies.CurrencyId
INNER JOIN dbo.CurrencyRates
ON dbo.Currencies.CurrencyId = dbo.CurrencyRates.CurrencyFromId) ci
WHERE ci.Rank = 1
编辑以包含所需的其他信息:
with cte as (
Select Cast(ROW_NUMBER() OVER
(ORDER BY Sum(JobCosting.PurchaseOrderItems.GrossCost) Desc) AS Int) As 'RowNum',
Admin.Companies.UniqueName As 'UN',
Sum(JobCosting.PurchaseOrderItems.GrossCost) As 'DonutValue'
From Admin.Companies
Inner Join JobCosting.PurchaseOrders
On Admin.Companies.CompanyId = JobCosting.PurchaseOrders.SupplierCompanyId
Inner Join JobCosting.PurchaseOrderItems
On JobCosting.PurchaseOrders.PurchaseOrderId =JobCosting.PurchaseOrderItems.PurchaseOrderId
Where UniqueName Like 'HH %' Group By UniqueName Order By 'DonutValue' Desc
)
SELECT *
FROM (SELECT CompanyId, UniqueName,CurrencyCode,ExchangeRate,FromCurrencyID,
ToCurrencyId, ActiveDate,
rank() OVER
(partition BY UniqueName,Admin.Currencies.CurrencyId
ORDER BY ActiveDate DESC) AS Rank
FROM Admin.Companies
INNER JOIN Admin.Currencies
ON Admin.Companies.CurrencyId = Admin.Currencies.CurrencyId
INNER JOIN Admin.CurrencyRates
ON Admin.Currencies.CurrencyId = Admin.CurrencyRates.FromCurrencyId
WHERE Admin.CurrencyRates.ToCurrencyId = 47) as ci
INNER JOIN cte
on cte.UN = ci.UniqueName
where ci.Rank = 1;
此解决方案使用公用表表达式。但是,您可以通过创建视图并将视图加入排名查询来获得相同的结果。