subtraction of values between columns other than the same table

Asked

Viewed 25 times

0

is the following, I am trying to get the result of a subtraction through a query between different columns of the same table.

I have the following table.

inserir a descrição da imagem aqui

As you can see in the table, I have the fact_type column, which can be (expense or revenue) and for each type is inserted a value that is stored in the value column, until then everything is right. However, after inserting the values in the table, I need to obtain the result of the subtraction between the values of tip_lancamento = revenue minus values of tip_lancamento = expense.

I’m trying this way, but it’s making a mistake

SELECT SUM(value) WHERE type_lancing = 'revenue' - SUM(value) type_lancing = 'expense' FROM lancings;

1 answer

1


I would trade the tipo_lancamento by an int type to gain more performance and be more precise. Failed to put the table name elsewhere too but the rest I would do so:

in Firebird:

Select (
    (SELECT SUM(valor) from lancamentos WHERE tipo_lancamento = 'receita') 
   - (SELECT SUM(valor) from lancamentos WHERE tipo_lancamento = 'despesa')
)from rdb$database
  • 1

    very obg Gabriel, following his solution, I was able to perform the query. Now I’m trying to adapt this query in the structure mvc.

Browser other questions tagged

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