9 hours 41 minutes
Hi. Welcome back to Module six. This is less than 6.8 running totals. The the running total column is a very common report request common enough that I decided to include it as a lesson because depending on what version of Sequels urine, it's not always straightforward how you generate a running total.
So let's go ahead and take a look at these bullet points again. A running total is a common report request. If you write many sequel reports, you will run into the need for a running total eventually.
Now a running total is a column that, as the previous record totals, as the report generates,
window functions make running totals much easier to deal with. Without the window functions, you typically have to do some additional sequel gymnastics to get the running total working the way that you want it to work.
So again, the running total was much more difficult and older versions of sequel.
Interestingly, Microsoft Sequel into introduced The Window functions in Microsoft sequel 2005 however, and my sequel, they weren't introduced until my Simple eight in 2018 which is fairly recently.
Now, with all that out of the way. Let's go ahead and head to the development environment. Take a look at what this looks like.
I welcome back to the development environment, went ahead and pulled up the operator documentation at the My sequel website. If you're ever looking at some sequel code in my sequel and you see an unfamiliar character being used in what you derived to be some kind of special way, one thing you might check is the operators documentation.
And I bring this up because we're gonna be focused on this guy right here, which is that we're looking assignment operator. In my opinion, we know that's the assignment operator because it assigns a value,
and we'll have to use this operator to perform the older method of getting a running total. Now. With that said, let's go ahead and take a look at our query.
So I have the common table expression that we generated in the union lesson.
But I don't have the selection query,
so we'll go ahead and start with writing the older way of writing running totals. So I'm gonna write a select
Andi, I'm going to get the amount so that I can check that I have a a good running total when I haven't done something wrong.
And here's where things start to get a little interesting. I want to make a variable
total that at symbol and the kids that were starting a variable,
I'm gonna sign a value that value is going to be itself
we're gonna select that from the query or the common table expression.
Now, here's where things get a little interesting. Again, we're gonna join
on a selection
of the at running total variable
the assignment operator starting at zero.
And we're going to order by
the ref number.
We'll go ahead and limit by three. Initially, just to make sure that we're not overwhelmed with results
wouldn't run that.
Okay, so a drive table must, of course, have an alias. So rt running total
and we can see that we have the regular amount. Here
we add that up. We have the running total ending over here on the very right,
so it looks like the running total is working correctly.
Now we review the way this is implemented. We can see that this looks a little different than the quarries were probably used to writing and that we're declaring a variable were signing that variable to itself, plus the amount column. And then the really interesting part is that were then joining to that variable as its own derived table.
So in larger queries that could get, uh, confusing to look at, I definitely prefer the new method. So let's go ahead and take a look at the window functions and how that would look. So I'm gonna go ahead and erase this.
I'm gonna start writing out the way that we would handle this within new window functions.
So select rough number
never bring in the amount as well.
And then I'm going to some the amount
over, and that's starting that window function.
I'm gonna order by
Andi from Query.
That's limit three again.
Let's run that
and we got the same results, and I believe this is, and I think you'll agree to that. This is easier to read. We don't have that weird join on the variable going on, and we're not having to declare a variable. Were just using this new function the over in conjunction with the order by to Generate are running total.
So this becomes more or less just getting used to using the window functions to generate the running totals in the way that you want to.
Now let's go out and get rid of this limit three
and run all that.
So now we have a running total all the way to that total line that we had in the previous lesson. And, of course, if we wanted to add the total line, that be easy. We just write a union in our query and add that total line in,
and that completes this lesson on running totals.
Let's go ahead and head over and take a look at the summary,
and that brings us to the 6.8 summary
and this lesson. We added a running total to our previous union query.
We reviewed the old method and the new improved method for generating a running total.
And of course, I recommend the new method whenever possible. The only time it's really not possible is if you happen to be saddled with an old version of sequel that simply cannot be changed for some legacy reason
and of course, the reason is it's just simply easier to deal with.
So that completes this lesson, and I hope to see in the next thank you.
The Microsoft Azure Test is a premium Cybrary assessment created by Interview Mocha. The exam ...
AWS Data Engineering
The AWS Data Engineering test is a premium Cybrary assessment test created by Interview Mocha. ...