If with two conditions

2

I need in a select to add a field, so I need to check two conditions, if the currency is 3 divide by the exchange, if the operation is 18 take the whole sum and multiply by -1. The select I have does sum by checking the currency, but I can not put two checks to make negative when the operation is 18, because operation 18 is cancellation of registration.

 SELECT  SUM(IF(MOEDA=2,(VALOR/CAMBIO), VALOR)) VL FROM banco.CAIXA where DATA= '2018-11-06' AND OPERACAO IN(2,18) 
    
asked by anonymous 07.11.2018 / 15:55

1 answer

1

Using the case when, it would look this way. Example:

select
    sum(
    (case when tipo = 2 then (valor/cambio) else valor end)) AS IF1, --se a moeda for 2 faça a divisão pelo cambio
    (case when tipo = 18 then (valor/cambio) else valor end) * -1 as IF2 --se a operação for 18 tenho que além da divisão multiplicar por -1
        from banco.CAIXA;
    
07.11.2018 / 16:01