Calculate running total in SQL Server 2012 — Transcript
Full transcript
- 0:00this is part 112 of SQL Server tutorial
- 0:03in this video we'll discuss how easy it
- 0:05is to calculate running total in SQL
- 0:07Server 2012 and later versions we'll use
- 0:11this employees table on the left for the
- 0:13examples in this video based on this
- 0:15table we want to write a query which is
- 0:17going to retrieve name gender salary
- 0:19columns in addition to these three
- 0:21columns we also want this running total
- 0:24column which is going to keep track of
- 0:26running total of the salary column
- 0:28notice this running total column starts
- 0:30up at the first salary which is 5,000
- 0:33when we add 4,500 to that it is 9,500
- 0:36when we add 5,500 to 9,500 it is 15,000
- 0:40and it goes on like that now let's see
- 0:43how easy it is to compute this running
- 0:45total column using SQL Server sum
- 0:47aggregate function along with the Over
- 0:49clause let's flip to SQL Server
- 0:51management studio so from this employees
- 0:53table we want
- 0:56name
- 0:58gender salary and then the running total
- 1:02to compute the running total let's use
- 1:04the sum aggregate function we're going
- 1:06to use this function on salary column
- 1:10and then let's use the Over clause
- 1:12followed by that Auto
- 1:14buy and then let's sort the data by the
- 1:18ID column and then I'm going to call
- 1:21this
- 1:23running
- 1:24total now the first question that comes
- 1:27to our mind is why am I not using salary
- 1:30column in the autoby Clause there's a
- 1:32specific reason for that which we'll
- 1:34discuss in just a bit for now let's
- 1:37execute this query and see what we get
- 1:39notice we get the running total column
- 1:41and look at the values in this column it
- 1:42starts off with the first salary which
- 1:44is 5,000 when we add 4,500 to that it is
- 1:479,500 when we add 5,500 to that it is
- 1:5015,000 and it goes on like that at the
- 1:54moment we are not using Partition by
- 1:56Clause so the data in this table is not
- 1:58partitioned in any way we have this
- 2:00gender column so let's partition the
- 2:02data by gender column to do that I'm
- 2:04going to use Partition
- 2:07by gender let's execute this query and
- 2:11see what we get the first thing to
- 2:12notice here is that we get two
- 2:14partitions the first one is female
- 2:15employees partition and the next one is
- 2:17male employees partition and look at
- 2:19what happens to the running total column
- 2:22Within These two partitions So within
- 2:24the female employees partition the first
- 2:25salary is 5,500 so it starts off with
- 2:28that when we add 4,000 to that it is
- 2:319,500 when we add 5,000 to that it is
- 2:3414,500 and when we add 7,000 to 14,500
- 2:38it is
- 2:4021,500 and look at what happens when the
- 2:43partition changes from female to male it
- 2:47calculates running total for the male
- 2:49employees partition separately so the
- 2:51first salary within male employees
- 2:53partition is 5,000 so it starts off with
- 2:56that when we add 4,500 to that it is
- 2:589,500 when we we add 3,500 to that it is
- 3:0213,000 so it's that easy to compute
- 3:06running total whether you have
- 3:08partitions or whether for the entire
- 3:11table now let's answer this question why
- 3:14are we not using salary column in the
- 3:17order by Clause to answer that first
- 3:19let's look at the raw data that we have
- 3:21in our employees table so select sta
- 3:24from
- 3:27employees and look at this ID column
- 3:29this is the primary key column for this
- 3:31table so the data in this column is
- 3:34guaranteed to be unique now let's sort
- 3:37the data
- 3:38by salary
- 3:43column now if you notice these three
- 3:47rowes right here notice that all these
- 3:49three employees have got the same salary
- 3:51so there are duplicate values and if you
- 3:53look at these two employees you know
- 3:55they have 5,500 as you know the salary
- 3:59for both of those employees now let's
- 4:01see what's going to happen when we use
- 4:03salary column in the order by Clause so
- 4:06we know that the salary column has got
- 4:08duplicate values and if we use that
- 4:11column which has got duplicate values
- 4:13with the autoby Clause let's see what's
- 4:15going to happen to keep things simple
- 4:17I'm going to get rid of this Partition
- 4:19by Clause from here so let's execute
- 4:22this query and see what we get so look
- 4:26at the running total column it starts
- 4:28off with the first salary which is 3
- 4:29,500 when we add 4,000 to that it is
- 4:327,500 when we add 4,500 to that it is
- 4:3512,000 and when we add 5,000 to 12,000
- 4:38it should actually be 177,000 but look
- 4:41at what we getting
- 4:4327,000 how is that possible that's
- 4:46possible because what it's doing here is
- 4:49it's adding these three 5000s that is
- 4:525,000 + 5,000 + 5,000 which is 15,000 to
- 4:5712,000 and that is what is giving us
- 5:00this 27,000 and look at that it gives
- 5:02the 277,000 for these three rows where
- 5:05we have those
- 5:07duplicates and then look at that next we
- 5:10have two more rows with same salary
- 5:12which is
- 5:135,500 so 5,500 + 5,500 that is 11,000
- 5:18when we add 11,000 to 27,000 it is
- 5:2138,000 and that's what we get for these
- 5:25two rows so this may not be what you
- 5:28want you know according to your business
- 5:29needs if that's the case you know it's
- 5:31always better to use a column that has
- 5:35got unique values in the auto by Clause
- 5:38that's the reason why I have used ID
- 5:40column along with Auto buy so when we
- 5:43use the ID
- 5:47column I get the running total that I
- 5:50expect so whenever I add you know 4,500
- 5:53to 5,000 I get 9,500 when we add 5,500
- 5:56to 9,500 we get 15,000 and it goes on
- 5:59like like that so it maintains a true
- 6:02running
- 6:04total so here we have the partitions
- 6:07example and here we have the reasons of
- 6:09why we're not using you know the salary
- 6:12column that has got duplicate values
- 6:14along with the auto by Clause when
- 6:16Computing running
- 6:18total thank you for listening and have a
- 6:20great day
About this transcript
This page contains the full transcript of Calculate running total in SQL Server 2012 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 1,028 words across 149 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.