Huge fan of the site, this is my first post. My question: I have a calculated column (BA for Batting Average) from a baseball stat database. I am trying to query the highest BA from a particular players career, which I know I need the MAX aggregated function. My desired query result should pull the highest batting average and the year in which the player achieved this. I've included two rows of a particular players stats from 1986, using this my desired query should be BA: .200 year:(1986).
Year Team Lg G PA AB R H 2B 3B HR RBI SB CS BB SO BA OBP
1986 TOR AL 3 5 5 1 1 0 0 0 0 0 0 0 2 .200 .200
1987 TOR AL 4 5 10 1 1 0 0 0 0 0 1 0 4 .100 .100
MySQL code is as follows:
SELECT MAX(TRIM(LEADING '0' FROM ROUND(H/AB,3))) as BA, yearID
FROM mytable WHERE playerID = 'bathruth'
This code produces the correct max batting average but gives the first results in the yearID (The players rookie/debut year).
I would like the year (yearID) to be from the same row as the MAX BA (Highest Batting Average)