WALLET TABLE
ID IS_SPEND HOWMUCH
- true. 500
- false. 1000
- 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