Skip to content
Advertisement

Finding cumulative sum using SQL Server with ORDER BY

Trying to calculate a cumulative sum up to a given number. Need to order by 2 columns : Delivery, Date.

Query:

Results:

Expected results:

Version: Microsoft SQL Server 2017

Advertisement

Answer

You need a third column in the ORDER BY clause to break the ties on Contract_Date and Delivery. It is not obvious which one you would use. Here is one option using column Balance:

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