I have the following table:
id value acc_no
-----------------
1 12 1
2 14 1
3 15 1
4 10 2
5 16 2
6 19 1
7 7 3
8 24 2
Expected output
id value acc_no result
------------------------------
1 12 1 12(current row values of acc_no=1)
2 14 1 2(14 (current row values)-12(previous row value of acc_no=1))
3 15 1 1(15-14)
4 10 2 10(current row values of acc_no=2)
5 16 2 6(16 (current row values)-12(previous row value of acc_no=2))
6 19 1 4(19(current row values)-15(previous row value of acc_no=1))
7 7 3 7(current row values of acc_no=3)
8 24 2 8(24(current row values)-16(previous row value of acc_no=2))
I tried this query:
select
id, value,
acc_no,
(value - (select value from tb_acc t1 where t1.id = t.id - 1)) as result
from
tb_acc t
But I didn't get the proper output as expected