Click here to Skip to main content
13,508,585 members
Rate this:
Please Sign up or sign in to vote.
See more:

I am stuck with a query of Transaction statement (balance sheet )...

I want calculate the balance of credit and debit of a customer...

Can you help me to develop query for calculate the balance ..(in mysql) .here is the normal structure..
                                                                      [ opening Balance - 0.00 ]

Transaction Date      Transaction Description   Debit     Credit          balance
---------------      ----------------------    -----     -----            --------
01/04/2012          maintance charge          2800       0.00              -2,800.00

01/04/2012           water charge             750.00     0.00              -3,550.00

16/04/2012           (...)                     0.00      8,400.00           4,850.00     

23/04/2013           (..)                      0.00      8,400.00           13,250.00

plz give a query to find the balance....

I think the equation is [(Credit - Debit) + previous.Balance]

[edit]Code block added - OriginalGriff[/edit]
Posted 11-Feb-13 21:54pm
Updated 31-Mar-18 22:40pm
Irbaz Haider Hashmi 12-Feb-13 4:00am
What have you done so far. Provide your work.
Also provide the table structure.
aravindnass 12-Feb-13 6:22am
I just tried but is my query

SELECT q.`credit`,q.`debit`
,@bal:=IF(@Id=q.`id`, @bal + ( - q.debit), - q.debit) AS balance
FROM Transcation AS q
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Have a look here:[^]

--total balance
SELECT (SUM(debit)*-1) + SUM(credit) AS TotalBalance
FROM Trasaction

--daily balance
SELECT TransDate, (SUM(debit)*-1) + SUM(credit) AS DailyBalance
FROM Trasaction
GROUP BY TransDate

--period of time balance
SELECT (SUM(debit)*-1) + SUM(credit) AS PeriodBalace
FROM Trasaction

--customer balance
SELECT CustId, (SUM(debit)*-1) + SUM(credit) AS CustomerBalance
FROM Trasaction
aravindnass 14-Feb-13 5:15am
Thank you for your answer ..But i need Separate Balance of each customer...
I think the equation is [(Credit - Debit) + previous.Balance] to find ..but I am unable to write the query... plz help me...
Maciej Los 14-Feb-13 5:18am
Have a look at the last example...
aravindnass 14-Feb-13 5:53am
getting error here:
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AS DailyBalace
FROM Trasaction
LIMIT 0, 1000' at line 1
Maciej Los 14-Feb-13 5:56am
Sorry, remove last ")"
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

you will get the last Balance by using this query

SELECT balance FROM Table name ORDER  BY date DESC LIMIT  1

Balance = (last Previous Balance + Credit) - Debit

than your query for insertion
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Not sure if this is still open. But you can try query like:

select distinct 
c.retailer_id as 'retailer',
c.created as 'date',
sum(c.item_total)as 'sales_total',
COALESCE(sum(d.amount),0) as 'bill_received',
(sum(c.item_total)-COALESCE(sum(d.amount),0)) as 'balance'

from `retailer_inventory` c
left join `retailer_payment` d

on c.retailer_id = d.retailer_id
and c.created =d.payment_date

where c.retailer_id=17

group by c.retailer_id,date

order by c.retailer_id,date asc
Richard MacCutchan 1-Apr-18 3:39am
FIVE years too late.
Member 13521806 1-Apr-18 9:26am
I never hoped that problem would have been waiting for my solution for 5 years :). It is just that I was working on similar problem today. Tried google, got to this result. Then figure out solution and in expectation that it might help somebody someday, I posted it to complete answer for original question!!!
CHill60 2-Apr-18 17:08pm
COALESCE(sum(d.amount),0) will return 0 if any value is null. Surely you just want to treat null values as zero, not the entire total.I.e.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180417.1 | Last Updated 1 Apr 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100