DECLARE @Test TABLE (
id int,
value int,
acc_no int
)
INSERT @Test(id, value, acc_no)
VALUES
(1, 12, 1),
(2, 14, 1),
(3, 15, 1),
(4, 10, 2),
(5, 16, 2),
(6, 19, 1),
(7, 7, 3),
(8, 24, 2)
SELECT id, t.value, acc_no, t.value - ISNULL(v.value, 0) AS result
FROM @Test t
OUTER APPLY (
SELECT TOP (1) value
FROM @Test
WHERE id < t.id
AND acc_no = t.acc_no
ORDER by id DESC
) v