Skip to content

Instantly share code, notes, and snippets.

@dnasca
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save dnasca/ae45c967e5f847b18ac9 to your computer and use it in GitHub Desktop.

Select an option

Save dnasca/ae45c967e5f847b18ac9 to your computer and use it in GitHub Desktop.
This query asks the question with the following constraints: Who are all the players in 2014 that had more than 10 HRs, 50 runs scored, 50 runs batted in, 10 stolen bases, and had at least 300 AB's? Also, for every player in the initial result set who met the requirements of HR>10, R>50, RBI>50, AB>300-- calculate the BBPERC (walks percentage) a…
##Using the baseball statistics dB found at http://www.seanlahman.com/baseball-archive/statistics/
SELECT
b.playerID,
CONCAT(m.nameFirst, ' ', m.nameLast) AS NAME,
SUM(b.AB) AS AB,
SUM(b.HR) AS HR,
SUM(b.R) AS R,
SUM(b.RBI) AS RBI,
SUM(b.SB) AS SB,
ROUND(b.bb / (b.AB + b.BB + b.HBP + COALESCE(b.SF, 0)) * 100,
2) AS BBPERC,
ROUND(b.so / (b.AB + b.BB + b.HBP + COALESCE(b.SF, 0)) * 100,
2) AS SOPERC,
ROUND(SUM(b.H) / SUM(b.AB), 3) AS AVG
FROM
Batting b,
Master m
WHERE
b.playerID = m.playerID
AND yearID = 2014
AND AB >= 300
GROUP BY b.playerID
HAVING HR >= 10 AND R >= 50 AND RBI >= 50 AND SB >= 10
ORDER BY SOPERC ASC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment