Aan de slagGa gratis aan de slag

Last country by name

Just like you can get the first row's value in a dataset, you can get the last row's value. This is often useful when you want to compare the most recent value to previous values.

Deze oefening maakt deel uit van de cursus

PostgreSQL Summary Stats and Window Functions

Cursus bekijken

Oefeninstructies

  • Return the year and the city in which each Olympic games were held.
  • Fetch the last city in which the Olympic games were held.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

WITH Hosts AS (
  SELECT DISTINCT Year, City
    FROM Summer_Medals)

SELECT
  Year,
  City,
  -- Get the last city in which the Olympic games were held
  ___ OVER (
   ORDER BY ___ ASC
   RANGE BETWEEN
     UNBOUNDED PRECEDING AND
     UNBOUNDED FOLLOWING
  ) AS Last_City
FROM Hosts
ORDER BY Year ASC;
Code bewerken en uitvoeren