DOUBT ON SUBSELECT IN FIREBIRD

1

Hello, good evening! I have a table that wraps the input and output part of a box, an example of this is: On 01/01/2018 the user informed that he entered $ 200.00 in the company safe, on the same day he made another entry transaction in the amount of $ 800.00. At the end of the day he withdrew R $ 500.00 totaling the balance of the day in the amount of R $ 500.00 only referring to operations.

In my database it is described as

Data       Op           Valor
01/01/2018 +            200
01/01/2018 +            800
01/01/2018 -            500

It is the same way that it is just above, I would like to try to get the input values minus the output values, I created this select:

SELECT
SUM(SELECT A.DINHEIRO 
FROM TVENSANGRIASUPRIMENTO A 
WHERE A.OPERACAO = '+' 
AND A.IDSANGRIASUPRIMENTO = B.IDSANGRIASUPRIMENTO)

-

SUM(SELECT A.DINHEIRO 
FROM TVENSANGRIASUPRIMENTO A 
WHERE A.OPERACAO = '-' 
AND A.IDSANGRIASUPRIMENTO = B.IDSANGRIASUPRIMENTO)
FROM TVENSANGRIASUPRIMENTO B

But none of this is working, could anyone help me?

Sincerely.

    
asked by anonymous 01.01.2018 / 21:59

1 answer

0

You do not need a SubSelect for this, just use CASE

SELECT 
    CASE WHEN OPERACAO = '+' THEN SUM(DINHEIRO) END SaldoEntrada,
    CASE WHEN OPERACAO = '-' THEN SUM(DINHEIRO) END SaldoSaida, 
    (CASE WHEN OPERACAO = '+' THEN SUM(DINHEIRO) END) - (CASE WHEN OPERACAO = '-' THEN SUM(DINHEIRO) END) AS SaldoDiferenca

FROM TVENSANGRIASUPRIMENTO
    
02.01.2018 / 14:31