table name: transactions
| customer | debit | credit |
|---|---|---|
| a | 70 | 50 |
| a | 100 | 20 |
| a | 20 | 60 |
| b | 100 | 20 |
| b | 40 | 80 |
| b | 10 | 30 |
| c | 100 | 200 |
| c | 100 | 30 |
| c | 80 | 90 |
| d | 100 | 200 |
| d | 90 | 30 |
| d | 80 | 90 |
| e | 100 | 100 |
| e | 100 | 30 |
| e | 80 | 90 |
check who have positive or negative bal_type; if(total of debit-total of credit>0 ) positive else negative
I try below:
SELECT CASE WHEN (SUM(debit)-SUM(credit))<0 THEN "negative"
ELSE "positive"
END AS bal_type,
customer
FROM
transactions
GROUP BY customer
MY OUTPUT:
| bal_type | customer |
|---|---|
| positive | a |
| positive | b |
| negative | c |
| negative | d |
| positive | e |
expecting:
| bal_type | customer |
|---|---|
| positive | e,a,b |
| negative | c,d |
NOTE: bal_type is derived type column. positive customer name should be order by DESC using there SUM(debit)-SUM(credit). and negative customer name should be order by ASC using there SUM(debit)-SUM(credit).