Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Create Running Totals Using Window Functions


    How could your Kickstarter-type site list the running total for each contribution next to it? Using window functions of course! The default behavior for aggregations under window functions is to create running totals.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    If we have a Kickstarter type site where we have projects and we have backers, and these backers are contributing to the projects, we have a list of each of their contributions. Maybe we want to show a running total of the contributions. We can do that with a sum function, sum of the contributions.

    We use our over clause to partition by the project ID. As we've seen in the past, if we don't include any kind of order clause, now the sum is going to be the same for the entire row, or for the entire window, and that's not what we want here. We'll have to have some kind of order by here, so we can say, "Order by contribution," which, by default, will be ascending.

    We see we start with 5.5, add 25. It gives us 30.5, etc., etc., for the entire window. This also works fine if we want to do it as a descending. We just have to make sure to include the order by, or some sort of explicit frame clause, which we'll talk about next.