Calculating Running Total in SQL
While it's easy enough to compute the total of selected rows, it's far more interesting to compute a running total over time. A running total is the summation of a sequence of numbers which is updated each time a number is added to the sequence, by adding the value of the new number to the previous running total.
For example, if your dataset is a "users_joined" table like so:
You can compute the total number of users joined in a day like this:
select date, count(user_id) from users_joined group by date order by date;
Which will give you a result like this:
If you wanted to compute a running total (or cumulative sum) of the counts, you can use a SQL window function to achieve that:
select date, count(user_id) as count, sum(count(user_id)) over (order by date) as running_total from users_joined group by date order by date;
No spam, ever! Unsubscribe any time. See past emails here.