Hi kutta_vin
this is really a hard task. Finally I have got the following solution:
select transaction, credit, debit, credit+coalesce((select sum(credit)
from act b where b.transaction < a.transaction),0) as sumCredit,
debit+coalesce((select sum(debit) from act b
where b.transaction < a.transaction),0) as sumDebit,
sumCredit - sumDebit as Balance from act a order by transaction;
/********* result:
transaction credit debit sumCredit sumDebit Balance
-----------------------------------------------------------
DB10004 233.00 0.00 233.00 0.00 233.00
DB10005 0.00 33.00 233.00 33.00 200.00
DB10006 40.00 0.00 273.00 33.00 240.00
DB10007 0.00 20.00 273.00 53.00 220.00
*********/
It is important that the rows be uniquely sequenced by Transaction column.
This is a modification of a well-known Celko-solution for running totals I have rewritten to meet your requirements. However, I haven't tested it completely, yet it seems that this solution meets at least your small sample. Tell me whether above solution is right to solve your problem.
krs,
tesu
p.s. Oh sorry, I really hope that MySQL would also be able to deal with subselects as specified in SQL standard 1999!