Skip to content
Advertisement

How to add next and subtract preceding row in SQL Server based on condition

I am trying to calculate the cumulative sum based on TranslationType column in SQL Server.

Sample data:

I tried but I have a problem the output I am getting is wrong :

Desired output of the difference value:

SQL create scripts:

My query attempt:

Advertisement

Answer

You should be summing a CASE expression which can distinguish between debits and credits:

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