Aan de slagGa gratis aan de slag

Moving maximum of Scandinavian athletes' medals

Frames allow you to restrict the rows passed as input to your window function to a sliding window for you to define the start and finish.

Adding a frame to your window function allows you to calculate "moving" metrics, inputs of which slide from row to row.

Deze oefening maakt deel uit van de cursus

PostgreSQL Summary Stats and Window Functions

Cursus bekijken

Oefeninstructies

  • Return the year, medals earned, and the maximum medals earned, comparing only the current year and the next year.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

WITH Scandinavian_Medals AS (
  SELECT
    Year, COUNT(*) AS Medals
  FROM Summer_Medals
  WHERE
    Country IN ('DEN', 'NOR', 'FIN', 'SWE', 'ISL')
    AND Medal = 'Gold'
  GROUP BY Year)

SELECT
  -- Select each year's medals
  ___,
  ___,
  -- Get the max of the current and next years'  medals
  ___ OVER (ORDER BY ___ ASC
             ROWS BETWEEN ___
             AND ___) AS Max_Medals
FROM Scandinavian_Medals
ORDER BY Year ASC;
Code bewerken en uitvoeren