clear markdown compare help donate or buy diddy dollings comments/suggestions/bugs a leap of faith? bible365
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 635503 fiddles created (13136 in the last week).

CREATE TABLE dbo.TuTabla(INDICE int, ID int, NUM int, MONTO int) ;
 hidden batch(es)


INSERT INTO dbo.TuTabla VALUES (4,4,1,100), (5,4,2,0), (6,4,2,100), (7,4,1,100), (8,4,1,200), (9,4,2,1000) ;
6 rows affected
 hidden batch(es)


SELECT * FROM dbo.TuTabla A OUTER APPLY (SELECT SUM(CASE WHEN NUM = 2 THEN -1 ELSE 1 END * MONTO) SALDO FROM dbo.TuTabla WHERE ID = A.ID AND INDICE <= A.INDICE) B ;
INDICE ID NUM MONTO SALDO
4 4 1 100 100
5 4 2 0 100
6 4 2 100 0
7 4 1 100 100
8 4 1 200 300
9 4 2 1000 -700
 hidden batch(es)