Introduction
Quick refresher
PARTITION BY ORDER BY with ranking
8. NTILE(x) with PARTITION BY ORDER BY
PARTITION BY ORDER BY with window frames
PARTITION BY ORDER BY with analytical functions
Revision

Instruction

Good job! Of course, you can use any other ranking function in the same way:

SELECT
  id,
  country,
  city,
  rating,
  NTILE(2) OVER(PARTITION BY country ORDER BY opening_day)
FROM store;

In the above query, the stores are divided into two groups: older and more recent stores. These groups are created separately for each country.

Exercise

Take the sales between August 1 and August 10, 2016. For each row, show the store_id, the day, the revenue on that day and quartile number (quartile means we divide the rows into four groups) based on the revenue of the given store in the descending order.

Stuck? Here's a hint!

Use NTILE(4) and PARTITION BY store_id ORDER BY revenue DESC.

Console

Code editor

Result

TableConsole