我在oracle中有两个表,表1和表2。
表1
mm/yy order-id cost currency
------- --------- ------ --------
01/17 1 25 pound
03/17 2 30 euro
04/17 3 16 pound
05/17 4 14 pound
06/17 5 10 euro
06/17 6 10 usd表2
dd/mm/yy currency Conversion_rate_to_USD
------------ ---------- --------------------
01/01/17 pound 1.24
02/01/17 pound 1.23
03/01/17 pound 1.26
........ ...... ....
........ ...... ....
........ ...... ....
01/04/17 pound 1.24
02/04/17 pound 1.23
03/05/17 pound 1.26
........ ...... ....
........ ...... ....
........ ...... ....
01/03/17 euro 1.15
02/03/17 euro 1.16
03/06/17 euro 1.11
01/06/17 euro 1.07我想为表1中的每一行检索以美元换算的成本(使用表2中当月的平均转换成本)。
提前感谢
发布于 2017-10-22 21:02:12
这个答案假设"date“列确实存储为日期。日期函数在不同的数据库中是不同的,但这(或一个小的变体)应该在大多数数据库中都有效。
要从table2获取每月的平均值:
select year(ddmmyy), month(ddmmyy), currency, avg(Conversion_rate_to_USD)
from table2
group by year(ddmmyy), month(ddmmyy), currency;要进行转换,请使用以下内容作为子查询:
select t1.*,
(t1.cost * t2.avg_c) as cost_in_usd
from table1 t1 join
(select year(ddmmyy) as yr, month(ddmmyy) as mon, currency,
avg(Conversion_rate_to_USD) as avg_c
from table2
group by year(ddmmyy), month(ddmmyy), currency
) t2
on t2.yr = year(t1.mmyy) and t2.mon = year(t1.mmyy) and
t2.currency = t1.currency;https://stackoverflow.com/questions/46874308
复制相似问题