SQL Server 2012: Generate Cumulative Sum Column using UNBOUNDED PRECEDING

The SQL Server 2012 also introduced UNBOUNDED option using PRECEDING and FOLLOWING options.

The UNBOUNDED option is very useful to determine a calculation from the beginning of a set to the current row, or from the current row to the end of a set or group.

We can calculate the cumulative SUM column with the many different options like: CTE, Self join, but in this post I am using UNBOUNDED PRECEDING option of SQL Server 2012.

For example, If we are working with sales and inventory management domain, every day we require to calculate cumulative sum of different columns like: stock value, profit figure, expense figure.

Generally, we can use this kind of demonstration for report purpose only and if it is required to store, we can populate and store for permanent use.

Below is a small demonstration to calculate Cumulative SUM column.

First, create sample table with data:

Populate cumulative SUM column using UNBOUNDED PRECEDING:

The Result:

Anvesh Patel

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz