Last active
August 22, 2022 18:40
-
-
Save wesrice/f1cc48fa8d18d17bc2de to your computer and use it in GitHub Desktop.
SQL Views for Sabermetrics using Lahman's Baseball Database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE VIEW sabermetrics_batting AS | |
SELECT | |
batting.*, | |
-- PA - Plate Appearances | |
(batting.AB + batting.BB + batting.HBP + batting.SF + batting.SH) as PA, | |
-- BB% - Walk Percentage (http://www.fangraphs.com/library/offense/rate-stats/) | |
round((batting.BB / (batting.AB + batting.BB + batting.HBP + batting.SF + batting.SH)), 3) as BBpct, | |
-- K% - Strikeout Percentage (http://www.fangraphs.com/library/offense/rate-stats/) | |
round((batting.SO / (batting.AB + batting.BB + batting.HBP + batting.SF + batting.SH)), 3) as Kpct, | |
-- ISO - Isolated Power (http://www.fangraphs.com/library/offense/iso/) | |
round((((batting.2B) + (2 * batting.3B) + ( 3 * batting.HR)) / batting.AB), 3) as ISO, | |
-- BABIP - Batting Average on Balls in Play (http://www.fangraphs.com/library/offense/babip/) | |
round(((batting.H - batting.HR) / ((batting.AB + batting.BB + batting.HBP + batting.SF + batting.SH) - batting.SO - batting.BB - batting.HR)), 3) as BABIP, | |
-- AVG - Batting Average | |
round((batting.H / batting.AB), 3) as AVG, | |
-- OBP - On Base Percentage - (http://www.fangraphs.com/library/offense/obp/) | |
round(((batting.H + batting.BB + batting.HBP) / (batting.AB + batting.BB + batting.HBP + batting.SF)), 3) as OBP, | |
-- SLG - Slugging Percentage | |
round(((batting.H + batting.2B + 2 * batting.3B + 3 * batting.HR) / batting.AB), 3) as SLG, | |
-- OPS - On Base + Slugging (http://www.fangraphs.com/library/offense/ops/) | |
round(((batting.H + batting.BB + batting.HBP) / (batting.AB + batting.BB + batting.HBP + batting.SF)) + (((batting.H - batting.2B - batting.3B - batting.HR) + (2 * batting.2B) + (3 * batting.3B) + (4 * batting.HR)) / batting.AB), 3) as OPS | |
FROM batting | |
ORDER BY batting.playerID ASC, batting.yearID ASC |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE VIEW sabermetrics_battingpost AS | |
SELECT | |
batting.*, | |
-- PA - Plate Appearances | |
(batting.AB + batting.BB + batting.HBP + batting.SF + batting.SH) as PA, | |
-- BB% - Walk Percentage (http://www.fangraphs.com/library/offense/rate-stats/) | |
round((batting.BB / (batting.AB + batting.BB + batting.HBP + batting.SF + batting.SH)), 3) as BBpct, | |
-- K% - Strikeout Percentage (http://www.fangraphs.com/library/offense/rate-stats/) | |
round((batting.SO / (batting.AB + batting.BB + batting.HBP + batting.SF + batting.SH)), 3) as Kpct, | |
-- ISO - Isolated Power (http://www.fangraphs.com/library/offense/iso/) | |
round((((batting.2B) + (2 * batting.3B) + ( 3 * batting.HR)) / batting.AB), 3) as ISO, | |
-- BABIP - Batting Average on Balls in Play (http://www.fangraphs.com/library/offense/babip/) | |
round(((batting.H - batting.HR) / ((batting.AB + batting.BB + batting.HBP + batting.SF + batting.SH) - batting.SO - batting.BB - batting.HR)), 3) as BABIP, | |
-- AVG - Batting Average | |
round((batting.H / batting.AB), 3) as AVG, | |
-- OBP - On Base Percentage - (http://www.fangraphs.com/library/offense/obp/) | |
round(((batting.H + batting.BB + batting.HBP) / (batting.AB + batting.BB + batting.HBP + batting.SF)), 3) as OBP, | |
-- SLG - Slugging Percentage | |
round(((batting.H + batting.2B + 2 * batting.3B + 3 * batting.HR) / batting.AB), 3) as SLG, | |
-- OPS - On Base + Slugging (http://www.fangraphs.com/library/offense/ops/) | |
round(((batting.H + batting.BB + batting.HBP) / (batting.AB + batting.BB + batting.HBP + batting.SF)) + (((batting.H - batting.2B - batting.3B - batting.HR) + (2 * batting.2B) + (3 * batting.3B) + (4 * batting.HR)) / batting.AB), 3) as OPS | |
FROM battingpost AS batting | |
ORDER BY batting.playerID ASC, batting.yearID ASC |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE VIEW sabermetrics_fielding AS | |
SELECT | |
fielding.* | |
-- PCT - Fielding Percentage | |
-- round(avg((fielding.PO + fielding.A) / (fielding.PO + fielding.A + fielding.E)), 3) as PCT | |
FROM fielding | |
ORDER BY fielding.playerID ASC, fielding.yearID ASC |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE VIEW sabermetrics_fieldingpost AS | |
SELECT | |
fielding.* | |
-- PCT - Fielding Percentage | |
-- round(avg((fielding.PO + fielding.A) / (fielding.PO + fielding.A + fielding.E)), 3) as PCT | |
FROM fieldingpost AS fielding | |
ORDER BY fielding.playerID ASC, fielding.yearID ASC |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE VIEW sabermetrics_pitching AS | |
SELECT | |
pitching.*, | |
-- IP - Innings Pitched | |
round((pitching.IPouts / 3), 3) as IP, | |
-- K/9 - Strikeouts per 9 innings (http://www.fangraphs.com/library/pitching/rate-stats/) | |
round((pitching.SO * 9) / (pitching.IPouts / 3), 3) as k_9, | |
-- BB/9 - Walks per 9 innings (http://www.fangraphs.com/library/pitching/rate-stats/) | |
round((pitching.BB * 9) / (pitching.IPouts / 3), 3) as BB_9, | |
-- K/BB - Strikeout to Walk Ratio | |
round((pitching.SO / pitching.BB), 3) as K_BB, | |
-- K% - Strikeout Percentage (http://www.fangraphs.com/library/pitching/rate-stats/) | |
round((pitching.SO / pitching.BFP), 3) as Kpct, | |
-- BB% - Walk Percentage (http://www.fangraphs.com/library/pitching/rate-stats/) | |
round((pitching.BB / pitching.BFP), 3) as BBpct, | |
-- HR/9 - Home Runs per 9 innings | |
round((pitching.HR * 9) / (pitching.IPouts / 3), 3) as HR_9, | |
-- AVG - Batting Average Against | |
round((pitching.H / (pitching.IPouts - pitching.BB - pitching.HBP - pitching.SH - pitching.SF)), 3) as AVG, | |
-- WHIP - Walks + Hits per Inning Pitch (http://www.fangraphs.com/library/pitching/whip/) | |
round(((pitching.BB + pitching.H) / (pitching.IPouts / 3)), 3) as WHIP, | |
-- BABIP - Batting Average on Balls in Play (http://www.fangraphs.com/library/pitching/babip/) | |
round(((pitching.H - pitching.HR) / (pitching.BFP - pitching.SO - pitching.BB - pitching.HR)), 3) as BABIP | |
FROM pitching | |
ORDER BY pitching.playerID ASC, pitching.yearID ASC |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE VIEW sabermetrics_pitchingpost AS | |
SELECT | |
pitching.*, | |
-- IP - Innings Pitched | |
round((pitching.IPouts / 3), 3) as IP, | |
-- K/9 - Strikeouts per 9 innings (http://www.fangraphs.com/library/pitching/rate-stats/) | |
round((pitching.SO * 9) / (pitching.IPouts / 3), 3) as k_9, | |
-- BB/9 - Walks per 9 innings (http://www.fangraphs.com/library/pitching/rate-stats/) | |
round((pitching.BB * 9) / (pitching.IPouts / 3), 3) as BB_9, | |
-- K/BB - Strikeout to Walk Ratio | |
round((pitching.SO / pitching.BB), 3) as K_BB, | |
-- K% - Strikeout Percentage (http://www.fangraphs.com/library/pitching/rate-stats/) | |
round((pitching.SO / pitching.BFP), 3) as Kpct, | |
-- BB% - Walk Percentage (http://www.fangraphs.com/library/pitching/rate-stats/) | |
round((pitching.BB / pitching.BFP), 3) as BBpct, | |
-- HR/9 - Home Runs per 9 innings | |
round((pitching.HR * 9) / (pitching.IPouts / 3), 3) as HR_9, | |
-- AVG - Batting Average Against | |
round((pitching.H / (pitching.IPouts - pitching.BB - pitching.HBP - pitching.SH - pitching.SF)), 3) as AVG, | |
-- WHIP - Walks + Hits per Inning Pitch (http://www.fangraphs.com/library/pitching/whip/) | |
round(((pitching.BB + pitching.H) / (pitching.IPouts / 3)), 3) as WHIP, | |
-- BABIP - Batting Average on Balls in Play (http://www.fangraphs.com/library/pitching/babip/) | |
round(((pitching.H - pitching.HR) / (pitching.BFP - pitching.SO - pitching.BB - pitching.HR)), 3) as BABIP | |
FROM pitchingpost AS pitching | |
ORDER BY pitching.playerID ASC, pitching.yearID ASC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In
pitching
andpitchingpost
,BFP
is the number of batters faced. To calculateBABIP
correctly we need the number of at-bats. I don't know that we can do so exactly for all records in the data, but I've been able to produce mostly identical results usingH/BAOpp
orBFP-HBP-BB-SH-SF
. Note that we have incomplete data before the year 2000.