我有这两张表包含自1952年以来所有棒球比赛的历史数据(回顾表数据)。
Game table (all games since 1952)
+--------+------------+
| GameID | Date |
+--------+------------+
|BOS0102 | 2015-06-01 |
|TOR0189 | 2015-06-01 |
|NYY0878 | 2015-06-02 |
|BOS0105 | 2015-06-02 |
+--------+------------+
Event table (play by play for each game)
+---------+----------+-----------+-----------+-----------------+
| EventID | GameID | BatterID | EventCode | IslAtBat |
+---------+----------+-----------+-----------+-----------------+
| 45352 | BOS01021 | mtrout001 | 1B | True |
| 45369 | BOS01021 | mtrout001 | FO | True |
| 45382 | BOS01021 | mtrout001 | X2 | True |
| 45398 | BOS01021 | mtrout001 | X1 | True |
| 45458 | BOS01021 | mtrout001 | Y9 | False |
+---------+----------+-----------+-----------+-----------------+
如果我想在他的整个职业生涯中计算Mike Trout击球率:
/* Batting average = number hits / number at-bats
/* Codes that count in the batting average : 1B, 2B, 3B, HR */
SELECT SUM(Case EventCode IN(‘1B’, ‘2B’, ‘3B’, HR’) THEN 1 ELSE 0 END) AS ‘NumberHits’,
SUM(Case IsAtBat IS TRUE THEN 1 ELSE 0 END) as ‘NumberAtBats’,
SELECT SUM(Case EventCode IN(‘1B’, ‘2B’, ‘3B’, HR’) THEN 1 ELSE 0 END)/SUM(Case IsAtBat IS TRUE THEN 1 ELSE 0 END) as ‘AVG’
FROM events WHERE BatterID = ‘mtrout001’
我想做的是:
对于游戏桌中的每个游戏,计算游戏中之前最后25个击球手的击球平均值,并将结果插入新表中:
+----------+------------+-------------------+
| GameID | BatterID | Last25AtBatAverage|
+----------+------------+-------------------+
| BOS01021 | mtrout001 | .352 |
| BOS01021 | jdoe001 | .212 |
| BOS01021 | mjohnson | .119 |
| BOS01021 | yhong001 | .326 |
| BOS01021 | rthoip001 | .280 |
| BOS01021 | qwillis001 | .096 |
| BOS01021 | agort001 | .257 |
| BOS01021 | bmay001 | .418 |
| BOS01021 | yhong001 | .326 |
| STL03023 | pdupuis001 | .256 |
| STL03023 | jwarren001 | .228 |
+----------+------------+-------------------+
这将告诉我,在2015-06-01赛季波士顿之前的最后25场比赛中,迈克特劳特的击球率为.352
最好的方法是什么?由于数据量巨大,即使是最小的查询也会占用大量时间。