The match is OVER
OVER() 子句可讓你在整個資料集上套用彙總函式,類似在 SELECT 中使用子查詢。相較於 SELECT 裡的子查詢,OVER() 子句有顯著優點——查詢會執行得更快,而且 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;