0

How do I take the following table:

enter image description here

and make it so the Amount 3 column subtracts from the remaining amount in the row above?

enter image description here

Basically, I know I can do Amount 1 - Amount 2 to get the difference, but if I have multiple values I am trying to subtract from an original value, how can I write a SQL function so the Amount 2 column is added to the cumulative remaining balance in the above Amount 3 column to have a new cumulative remaining amount?

I'm assuming it's some sort of LAG function, but I still need help.

YuTing
  • 5,960
  • 2
  • 3
  • 16
  • Does this answer your question? [Calculate a Running Total in SQL Server](https://stackoverflow.com/questions/860966/calculate-a-running-total-in-sql-server). If that doesn't work for you, search this site for `sql running total` or `sql running balance` for many more possible existing answers. – Ken White Apr 22 '22 at 02:31

0 Answers0