amosse Posted December 5, 2007 Share Posted December 5, 2007 Hello to everybody I trying to get running balance, this is the query: set @bal1=0, @bal2=0; SELECT Id, Debit, Credit, Net, @bal2 := @bal2 + net AS CumNet FROM ( select id, fin_record_debit AS Debit, fin_record_credit AS Credit, @bal1 := fin_record_debit - fin_record_credit AS Net from fin_records ) AS tmp; Unfortunately I get CumNet NULL, both fin_record_debit and fin_record_credit don't have any NULL value (default value 0,00) Someone can help me? Please. Amos Link to comment https://forums.phpfreaks.com/topic/80362-solved-running-balance/ Share on other sites More sharing options...
fenway Posted December 6, 2007 Share Posted December 6, 2007 Have you read this?> Link to comment https://forums.phpfreaks.com/topic/80362-solved-running-balance/#findComment-407592 Share on other sites More sharing options...
amosse Posted December 7, 2007 Author Share Posted December 7, 2007 Got it! Both solutions are working: Solution 1 select x1.id,x1.fin_record_date,x1.fin_record_desc,x1.fin_record_debit,x1.fin_record_credit,sum(x2.fin_record_bal1) as fin_record_bal2 from (select id,fin_record_date,fin_record_desc,fin_record_debit,fin_record_credit,(fin_record_debit-fin_record_credit) as fin_record_bal1 from fin_records) as x1 inner join (select id,fin_record_date,fin_record_desc,fin_record_debit,fin_record_credit,(fin_record_debit-fin_record_credit) as fin_record_bal1 from fin_records) as x2 on x1.id >= x2.id group by x1.id order by x1.fin_record_date,x1.id; Solution 2 SET @bal1=0,@bal2=0; SELECT Id, Debit, Credit, Net, @bal2 := @bal2 + net AS CumNet FROM ( select id, fin_record_debit AS Debit, fin_record_credit AS Credit, @bal1 := fin_record_debit - fin_record_credit AS Net from fin_records ) AS tmp; BTW: Solution 2 is not working with MySQL Query Browser, but is working with MySQL Command Line Client Amos Link to comment https://forums.phpfreaks.com/topic/80362-solved-running-balance/#findComment-409108 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.