I have one table of data with revenue in multiple currencies (let's call this one TRANSACTION_TABLE
, with columns as such:
TRANSACTION_NAME
TRANSACTION_VALUE
CURRENCY
, and another table with exchange rates (EXCHANGE_RATE
) with columns as such:
FROM_CURRENCY (e.g. JPY)
TO_CURRENCY (e.g. USD)
EXCHANGE_RATE (x)
The table has, at minimum, every currency converting to USD, but is not exhaustive with exchange rates for non-USD TO_CURRENCY
values.
What I'm trying to achieve, is a query which converts the transactions to any currency, even if not explicitly stipulated in the EXCHANGE_RATE
table, by converting the currencies to USD first, and then from USD into the destination currency.
E.g. 1000 JPY to GBP:
- Find rate JPY to USD - calculation =
1000 * EXCHANGE_RATE = 9
- Find rate GBP to USD - calculation =
9 \ EXCHANGE_RATE = 7
At the moment, I've done a left join for TRANSACTION_TABLE
on EXCHANGE_RATE
but I'm lost at where to go next.
Any assistance would be greatly appreciated.
The query (very basic) I've built so far is as follows, and I'm a novice SQL user. I built this query first to convert to USD, which works fine (as my Exchange Rate table contains values for all currencies to USD) - but it obviously fails when setting the destination currency as GBP, as it'll just return nulls.
SELECT TRANSACTION_NAME,
SUM (TRANSACTION_VALUE * EXCHANGE_RATE)
AS "REVENUE GBP"
FROM TRANSACTION_TABLE S
LEFT JOIN EXCHANGE_RATE C ON S.CURRENCY = C.FROM_CURRENCY AND C.TO_CURRENCY = 'GBP'
ORDER BY TRANSACTION_NAME