database table name tbl_log
ID | month | open_qty | in_qty | out_qty | balance | item
----------------------------------------------------------------------------
1 | 2023-01 | 0 | 1 | 0 | 1 | A
2 | 2023-02 | 1 | 2 | 1 | 2 | A
3 | 2023-03 | 2 | 5 | 2 | 5 | A
4 | 2023-04 | 5 | 0 | 1 | 4 | A
I am not able to create an idea for calculating an opening balance
for each month where item = A
I am trying to achieve by using but no idea, I am using MySql query please help to calculate opening balance of the each month
please help
SELECT
COALESCE(SUM(QTY),
0) AS `OpeningBalance`
FROM
(
SELECT
`in_qty` + `open_qty` QTY
FROM
`tbl_log` CR
WHERE
CR.item = 'A'
) t