Ok! We'll introduce the features chronologically. In part 4, you learned ranking functions. They are one of the places where you can apply PARTITION BY
and ORDER BY
together.
So far, all the rankings we calculated were performed for all the rows from the query result. With that knowledge, we could have calculated the position of each store in the global network based on their ratings:
SELECT
id,
country,
city,
rating,
RANK() OVER(ORDER BY rating DESC)
FROM store;
Now, we can add PARTITION BY
to calculate the positions independently for each country:
SELECT
id,
country,
city,
rating,
RANK() OVER(PARTITION BY country ORDER BY rating DESC)
FROM store;
In this way, we create a separate ranking for each country, so Paris and Frankfurt can both get rank = 1
for the separate rankings in France and Germany: