第一次在这里海报。
我有两个表 1)交易(T1)2)汇率(T2)。T1 保存多种货币的每日交易,T2 保存所有货币的每日汇率。
首先,我想计算给定时期内每种货币的平均汇率(比如 2016 年 1 月 1 日至 2016 年 6 月 30 日之间的美元)。
然后我想通过计算的平均汇率带出交易和转换的货币金额,以便美元交易使用计算的美元 AV。率并给我 GBP av 金额和 EURO 使用 EURO av。每一行的转换率等等。
获取平均速率的 SQL 如下;
select currency,avg(b.exch_rate) as avg_rate
from uviexchrates b
where date_from >'2015-01-01' and date_from < '2015-12-31'
and b.rates_to='gbp' and b.client like 'gc' group by b.currency
以上给了我类似的东西;
currency avg_rate
AUD 2.04
CAD 1.96
CHF 1.47
USD 1.41
我对事务表的查询是;
select currency,cur_amount from agltransact
where period between '201600' and '201606'
我追求的结果是;
cur_amount currency Av_rate converted_amount
-357000.00 EUR 1.12 -318153.46
6.55 EUR 1.12 5.84
6.55 EUR 1.12 5.84
27.77 USD 1.41 19.68
7.86 AUD 2.04 3.86
27.09 USD 1.41 19.20
54.98 CAD 1.96 28.11
计算最右边的 2 列。上面第一个查询的 Av_rate 和 convert_amount 是 cur_amount * av_rate 的结果。
问题; 我如何组合这两个查询以产生上述结果?
希望这很清楚。
非常感谢