SQL 2016 Hosting Italy – SQL Server: How to Calculate running totals using T-SQL?

This is not a new topic. If you search, you may sure realize several posts on this, largely with old techniques however not using newest capabilities like SQL 2016 Windowing. Since I wrote a post on Analysis Services for identical, thought to put in writing the same on T-SQL too. Here is that the approach of calculating running totals using Window components and functions that provides an efficient way of calculating and easy code structure.

The following question shows the approach of calculating. the primary code creates a window based on SalesOrderId (which is unique) and obtain the running totals over SubTotal for a given year. The second code creates a window on OrderDate (which isn’t unique). this may show the totals for the date rather than Running-Totals for the date unless the range is nominative using boundaries. that’s the explanation for adding upper and lower boundaries using ROW, UNBOUNED PRECEDING and CURRENT ROW within the window for restricting rows to be participated for the calculation.

Here may be a comparison on a similar using old techniques. tho’ it shows that the query uses window is quicker than different queries, always check and choose the most effective.