From the course: PostgreSQL: Advanced Queries
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Streamline partition queries with a WINDOW clause - PostgreSQL Tutorial
From the course: PostgreSQL: Advanced Queries
Streamline partition queries with a WINDOW clause
- If you write a query that includes multiple window functions, it's not uncommon to use the same partition over and over again. For instance, you may want to review the average, minimum and maximum price for each product within the same size classification. Using the OVER clause and a window frame partition, it might look like this. We'll pull the sku, product name, category ID, size and price columns from inventory.products, and we'll calculate the average min and max price over a partition by the product size. When I execute the query, the results give you these statistical details about the size group that each product is a member of. But there's a lot of repetition with the same over clause written out several times. If you wanted to alter the query, to look at a different partition, you'd have to update all three lines. This process can be simplified by specifying the window a single time within…
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
-
-
-
-
-
-