Note that there are some explanatory texts on larger screens.

plurals
  1. POPartial sum and subtraction (minus) in UNION Query
    primarykey
    data
    text
    <p>I have two similar tables (one for bills, the other payments) right now I show to users a union mixing data from both..</p> <pre><code>Table Bills CustomerId Amount 1 100 2 100 1 100 2 100 Table Payments CustomerId Amount 1 100 2 100 1 100 </code></pre> <p>Right now my users can see the following informtation</p> <pre><code>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 </code></pre> <p>Everything works fine using UNION Statement</p> <p>Right now I need to show Partial Balance on each record so users can keep in mind balanca while they are looking at records..</p> <p>Like this... (DESIRED)</p> <pre><code>From Customer 1 Type CustomerId Amount Partial B 1 100 100 P 1 -100 0 B 1 100 100 P 1 -100 0 </code></pre> <p>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...</p> <pre><code>From Customer 1 Type CustomerId Amount Partial B 1 100 100 P 1 -100 200 B 1 100 200 P 1 -100 100 </code></pre> <p>it seems that first sum everything from bills and then start subtraction... anyone knows how to solve it?</p> <p><em><strong></em>**<em>*</em>**<em>*</em></strong> // update // <strong><em>*</em>**<em>*</em>****</strong></p> <p>here original query ...</p> <p>(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;</p> <p>this brings something like this</p> <p>Bill FX 1 Customer 9 2011-02-25 0.00 0.00 100.00</p> <p>Payment CO 1 37 2011-03-04 0.00 0.00 -100.00</p> <p>Bill FX 1 Customer 616 2011-03-23 0.00 0.00 100.00</p> <p>Payment CO 1 751 2011-04-12 0.00 0.00 -100.00</p> <p>Bill FX 1 Customer 1267 2011-04-27 0.00 0.00 100.00</p> <p>Payment CO 1 1157 2011-05-10 0.00 0.00 -100.00</p> <p>Bill FX 1 Customer 1974 2011-05-26 0.00 0.00 100.00</p> <p>Payment CO 1 1654 2011-06-08 0.00 0.00 -100.00</p> <p>then When I try to sum patiars...using the following code</p> <p>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;</p> <p>results...</p> <p>Bill FX 1 Customer 9 2011-02-25 0.00 0.00 100.00 100.00</p> <p>Payment CO 1 37 2011-03-04 0.00 0.00 -100.00 1905.00</p> <p>Bill FX 1 Customer 616 2011-03-23 0.00 0.00 100.00 200.00</p> <p>Payment CO 1 751 2011-04-12 0.00 0.00 -100.00 1805.00</p> <p>Bill FX 1 Customer 1267 2011-04-27 0.00 0.00 100.00 300.00</p> <p>Payment CO 1 1157 2011-05-10 0.00 0.00 -100.00 1705.00</p> <p>As you Can See seems to sum first all from bills and then start substraccion from payments...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload