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 - 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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Create a window function with an OVER clause4m 11s
-
(Locked)
Partition rows within a window4m 7s
-
(Locked)
Streamline partition queries with a WINDOW clause2m 42s
-
(Locked)
Ordering data within a partition5m 8s
-
(Locked)
Calculate a moving average with a sliding window4m 52s
-
(Locked)
Return values at specific locations within a window7m 53s
-
(Locked)
Solution: Leverage window functions4m 41s
-
-
-
-
-
-