View Single Post
Old 12-13-2021, 03:53 PM   #1
Syd Thrift
Hall Of Famer
 
Syd Thrift's Avatar
 
Join Date: May 2004
Posts: 10,611
A couple of quick MySQL tricks I thought I'd post

I'm realizing now that since I work with SQL a lot in my daily life, I kind of know my way about the syntax. So, I'd figure I'd use this space to come up with some reports that I plan on using and which you also might find come in handy.

Note that I'm using a MySQL DB called 'modern-ish-baseball'; you'd need to replace that with whatever you're calling your own DB to use this (and of course you'll need to have OOTP dump the relevant tables).

I'll start here with a report of a team's batters. The stats I included are your standard "baseball card" stats, no more, no less, and players are ordered by their primary position (i.e. the place they played the most at). As I'm running a temp table in the WHERE clause, this is not the most efficient query in the world but I'm running it against a 24 team league with around 25 years of history and it still only took about 2s to run.

The results:

Code:
abbr    first_name	last_name	g	ab	r	h	2B	3B	hr	rbi	bb	ibb	k	hpb	sh	sf	sb	cs	gdp	ba	    obp	    slg	    pos	g
NYY	    Jason	    Mooneyhan	116	435	54	114	18	1	10	61	29	2	34	2	0	11	0	0	17	0.262	0.929	0.377	2	114
NYY	    Khalil	    Tabb	    64	179	19	62	9	0	4	29	11	5	32	2	0	1	0	0	11	0.346	0.987	0.464	2	52
NYY	    Josh	    Paige	    3	3	0	0	0	0	0	0	0	0	0	0	0	0	0	0	1	0.000	--  	0.000	2	2
NYY	    Justin	    Richens	    63	231	34	71	17	2	9	26	41	3	14	6	0	5	0	0	6	0.307	0.959	0.515	3	63
NYY	    Jose	    Ayala	    51	193	24	55	9	0	5	23	13	2	34	1	0	4	2	0	10	0.285	0.945	0.409	3	48
NYY	    John	    Chapman	    60	138	13	31	6	0	3	14	26	1	16	1	0	2	0	0	0	0.225	0.967	0.333	3	33
NYY	    Bobby	    Berg	    12	23	7	8	2	0	2	8	6	0	0	0	0	0	0	0	2	0.348	1.000	0.696	3	6
NYY	    Wing-fung	Yi	        105	383	44	117	17	6	6	45	67	5	29	2	0	3	12	15	4	0.305	0.984	0.428	4	103
NYY	    Elijah	    Patton	    38	92	10	19	4	0	0	12	9	3	13	0	0	2	0	1	5	0.207	0.933	0.250	4	17
NYY	    Chris	    Eckert	    16	33	4	7	1	0	0	3	4	2	7	0	0	0	0	0	2	0.212	1.000	0.242	4	7
NYY	    Nate	    Lieb	    3	9	1	2	0	0	1	2	2	1	3	0	0	0	0	0	0	0.222	1.000	0.556	4	3
NYY	    Tom	        Weiss	    128	423	62	115	12	2	17	69	60	3	38	7	1	3	0	0	8	0.272	0.978	0.430	5	108
NYY	    David	    Salinas	    49	169	14	36	5	0	1	18	15	0	12	1	0	2	2	2	8	0.213	0.963	0.260	5	45
NYY	    Ty	        Stover	    146	530	85	127	24	6	25	92	96	1	105	3	0	7	1	1	19	0.240	0.970	0.449	6	116
NYY	    Jason	    Wichert	    28	72	4	14	2	0	0	6	3	0	18	0	0	2	1	0	2	0.194	0.895	0.222	6	26
NYY	    Chris	    Grube	    10	28	0	1	0	0	0	1	2	1	1	0	0	0	0	0	0	0.036	1.000	0.036	6	9
NYY	    Ross	    Poynor	    118	389	51	115	13	4	8	39	24	4	74	0	3	2	9	12	4	0.296	0.965	0.411	7	62
NYY	    Dan	        Field	    69	287	32	85	17	0	5	30	23	4	24	3	0	0	0	0	5	0.296	1.000	0.408	7	68
NYY	    Ramiro	    Palencia	24	64	7	11	3	0	0	2	17	0	13	1	0	0	1	1	0	0.172	1.000	0.219	7	17
NYY	    Matt	    Brettell	54	52	5	12	2	0	2	6	2	0	15	0	1	0	0	0	0	0.231	0.933	0.385	7	6
NYY	    Willie	    Morales	    20	29	3	4	2	0	1	3	2	0	7	0	0	0	0	0	0	0.138	1.000	0.310	7	4
NYY	    Micah	    MacMillan	109	445	59	110	25	10	5	36	40	2	46	7	1	1	19	10	1	0.247	0.987	0.382	8	108
NYY	    Marc	    Ash	        62	178	21	44	8	1	1	14	19	0	45	0	0	1	4	5	2	0.247	0.984	0.320	8	33
NYY	    Frank	    Meneses	    117	428	63	95	9	8	12	45	81	2	95	4	0	1	11	7	6	0.222	0.994	0.364	9	111
NYY	    Eric	    Green	    33	74	7	14	2	0	1	8	2	0	14	2	0	0	1	0	1	0.189	1.000	0.257	9	16
NYY	    Arturo	    Ganzalez	8	27	2	6	0	0	0	1	0	0	2	0	0	0	2	0	1	0.222	1.000	0.222	9	5
And the SQL:

Code:
SET @year = 1969, @team_abbr='NYY';

SELECT tm.abbr,
	plr.first_name,
    plr.last_name,
    bat.g,
    bat.ab,
    bat.r,
    bat.h,
    bat.d as 2B,
    bat.t as 3B,
    bat.hr,
    bat.rbi,
    bat.bb,
    bat.ibb,
    bat.k,
    bat.hp as hpb,
    bat.sh,
    bat.sf,
    bat.sb,
    bat.cs,
    bat.gdp,
    round(bat.h / bat.ab, 3) as ba,
    round((bat.h + bat.bb + bat.hp) / (bat.h + bat.bb + bat.hp + bat.sh + bat.sf), 3) as obp,
    round((bat.h + bat.d + 2 * bat.t + 3 * bat.hr) / ab, 3) as slg,
    fld.position,
    fld.g
FROM `modern-ish-baseball`.teams tm 
	LEFT JOIN players_career_batting_stats bat
		ON bat.team_id = tm.team_id
	LEFT JOIN players plr
		ON plr.player_id = bat.player_id
    LEFT JOIN players_career_fielding_stats fld
		ON fld.player_id = plr.player_id and fld.year = @year and bat.team_id = fld.team_id
where tm.abbr = @team_abbr
	AND bat.year = @year
    AND bat.split_id = 1
    AND fld.ip = (select max(ip) from `modern-ish-baseball`.players_career_fielding_stats f
	WHERE f.year = @year and f.team_id = tm.team_id and f.player_id = plr.player_id)
    AND fld.position > 1
ORDER BY position, bat.pa desc
You could pretty easily drop this into a stored procedure and then just input the year and team abbreviation above to get back the relevant data. Also, when I run this myself I'll probably get rid of the team abbreviation from the list and maybe add handedness info.... the point is, MySQL gives you the wherewithal to do this.
__________________
Quote:
Originally Posted by Markus Heinsohn
You bastard....
The Great American Baseball Thrift Book - Like reading the Sporting News from back in the day, only with fake players. REAL LIFE DRAMA THOUGH maybe not
Syd Thrift is offline   Reply With Quote