There is a table A which has Start and End Date, Price and CurrencyCode. There is a table B which has historical + Today's Exchange Rates. I need to convert table A Price based on the following assumptions:
- For the Historical Dates (I.e Start & End Date) < Today's Date, I will get their average exchange rate and use that for the conversion.
- Any future Date (Date > Today) will use today's exchange rate.
Esentially, I need to get an average Exchange Rate for the Duration of the WeekStart - WeekEnd Date.
Here is a schema/ some sample data for Table B which contains the Exchange Rate.
DateTime Date FromCurrCode ToCurrCode Rate 2016-07-07 07:16:12.000 2016-07-07 USD CAD 1.2939 2016-07-06 07:27:12.000 2016-07-06 USD CAD 1.3017 2016-07-05 07:34:07.000 2016-07-05 USD CAD 1.2922 2016-07-04 07:42:07.000 2016-07-04 USD CAD 1.2853 2016-07-03 07:52:06.000 2016-07-03 USD CAD 1.2908 2016-07-02 07:06:08.000 2016-07-02 USD CAD 1.2915
Here is a schema/sample data for Table A which has the WeekStart and WeekEnd Dates:
order# WeekStart WeekEnd Curr 1256 2016-07-04 00:00:00.000 2016-07-10 00:00:00.000 USD 1255 2016-07-04 00:00:00.000 2016-07-10 00:00:00.000 USD
Based on Table B, because the End Date goes into the future, the average rate should be calculated as the following:
2016-07-04 ---- Use historical currency
2016-07-05 ---- Use historical currency
2016-07-06 ---- Use historical currency
2016-07-07 ---- Use today's currency rate
2016-07-08 ---- Use today's currency rate
2016-07-09 ---- Use today's currency rate
2016-07-10 ---- Use today's currency rate
Then using these rates above, it should return an Average Rate as a column for each of Rows in Table A.
However, if the WeekStart and WeekEnd dates were in the past and less than today's date, then it would just calculate the average for the Start-End using the historical rate in table B.
Am I correct to assume this can be done via function? That would be the best approach. If so what would the function implementation look like.