Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Quick Refresher
PARTITION BY ORDER BY with Ranking
8. NTILE(x) with PARTITION BY ORDER BY
PARTITION BY ORDER BY with Window Frames
Using PARTITION BY ORDER BY with Analytical Functions
Summary and Review

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 OpeningDay ASC) AS GroupNo
FROM Store;

In the above query, the stores are divided into two groups: older and newer 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 StoreId, the Day, the Revenue on that day, and the quartile number based on the Revenue of that store in descending order. Name the column Quartile.

Note: A quartile is one of four equal groups that are divided based on a value. In this case, we divide the rows into four groups based on store revenue.

Stuck? Here's a hint!

Use NTILE(4) and PARTITION BY StoreId ORDER BY Revenue DESC.