THE BOOK cover
The Unwritten Book is Finally Written!
An in-depth analysis of: The sacrifice bunt, batter/pitcher matchups, the intentional base on balls, optimizing a batting lineup, hot and cold streaks, clutch performance, platooning strategies, and much more.
Read Excerpts & Customer Reviews
If you are a media member and would like a review copy of The Book, please contact Kevin Cuddihy of Potomac Books.

Buy The Book from Amazon

MOST RECENT ARTICLES
MAIL : You ask | We say

Advanced


THE BOOK--Playing The Percentages In Baseball

<< Back to main

Tuesday, October 31, 2006

Database Hacks

By Tangotiger, 09:45 AM

I recommend Joe Adler’s Baseball Hacks.  This blog entry will be some of my hacks in using various data.  Here’s one for the Lahman database:


Want to know who played the most games at a particular position?

SELECT yearID, teamID, Pos, Mid(Max(Str(1000+[g])+"_"+[playerid]),7) AS player
FROM Fielding
GROUP BY yearID, teamID, Pos;

This is in Access.  You can do an equivalent one in any database.  The key part is this:
Mid(Max(Str(1000+[g])+"_"+[playerid]),7)

The first thing it evaluates is:
Str(1000+[g])

What that does is creates a 4-digit number, and then converts it into a string.  I then attach the playerid to the end of that string.  This:
Max(Str(1000+[g])+"_"+[playerid])
just gets me the maximum value, which because the “1” is the first character in the string, cancels out for everyone, and the next significant character is the number of games.  Attaching the playerid breaks ties, though you can insert whatever tie-breaker you want in-between.

The MID function simply strips out the playerid from the above result.  Voila.  Instant starting players for each team.

In the Lahman DB, some years have “OF” (so you only get one player here) and some have the three OF positions broken down.  The “OF” only years is more cumbersome to handle.

(13) Comments • 2008/04/02 • SabermetricsData
Page 1 of 1 pages

<< Back to main


Latest...

COMMENTS

Dec 05 04:40
Sabermetric Moves of the 2009 Pre-Season

Dec 05 05:33
Avery being Avery

Dec 05 05:06
NYC’s 3 1/2 year mandatory jail time sentence for carrying a loaded weapon

Dec 04 23:42
Poll: Would you vote Raines for the Hall?

Dec 04 23:07
How to calculate the area of a baseball field

Dec 04 22:48
Complete Run Expectancy, Retrosheet Years

Dec 04 22:03
Raines for the Hall

Dec 04 15:55
Mailbags on Parade

Dec 04 14:01
What would happen if the shootout period was 10 minutes, not 5?

Dec 04 11:49
Estimating BABIP