0

给定一个包含货币和汇率元组的表,如下所示:

EUR CHF 1.20
USD EUR 0.80
CHF JPY 1.30

我怎样才能简单地生成货币(A,B)和(B,A)之间的所有汇率?

我想要以下内容:

EUR CHF
CHF EUR

EUR USD
USD EUR

USD CHF
CHF USD

具有所有可能的组合,即可以通过链接多个速率从多个速率中得出一个速率

A to B * B to C * C to D = A to D
4

2 回答 2

1

我能找到的唯一方法是使用循环,我下面的所有查询都使用这个示例数据:

DECLARE @T TABLE (FromCurrency VARCHAR(3), ToCurrency VARCHAR(3), ExchangeRate DECIMAL(10, 5));
INSERT @T VALUES
    ('EUR', 'CHF', 1.20),
    ('USD', 'EUR', 0.80),
    ('CHF', 'JPY', 1.30);

第一步是获取所有互惠汇率(即 A -> B 表中的 B -> A),因为这些是最容易获得的:

DECLARE @TempExchangeRates TABLE (FromCurrency VARCHAR(3), ToCurrency VARCHAR(3), ExchangeRate DECIMAL(10, 5));

INSERT @TempExchangeRates (FromCurrency, ToCurrency, ExchangeRate)
SELECT  FromCurrency, ToCurrency, ExchangeRate
FROM    @T
UNION
SELECT  ToCurrency, FromCurrency, CAST(1 / ExchangeRate AS DECIMAL(10, 5))
FROM    @T t
WHERE   NOT EXISTS
        (   SELECT  1
            FROM    @T t2
            WHERE   t.FromCurrency = t2.ToCurrency
            AND     t.ToCurrency = t2.FromCurrency
        )

此时我们有:

CHF EUR 0.83333
CHF JPY 1.30000
EUR CHF 1.20000
EUR USD 1.25000
JPY CHF 0.76923
USD EUR 0.80000

所以我们仍然失踪

CHF --> USD 
EUR --> JPY 
JPY --> CHF 
JPY --> EUR 
USD --> EUR 

您需要继续执行此连接

SELECT  a.FromCurrency, b.ToCurrency, CAST(a.ExchangeRate * b.ExchangeRate AS DECIMAL(10, 5))
FROM    @TempExchangeRates a
        INNER JOIN @TempExchangeRates b
            ON a.ToCurrency = b.FromCurrency
            AND a.FromCurrency != b.ToCurrency
WHERE   NOT EXISTS
        (   SELECT  1
            FROM    @TempExchangeRates c
            WHERE   a.FromCurrency = c.FromCurrency
            AND     b.ToCurrency = c.ToCurrency
        )

并插入结果,直到找到所有组合:

WHILE (1 = 1)
    BEGIN
        INSERT @TempExchangeRates (FromCurrency, ToCurrency, ExchangeRate)
        SELECT  DISTINCT a.FromCurrency, b.ToCurrency, CAST(a.ExchangeRate * b.ExchangeRate AS DECIMAL(10, 5))
        FROM    @TempExchangeRates a
                INNER JOIN @TempExchangeRates b
                    ON a.ToCurrency = b.FromCurrency
                    AND a.FromCurrency != b.ToCurrency
        WHERE   NOT EXISTS
                (   SELECT  1
                    FROM    @TempExchangeRates c
                    WHERE   a.FromCurrency = c.FromCurrency
                    AND     b.ToCurrency = c.ToCurrency
                )

        IF @@ROWCOUNT = 0
            BEGIN
                BREAK;
            END
    END

在第一个循环中,这将检索

CHF USD 1.04166
EUR JPY 1.56000
JPY EUR 0.64102
USD CHF 0.96000

然后在第二

JPY USD 0.80128
USD JPY 1.24800

然后将找到所有 12 个元组。

于 2012-12-06T12:36:53.263 回答
1

您可以使用递归公用表表达式来做到这一点

设置:

Create Table ExchangeRates (
    FromCurrency char(3),
    ToCurrency char(3),
    Rate decimal(10, 2),
    Constraint PK_ExchangeRates Primary Key (FromCurrency, ToCurrency)
);

Create Index IX_ToCurrency On ExchangeRates(ToCurrency, FromCurrency, Rate);

Insert Into ExchangeRates (FromCurrency, ToCurrency, Rate) Values
  ('EUR', 'CHF', 1.20),
  ('USD', 'EUR', 0.80),
  ('CHF', 'JPY', 1.30);

CTE 假定所有货币代码正好是三个字符:

With AllExchanges as (
    Select
        FromCurrency,
        ToCurrency,
        Rate
    From
        ExchangeRates
    Union
    Select
        ToCurrency,
        FromCurrency,
        Cast(1.0 / Rate As Decimal(10, 2))
    From
        ExchangeRates
)
, Paths as (
    Select
        FromCurrency,
        cast(FromCurrency as varchar(max)) As ExchangePath,
        ToCurrency,
        Rate
    From
        AllExchanges a
    Union All
    Select
        p.FromCurrency,
        p.ExchangePath + ',' + p.ToCurrency,
        a.ToCurrency,
        Cast(p.Rate * a.Rate as Decimal(10, 2))
    From
        Paths p
            Inner Join
        AllExchanges a
            On p.ToCurrency = a.FromCurrency
    Where
        p.ExchangePath Not Like '%' + a.ToCurrency + '%'
    )
Select
    FromCurrency,
    ExchangePath + ',' + ToCurrency As ExchangePath,
    ToCurrency,
    Rate
From
    Paths

http://sqlfiddle.com/#!3/0fdc5

于 2012-12-06T14:41:57.527 回答