If with two conditions

Asked

Viewed 742 times

2

I need in a select to add a field, for this I need to check two conditions, if the currency is 3 divided by the exchange, if the operation is 18 take all the sum and multiply by -1. The select I have makes the sum by checking the currency, but can’t put two checks to make it 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) 
  • Your question is not clear, I even tried to answer but before it is necessary to understand these conditions that should be in if. Note that you can put one if within itself if, as an example: IF(pais = 'Brasil', 'R$', IF(pais = 'Estados Unidos', '$', '€')) AS simboloMonetario

1 answer

1

Using case when, it would look like this. 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;
  • the problem is that I need to test more than one condition, I need to check if the currency is 2 split by the exchange, and I must take into account that if the operation is 18 I have to multiply beyond the division by -1.

  • I changed the answer, check if it meets your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.