Skip to content
Advertisement

Running subtraction but subtracting only if the amount is still above a limit

I have a table with the following structure, I want to subtract the TotalAmount to Amount but only if the remaining amount would still above the limit. The sorting is the sequence which amount should be tried first before moving on to the next data

Expected Result

I’ve been trying window functions, but I don’t know how to apply the logic of keeping the same amount with a condition.

I also tried something with case and checking if i’m below the limit

Advertisement

Answer

Declare a running variable @Amount to remember the amount deducted and do the calculation for each row selected.

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