Question

I have a column of USD and EUR currencies and would like to multiply the values to the corresponding exchange in chilean pesos. So far I know how to extract the contracts that are in USDand the ones that are in EUR but I don't kow how to multiply each value by its own exchange rate. I think it could be something like:

SELECT contracts.value, contracts.currency 
FROM contracts
WHERE contracts.currency='USD' then contracts.value=contracts.value*500  
   OR contracts.currency='EUR' then contracts.value=contracts.value*750

Thank you agian for the help!

Was it helpful?

Solution 2

You can filter your contracts.value by contracts.currency using CASE statement :

 SELECT contracts.currency 
      , CASE WHEN contracts.currency = 'USD' THEN contracts.value*500 
             WHEN contracts.currency = 'EUR' THEN contracts.value*750
             ELSE contracts.value
        END
 FROM contracts

OTHER TIPS

You can do that in the select clause:

SELECT IF(contracts.currency='USD', contracts.value*500, contracts.value*750) as amount
FROM contracts

Documentation: http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html

If your going to have more than two currencies, you could use a lookup table.

currency | factor
-----------------
USD      | 500
EUR      | 750

ANd then query like this:

SELECT c.contract_id, c.contract, c.value, c.currency, 
    cc.factor, c.value * cc.factor    AS calculatedValue
FROM contract c
    INNER JOIN currency_conversion cc ON c.currency = cc.currency

Demo here: http://sqlfiddle.com/#!2/3797e/2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top