我有这两个表格,包含了自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 |
+---------+----------+-----------+-----------+-----------------+
如果我想计算迈克·特劳特整个职业生涯的平均击球率:
/* 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次击球,他的平均击球率是0.352
最好的方法是什么?由于数据量巨大,即使是最小的查询也要花费大量时间。
暂无答案!
目前还没有任何答案,快来回答吧!