Considering the following data:
DECLARE @TBL TABLE (RecordID BIGINT,
ID BIGINT,
RowNumber BIGINT,
Amount NUMERIC(6, 2),
Balance NUMERIC (6, 2)
)
INSERT INTO @TBL VALUES (99, 1, 1, 10, 20)
INSERT INTO @TBL VALUES (100, 3, 1, 5, 20)
INSERT INTO @TBL VALUES (101, 1, 2, 5, 20)
INSERT INTO @TBL VALUES (102, 1, 3, 10, 20)
INSERT INTO @TBL VALUES (100, 3, 2, 50, 20)
SELECT * FROM @TBL
Assuming in this case that we have a table with this data, what I'd like to do is within each ID, for each row number ascending subtract the amount to the balance. So, my expected output for balance column should be, respectively:
10
15
5
-5
-35
My question is if there is any way of using CTE instead of a cursor to accomplish this outcome?
Sorry I didn't post any images (they show better what I'm trying to accomplish, but the forum doesn't allow it since my reputation is < 10).