Introduction
Quick refresher
5. PARTITION BY – refresher 1
PARTITION BY ORDER BY with ranking
PARTITION BY ORDER BY with window frames
PARTITION BY ORDER BY with analytical functions
Revision

Instruction

Before we start writing queries with PARTITION BY ORDER BY, let's quickly revise queries with PARTITION BY alone. Take a look:

SELECT
  country,
  city,
  rating,
  AVG(rating) OVER(PARTITION BY country)
FROM store;

In the above query, we show the rating of each store plus the average rating calculated for the respective country. If we hadn't used PARTITION BY country, we would have ended up with an average across all stores. This way, we get separate average values for each country.

Exercise

For each sales row, show the store_id, day, revenue on that day and the average revenue in that store.

Stuck? Here's a hint!

Use PARTITION BY store_id.

Console

Code editor

Result

TableConsole