I was reading over the tutorial here: http://www.1keydata.com/sql/sql-running-totals.html and it all made sense until it suddenly got extremely ridiculously unbelievably complicated when it got to rank, median, running totals, etc. Can somebody explain in plain English how that query results in a running total? Thanks!
I was reading over the tutorial here: http://www.1keydata.com/sql/sql-running-totals.html and it all made sense until
Share
Before I get started, I’ve not seen this before and it doesn’t look like a terribly comprehensible way to accomplish a running total.
Okay, here’s the query from the tutorial:
And the sample output
The simple part of this query is displaying the sales data for each employee. All we’re doing is selecting
nameandsalesfrom each employee and ordering them by the sale amount (descending). This gives us our base list.Now for the running total, we want every row that has already been displayed. So, we join the table against itself, on each row that would already have been displayed:
Then we use the
SUMaggregate function and group accordingly. A good way to understand this is if you look at what would happen if you didn’t use the group function. The ‘Sophia’ row would look like this:Notice how we got Greg’s sales row? The group will sum that up, and voila!
Hope that helps. Joe