15,171,729 members
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 5:08am
v2

## Solution 1

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.

## Solution 2

SQL
```WITH    i AS
(
SELECT  inkcode, SUM(quantity) AS qin
GROUP BY
inkcode
),
o AS
(
SELECT  inkcode, SUM(quantity) AS qout
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```
fjdiewornncalwe 10-Jan-13 10:19am

Plagiarized from here

## Solution 3

how to display in above balance from two table for example 1 table name INVOICE and 2nd name REFUND
Patrice T 26-Dec-17 17:43pm