4 Ways To Calculate A Running Total With SQL


Calculating a running total/rolling sum in SQL is a useful skill to have. 

It can often come in handy for reporting and even when developing applications. Sometimes your users might want to see a running total of the points they have gained or perhaps the money they have earned. Like many problems in SQL, there are multiple ways you can solve this problem.

You can use analytic functions, self joins or an aggregate table that tracks the running sum. Here are a few examples(Also, skip down to the bottom if you just want to watch these explanations in video form).

Using An Analytic Function

Using an analytic function is the easiest way to calculate a running total. An analytic function lets you partition data by specific field. For instance, in this case, we can break down the rolling sum by driver_id and month. This will give us the running total by customer and month. So every month will start again at 0.

Read more at Towards Data Science