The match is OVER
OVER() 子句可让您在整个数据集上应用聚合函数,类似在 SELECT 中使用子查询。相比 SELECT 中的子查询,OVER() 子句有明显优势——查询运行更快,而且它还能配合更多附加函数和子句使用,我们会在本章后面介绍。
在本练习中,您将用 OVER() 子句改写前几章中的一些查询。
本练习是课程的一部分
SQL 中的数据处理
练习说明
- 从
match和country表中选择比赛ID、国家name、season、home_goal和away_goal。 - 完成查询,计算整体进球数的平均值,并使用窗口函数将该聚合值包含到每一行中。
交互式实操练习
通过完成这段示例代码来试试这个练习。
SELECT
-- Select the match id, country name, season, home, and away goals
___,
c.___ AS country,
m.season,
___.home_goal,
___,
-- Use a window to include the aggregate average in each row
___(___.home_goal + ___) ___ AS overall_avg
FROM match AS m
LEFT JOIN country AS c ON m.country_id = c.id;