0

感谢您对我上一个问题的回答。我希望可以这样做,但我现在有另一个相关的问题:

我现在被要求扩展我正在做的事情,包括将以当地货币持有的 GrossCost 转换为 GBP 的基础货币。

我分别拥有这两个部分的 SQL,但不知道如何组合它们。这两部分是:

  1. 获取每家公司的 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

  2. 获取每种货币的货币兑换率。换算为相关货币的总成本 * 汇率。这是汇率 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
    

    WHERE ci.Rank = 1

我需要做的是最终得到一个组合 SQL,它获取所有相同的行并计算每家公司 GrossSales 总和的转换值。

同样,任何帮助都会非常感激,因为我真的被困住了。

提前致谢

4

2 回答 2

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;

此解决方案使用公用表表达式。但是您可以通过创建视图并将视图加入排名查询来获得相同的结果。

于 2013-10-02T18:41:21.863 回答
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
于 2013-10-02T18:23:46.007 回答