Introduction
Quick Refresher
5. PARTITION BY – Refresher 1
PARTITION BY ORDER BY with Ranking
PARTITION BY ORDER BY with Window Frames
Using PARTITION BY ORDER BY with Analytical Functions
Summary and Review

Instruction

Before we start writing queries with PARTITION BY and ORDER BY, let's quickly review how we use PARTITION BY in queries. Take a look:

SELECT
  Country,
  City,
  Rating,
  AVG(Rating) OVER(PARTITION BY Country) AS AvgRating
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 StoreId, the Day, the Revenue on that day and the average Revenue in that store. Name the last column AvgRevenue.

Stuck? Here's a hint!

Use PARTITION BY StoreId.