I have a Currency table with the following structure where currency rates for each transaction currency are maintained in reference currency i.e. EUR but not necessarily in other currencies.
bcur|curr | effectivedt|Expressinbase|rate
EUR |SAR |01/04/2020 |1 |12.23
EUR |SAR |01/05/2020 |1 |12.27
EUR |SAR |01/06/2020 |1 |12.29
EUR |INR |01/04/2020 |1 |77.78
EUR |INR |01/05/2020 |1 |77.90
EUR |USD |01/04/2020 |1 |1.34
EUR |GBP |01/04/2020 |1 |23
EUR |GBP |01/05/2020 |1 |32
USD |SAR |01/04/2020 |1 |45
USD |SAR |01/05/2020 |1 |54
USD |GBP |01/04/2020 |2 |0.83
INR |SAR |01/04/2020 |1 |80
.
.
I am selecting my sales order table for some amounts and now I need to convert the amount in transaction currency to EUR, USD and INR as per the transaction date in the Sales order table.
When transaction currency = the currency under consideration assign the amount accordingly
When transaction currency <> the currency then
if the relation exists is currency table.
If the rate is maintaied with Expressinbase = 1 then Amount in <curr> = Sales Order Amount / rate
If the rate is maintaied with Expressinbase = 2 then Amount in <curr> = Sales Order Amount * rate
If no direct relation is maintained (for USD or INR)
Amount in <USD or INR> = Amount in EUR/ rate of EUR to <USD or INR>
This is quite helpful for the logic but I am still stuck at getting only 1 record from currency table as per transaction date
Implicitly Calculate Exchange Rates in SQL from Exchange Rate table