首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQL :从给定货币到目标货币的货币转换

SQL :从给定货币到目标货币的货币转换
EN

Stack Overflow用户
提问于 2016-10-05 19:27:25
回答 1查看 29.2K关注 0票数 1

我有两个表,Table1,amountCurrency和目的货币,Table2有转换率(如图像会话率中提到的,以美元为单位)。

我想做的事:

将金额从amountCurrency转换为目标货币,并在Table1的最后一栏中更新

示例: Table1第一行中的数量在INR中,我想将其转换为CAD。根据数学,我将得到1 INR在给定的conversion_date上的对话率,通过AmountCurrency从Table2倍数获得。就像,

代码语言:javascript
复制
select Rate from Table1 where converstion_Date = '2014-06-30' and Currency = 'INR'.

以上查询将给我0.0160752000,我们将将INR转换为美元,即100 * 0.0160752000 = 1.60752美元。

由于我们想要将它转换为CAD,在给定的conversion_date上,1 CAD = 0.9399380000美元,1 CAD得到的转换率,现在我们需要转换1.60752美元到CAD,可以通过它除以它的速度,即1.60752/1.60752 = 1.71024 CAD。

到目前为止,我的Table1大约有10000行,而Table2对所有日期的所有货币都有换算率。在CalculateAmountInDestinationCurrency列中迭代Table1行并进行转换和更新的最佳方法是什么。

我想做个循环,

代码语言:javascript
复制
While (Select Count(*) From Table1) > 0
begin

// step1 : Get top row   

//step2 : Get conversition rate for  **Amountcurrency** using select query to table2   

//step3 : Multiply with amount (Here we have USD value for amount)    

//step4: Get conversion rate for **DestinationCurrency**   

//step5: Divide USD Amount with result from step 4    

//Update   

end

任何帮助都是非常感谢的。这样做好吗?还有更好的办法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-05 20:24:19

您所需要的只是一个带有基表实例的查询和两个转换表实例,一个在base.amountCurrency = rate.currency上连接,另一个在base.destinationCurrency = rate.currency上连接。如果您需要在其中添加conversion_date标准,您也可以这样做,如果您对每种货币都有多个汇率,并且您想要的是最近的一个或任何一个。

类似于:

代码语言:javascript
复制
select
  b.*,
  a.rate as rate_amount,
  d.rate as rate_destination,
  amount * a.rate / d.rate as amtInDestCurrency
from
  base b inner join 
  rate a on
  b.amountcurrency = a.currency and
  b.conversion_date = a.conversion_date inner join
  rate d on 
  b.destinationCurrency = d.currency and
  b.conversion_date = d.conversion_date
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39882140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档