By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE `stock_table` (
`ID` int(11) NOT NULL,
`product_code` varchar(20) NOT NULL,
`qty` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `stock_table` (`ID`, `product_code`, `qty`) VALUES
(1, 'PO1', 5),
(2, 'PO1', 12),
(3, 'PO1', 10),
(4, 'PO1', 8),
(5, 'PO1', 9),
(6, 'PO1', 16);
Records: 6 Duplicates: 0 Warnings: 0
SELECT `ID`,
`product_code`,
`qty`,
SUM(`qty`) OVER (PARTITION BY `product_code` ORDER BY `ID`) Qty_Total
FROM `stock_table`;
ID | product_code | qty | Qty_Total |
---|---|---|---|
1 | PO1 | 5 | 5 |
2 | PO1 | 12 | 17 |
3 | PO1 | 10 | 27 |
4 | PO1 | 8 | 35 |
5 | PO1 | 9 | 44 |
6 | PO1 | 16 | 60 |