I have two similar tables (one for bills, the other payments) right now I show to users a union mixing data from both..
Table Bills
CustomerId Amount
1 100
2 100
1 100
2 100
Table Payments
CustomerId Amount
1 100
2 100
1 100
Right now my users can see the following informtation
From Customer 1
Type CustomerId Amount
B 1 100
P 1 -100
B 1 100
P 1 -100
TOTAL 0
From Customer 2
Type CustomerId Amount
B 1 100
P 1 -100
B 1 100
Total 100
Everything works fine using UNION Statement
Right now I need to show Partial Balance on each record so users can keep in mind balanca while they are looking at records..
Like this... (DESIRED)
From Customer 1
Type CustomerId Amount Partial
B 1 100 100
P 1 -100 0
B 1 100 100
P 1 -100 0
I've alredy try using Variables like @Partial := @Partial + Amount but it seems that it first sum the first part (bills) and then the rest (payments)... like this...
From Customer 1
Type CustomerId Amount Partial
B 1 100 100
P 1 -100 200
B 1 100 200
P 1 -100 100
it seems that first sum everything from bills and then start subtraction... anyone knows how to solve it?
****** // update // ********
here original query ...
(SELECT 'Bill' as cType , b.type, b.tal , 'Customer', b.number , b.date , b.subtot, b.tax, IF(b.type='CA' or b.type='CB' or b.type='CC' or b.type='CX',b.total*-1,b.total) as total FROM bills b WHERE b.idcustomer='000140') UNION ALL (SELECT 'Payment' as cType, 'CO' , '1' , '' , c.idcash , c.date , 0 ,0 , -c.amount FROM cash c WHERE c.idcustomer='000140' and ( c.type='CO' or c.type='DM') ) order by date asc;
this brings something like this
Bill FX 1 Customer 9 2011-02-25 0.00 0.00 100.00
Payment CO 1 37 2011-03-04 0.00 0.00 -100.00
Bill FX 1 Customer 616 2011-03-23 0.00 0.00 100.00
Payment CO 1 751 2011-04-12 0.00 0.00 -100.00
Bill FX 1 Customer 1267 2011-04-27 0.00 0.00 100.00
Payment CO 1 1157 2011-05-10 0.00 0.00 -100.00
Bill FX 1 Customer 1974 2011-05-26 0.00 0.00 100.00
Payment CO 1 1654 2011-06-08 0.00 0.00 -100.00
then When I try to sum patiars...using the following code
set @running_total=0; (SELECT 'Bill' as cType , b.type, b.tal , 'Customer', b.number , b.date , b.subtot, b.tax, IF(b.type='CA' or b.type='CB' or b.type='CC' or b.type='CX',b.total*-1,b.total) as total, ( @running_total := @running_total + total) AS RunningTotal FROM bills b WHERE b.idcustomer='000140') UNION ALL (SELECT 'Payment' as cType, 'CO' , '1' , '' , c.idcash , c.date , 0 ,0 , -c.amount, ( @running_total := @running_total-c.amount) AS RunningTotal FROM cash c WHERE c.idcustomer='000140' and ( c.type='CO' or c.type='DM') ) order by date asc;
results...
Bill FX 1 Customer 9 2011-02-25 0.00 0.00 100.00 100.00
Payment CO 1 37 2011-03-04 0.00 0.00 -100.00 1905.00
Bill FX 1 Customer 616 2011-03-23 0.00 0.00 100.00 200.00
Payment CO 1 751 2011-04-12 0.00 0.00 -100.00 1805.00
Bill FX 1 Customer 1267 2011-04-27 0.00 0.00 100.00 300.00
Payment CO 1 1157 2011-05-10 0.00 0.00 -100.00 1705.00
As you Can See seems to sum first all from bills and then start substraccion from payments...