按性别与项目统计的卫冕冠军
在上一个练习中,您按性别进行了分区,以确保某一性别的数据不会混入另一性别的数据中。不过,如果有多列,仅按其中一列分区,其他列的结果仍会被混合在一起。
本练习是课程的一部分
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;