0

I have this table:

+--------+-----------------+
| amount | transactionType |
+--------+-----------------+
|     10 | Debit           |
|     5  | Credit          |
|    15  | Credit          |
|    20  | Debit           |
|     2  | Credit          |
+--------+-----------------+

I can create a sum like so: SELECT sum(amount) from transactions, however, I need to perform a sum based on the column transactionType value, ie. if transactionType === 'Debit', minus the amount, if transactionType === 'Credit', add the amount

Can postgres do this natively?

2
  • Sure, look up the "case when ...". Dec 1 '21 at 15:36
  • Looks like a simple GROUP BY, but this is not a DBA question. Dec 1 '21 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.