Jump to content

[SOLVED] running balance


amosse

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.