SQL Running Total: How To Calculate a Running Total in SQL

preview_player
Показать описание

Calculating a running total in SQL is something that we need to do sometimes.

A running total is a number that includes all of the previous numbers before it. It can be used to calculate sales so far for a month, or the score so far in a sports match.

You can use the concept of a “window function” in SQL to calculate the running total. This works in major SQL vendors including Oracle, SQL Server, MySQL, and PostgreSQL.

Watch this video to see how to do it: both calculating a running total and a running total for separate groups.

Timestamps:
00:00 Intro
00:20 What is a running total?
00:40 Sample table
00:57 Select query
02:00 Add the SUM function
03:29 Results
04:03 Grouping and running total
05:32 Summary
Рекомендации по теме
Комментарии
Автор

Yep... Exactly what I needed... Been wrecking my head over all the sql queries I'd have to write to populate my graphs for my Web app and thought.... There's no way someone didn't think of a solution to this... And here is someone explaining said solution crystal clear... Lovely lovely lovely

teekay
Автор

This is absolutely awesome, vrystal clear. You are legend! Why other tutorials, most of them, to get the running as simple as this they use a cumbersome or convult synthax within the over clause like ove(order by ... rows between preceding unbounded and current....)

osoriomatucurane
Автор

Muy bien explicado :) muchas gracias por el aporte.

yensen
Автор

Hi Ben, This is a very simple effective, explanation. How would you modify the SUM function to total number of orders per day, resetting the counter on change of date?

tomsparenberg
Автор

Hi Ben, One of the best in explaining .. Thank you very much. I have subscribed and will visit your site. Is there a book you recommend, where the same example can be done using CTE, sub queries etc. Please can you provide me link to the database, so I can practice along with your videos.

granand
Автор

an error show in correct syntax near 'order'

mamushbejiga