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.


    Understand Frame Clauses

    Brett CassetteBrett Cassette

    What if you want aggregations that aren’t running totals? What if you want the sum total for the whole frame? Or the minimum value across the frame? Unbounded preceding, unbounded following, and current row help you define your frame of reference.



    Become a Member to view code

    You must be a 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




    Windows can have different frames of reference. Those are created with a rows clause. You could say "rows between." The default is unbounded preceding and current row. What that says is that all the rows in my window that come before me, plus myself.

    In this case, since we're creating a sum of the contributions, the sum of 500 and everything before is just 500. In this case, the sum of 30 and 500 is 530. In this row, the sum of all three of these, 555, etc., by the time we'd get to this last row. That's why this creates a running total for us, because that's the default when we use an order by clause.

    If we remove all of this. Now, that's not what we're going to get. What we're getting here is rows between unbounded preceding, and unbounded following. This will be the same thing.

    What that says is current row, plus all of the previous rows up here, nothing in all of the following rows. For all of these, you're going to get exactly the sum total of all of them.

    But if we want to use the same behavior even without the order by clause, now, we just use "and current row," and we get that running total again.

    Likewise, we could say between current row and unbounded following. That will do the running total in reverse. This one is all of the rows. Then, we keep subtracting until we get down to just the last row in the frame.