Skip to content
Advertisement

Postgresql Select and display one column based on another column

WALLET TABLE

ID IS_SPEND HOWMUCH

  1. true. 500
  2. false. 1000
  3. true. 5

I want to calculate how much money I have after several transactions.

spend as spend money and not spend as earn money

I tried

select is_spend, sum(howmuch) from table group by is_spend

But it cannot reach my goal.

I don’t know how can I get the result I want,

just output 495 is perfect.

Advertisement

Answer

You seem to want aggregation:

select sum(case when is_spend then -howmuch else howmuch end)
from table 
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement