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
PARTITION BY ORDER BY with Window Frames
13. Explanation
Using PARTITION BY ORDER BY with Analytical Functions
Summary and Review

Instruction

Nice work!

In part 5, you got to know window frames. Can we use them with PARTITION BY to create even more sophisticated windows? Of course we can. Take a look:

SELECT
  Id,
  Country,
  City,
  OpeningDate,
  Rating,
  MAX(Rating) OVER(PARTITION BY Country ORDER BY OpeningDate ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS MaxRating
FROM Store;

In the above example, we show some information about each store and the maximal rating of any store opened up to that date (that's where we need a window frame) in the respective country (that's where we need PARTITION BY).

Exercise

Show sales statistics between August 1 and August 7, 2016. For each row, show the StoreId, Day, the Revenue, and the highest revenue in that store up to that date. Name the column CurrentMaxRevenue.

Stuck? Here's a hint!

Use:

ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW