0
votes

first time poster here.

i have two tables 1)transactions (T1) 2) exchange rates(T2). T1 holds daily transaction in several currencies, T2 holds daily exchange rates for all currencies.

firstly, i want to calculate the average rate for every currency for a given period (say for USD between 1 Jan 2016 to 30 Jun 2016).

i then want to bring out the transactions and converted currency amount by the calculated average rate so that the USD transaction uses the calculated USD AV. rate and gives me GBP av amount and the EURO use the EURO av. rate to convert and so on, for every single line.

the SQL to get the average rate out is as below;

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

the above gives me something like ;

currency    avg_rate
AUD         2.04
CAD         1.96
CHF         1.47
USD         1.41

my query for the Transaction table is;

select currency,cur_amount from agltransact
where period between '201600' and '201606' 

the result i am after is;

     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 

the 2 far right columns are calculated. Av_rate from above 1st query & the converted_amount is the result of cur_amount * av_rate.

Question; how do i combined the 2 queries so that the above result is produced?

hope that is clear.

many thanks

2
I think simple join will do...Avi

2 Answers

0
votes

I would use left join to connect the second table to the first query:

select t.currency, t.cur_amount, e.avg_rate, t_cur_amount / e.avg_rate
from agltransact t left join
     (select e.currency, avg(b.exch_rate) as avg_rate 
      from uviexchrates e 
      where e.date_from >= '2016-01-01' and e.date_from <= '2016-06-30' and
            e.rates_to = 'gbp' and
            e.client like 'gc'
      group by e.currency
     ) e
     on t.currency = e.currency
where t.period between '201600' and '201606' ;

Note: I changed the dates in the first query to match the description in the text.

0
votes
SELECT  T1.cur_amount ,
        T1.currency ,
        T2.avg_rate ,
        T1.cur_amount * T2.avg_rate AS converted_amount
FROM    ( SELECT    currency ,
                    cur_amount
          FROM      agltransact
          WHERE     period BETWEEN '201600' AND '201606'
        ) T1
        LEFT OUTER JOIN ( 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
                        ) T2 ON T1.currency = T2.currency