I have this table named expenses with the fallowing fields refnum, transdate, transtype, transcategory, amount
this table has the following data
refnum | transdate | transtype | transcategory | amount
1 | 10/10/2015 | cash-out | null | 5000.00
1 | 10/10/2015 | cash-in | null | 3000.00
1 | 10/10/2015 | cash-in | null | 1000.00
2 | 10/10/2015 | cash-out | null | 5000.00
2 | 10/10/2015 | cash-in | null | 3000.00
2 | 10/10/2015 | cash-in | null | 2000.00
3 | 10/10/2015 | cash-out | null | 5000.00
3 | 10/10/2015 | cash-in | null | 5000.00
The question is: Is it possible to get the balance of the amount base on its transtype and refnum
I have this query:
SELECT
A.refnum, A.transtype, A.amount,(A.amount-B.amount) as balance
FROM
expenses AS A
INNER JOIN
expenses AS B ON A.refnum = B.refnum
WHERE
A.transtype = 'cash-in'
AND B.transtype = 'cash-out'
AND (A.amount - B.amount) > 0
I am confused, this query I think is good if cash-in is exactly the same as the amount of cash-out but the problem is almost all cash-in is not the same amount of cash-out, hope you guys understand what i am asking for.
I need the query that will show only with the refnum of 1.
Is it possible?
SELECT refnum, SUM(CASE WHEN transtype='cash-out' then -amount ELSE amount END)) from expenses GROUP BY refnum