Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to calculate the running balance as below format.
Same like bank A/c.

Opening balance is in another table.

HTML
  DATE            DEBIT       CREDIT     BALANCE
             OPENING BALANCE              5000
10-02-2012        1500                    6500
12-02-2012                     2000       4500
15-02-2012         500                    5000


Please do the needful for writing SQL Query.


Thanks,
Karthik.J
Posted
Updated 26-Dec-17 4:08am
v2

Start looking at the JOIN[^] construct. This will help you write your own query that will join two tables and return the result as appropriate.
 
Share this answer
 
SQL
WITH    i AS
        (
        SELECT  inkcode, SUM(quantity) AS qin
        FROM    tblInkReceiving
        GROUP BY
                inkcode
        ),
        o AS
        (
        SELECT  inkcode, SUM(quantity) AS qout
        FROM    tblInkDelivery
        GROUP BY
                inkcode
        )
SELECT  COALESCE(i.inkcode, o.inkcode) AS inkcode,
        COALESCE(qin, 0) AS stock_in,
        COALESCE(qout, 0) AS stock_out,
        COALESCE(qin, 0) - COALESCE(qout, 0) AS stock_balance
FROM    i
FULL JOIN
        o
ON      o.inkcode = i.inkcode
 
Share this answer
 
Comments
fjdiewornncalwe 10-Jan-13 10:19am    
Plagiarized from here
how to display in above balance from two table for example 1 table name INVOICE and 2nd name REFUND
 
Share this answer
 
Comments
Patrice T 26-Dec-17 17:43pm    
Not a solution. Ask your own question.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900