给出货币和汇率元组的表格,如下所示:
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
答案 0 :(得分:1)
我能找到的唯一方法就是使用LOOP,我下面的所有查询都使用这个示例数据:
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);
第一步是获得所有互惠汇率(即B - > A,从A - > B表),因为这些是最容易得到的:
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个元组。
答案 1 :(得分:1)
您可以使用Recursive Common Table Expression
执行此操作设定:
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