IniziaInizia gratis

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.

Questo esercizio fa parte del corso

PostgreSQL Summary Stats and Window Functions

Visualizza il corso

Istruzioni dell'esercizio

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

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

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;
Modifica ed esegui il codice