依性別與項目找出衛冕冠軍
在前一個練習中,你用性別進行分割,確保一個性別的資料不會和另一個性別混在一起。不過,如果有多個欄位,只用其中一個來分割,其他欄位的結果仍然會被混在一起。
本練習屬於課程
PostgreSQL 統計摘要與視窗函式
練習說明
- 依性別與項目,回傳各年份賽事的前一屆冠軍。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
WITH Athletics_Gold AS (
SELECT DISTINCT
Gender, Year, Event, Country
FROM Summer_Medals
WHERE
Year >= 2000 AND
Discipline = 'Athletics' AND
Event IN ('100M', '10000M') AND
Medal = 'Gold')
SELECT
Gender, Year, Event,
Country AS Champion,
-- Fetch the previous year's champion by gender and event
___ OVER (___
ORDER BY Year ASC) AS Last_Champion
FROM Athletics_Gold
ORDER BY Event ASC, Gender ASC, Year ASC;