From the course: PostgreSQL: Advanced Queries

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Ordering data within a partition

Ordering data within a partition - PostgreSQL Tutorial

From the course: PostgreSQL: Advanced Queries

Ordering data within a partition

- [Instructor] The window frame created by an over clause contains the set of rows that a window function will operate on. We've seen that we can create dynamic partitions within this window frame, but we can also sort the rows within the frame. This sorting can have an effect on the way the aggregate function is applied. To demonstrate how this works, I'm going to query the inventory.categories table just to get a couple of numbers to play with. We'll simply select the category_ID column from the categories table. The results give me a single column with the values one, two, and three. Now we can calculate and add up these three numbers by using a sum total with an over clause. Let's add that into the query. Remember that if you leave the over clause empty, we have an open set of parentheses here, then the window frame will contain all of the rows. When I execute the query, we get a second column here that has…

Contents