MYSQL Operation subtraction fields from the same table with different WHERE?

Asked

Viewed 41 times

1

Hello

There is a table with TYPE field and Value,

Operation value field output (TYPE = REVENUE) - (TYPE = EXPENSE) in MYSQL in a Select

inserir a descrição da imagem aqui

  • 1

    Need to add up all that revenue and subtract all that is expense? Or are there other conditions?

  • exactly, add up all that is revenue and subtract all that is expense, just.

  • in this case, it seems that there is already a correct answer. if it solves your problem you must accept it.

1 answer

3


Here is an example for testing where the Case command within the Sum function is used to multiply the value by -1 when the type is spent, and thus subtract:

select sum(valor * case when tipo = 'RECEITA' then 1 else -1 end) as resultado
from Tabela

I hope it helps

  • Very obg, it worked !

Browser other questions tagged

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