Skip to content
Advertisement

Sql server Balance Table Update Recursive

I Have a SQL Server database with a table named Balance

The table:

Id Date IN OUT Balance
3345312 2022-08-07 100 50 250
5435245 2022-08-06 50 50 200
4353451 2022-08-05 0 100 200
5762454 2022-08-04 20 100 300
7634523 2022-08-03 400 100 380
5623456 2022-08-02 100 20 80
4524354 2022-08-01 0 0 0
  • Id = Unique Identifier
  • Date = Balance Date
  • IN = IN Ammount
  • OUT = OUT Ammount
  • Balance = Last Day Balance + IN – OUT

The value of Balance column always should be equal to (Last Day Balance + IN – OUT)

That is, if on 2022-08-04 the number of entries is changed from 20 to 100, the balance from 2022-08-04 onwards must be recalculated:

Id Date IN OUT Balance
3345312 2022-08-07 100 50 330
5435245 2022-08-06 50 50 280
4353451 2022-08-05 0 100 280
5762454 2022-08-04 100 100 380

I’ve tried to make a recursive query to update this balance

It didn’t worked. It just updates the balance of the day I updated ignoring the next days.

Here is what I did until now:

Advertisement

Answer

You may use the following:

SUM([IN] - [OUT]) Over (Order By [Date]) will calculate the cumulative sum of (IN – OUT) over the increasing of date.

Note: I think it’s not a good practice to store the balance value in the table because it’s a calculated value, see this post.

But if you want to do that you may use updatable CTE as the following:

See a demo from db<>fiddle.

Update, according to the new requirement stated in the comments, that you have multiple products in the table and you want to perform the update on a specific product from a specific date up.

To find the running/ cumulative sum for multiple products you have to add Partition By Pid to the Over clause, where Pid is the product Id, so the select query will be:

And the update query:

See a demo.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement