Introduction
Quick refresher
PARTITION BY ORDER BY with ranking
PARTITION BY ORDER BY with window frames
13. Explanation
PARTITION BY ORDER BY with analytical functions
Revision

Instruction

Alright! Let's move on. In part 5, you got to know window frames. Can we use them together with PARTITION BY to create even more sophisticated windows? Of course we can. Take a look:

SELECT
  id,
  country,
  city,
  opening_day,
  rating,
  MAX(rating) OVER(PARTITION BY country ORDER BY opening_day
    ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
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 store_id, day, revenue and the best revenue in the respective store up to that date.

Stuck? Here's a hint!

Use ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.

Console

Code editor

Result

TableConsole