-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpitching.sql
304 lines (293 loc) · 11.1 KB
/
pitching.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
--Query the pitcher names
SELECT master.namefirst, master.namelast, pitching.*
FROM baseball.pitching AS pitching
JOIN baseball.master AS master
ON master.playerid = pitching.playerid
WHERE pitching.yearid >= 1871
AND pitching.yearid <= 2016
--Query the top starting pitchers within a given date range by average ERA and
--innings pitched per year meets ERA championship qualifier
SELECT eraavg.avgera, pitching.fname, pitching.lname, pitching.playerid, eraavg.avgipouts as average_outs_per_year FROM
(SELECT master.namefirst as fname, master.namelast as lname, pitching.*
FROM baseball.pitching AS pitching
JOIN baseball.master AS master
ON master.playerid = pitching.playerid
) as pitching
JOIN
(SELECT
avg(pitching.era) as avgera,
pitching.playerid as idavg,
avg(pitching.ipouts) as avgipouts
FROM baseball.pitching
WHERE pitching.yearid >= 2015
AND pitching.yearid <= 2016
GROUP BY idavg) as eraavg
ON eraavg.idavg = pitching.playerid
AND eraavg.avgipouts > 486
GROUP BY eraavg.avgera, pitching.fname, pitching.lname, pitching.playerid, average_outs_per_year, eraavg.avgwins
ORDER BY eraavg.avgera ASC;
--Query the top starting pitchers by ERA
SELECT eraavg.avgera, pitching.fname, pitching.lname,
pitching.playerid, eraavg.avgipouts as average_outs_per_year, eraavg.avgwins
FROM
(SELECT master.namefirst as fname, master.namelast as lname, pitching.*
FROM baseball.pitching AS pitching
JOIN baseball.master AS master
ON master.playerid = pitching.playerid
) as pitching
JOIN
(SELECT
avg(pitching.era) as avgera,
pitching.playerid as idavg,
avg(pitching.ipouts) as avgipouts,
sum(pitching.w) as avgwins
FROM baseball.pitching
WHERE pitching.yearid >= 1871
AND pitching.yearid <= 2016
GROUP BY idavg) as eraavg
ON eraavg.idavg = pitching.playerid
JOIN baseball.fielding
ON baseball.fielding.playerid = eraavg.idavg
WHERE eraavg.avgipouts > 486
AND gs > 10
GROUP BY eraavg.avgera, pitching.fname, pitching.lname,
pitching.playerid, average_outs_per_year, eraavg.avgwins
ORDER BY eraavg.avgera ASC;
--Query the top starting pitchers by ERA, over 559 outs per year
SELECT CAST(eraavg.avgera as DECIMAL(5,3)), pitcher_names.fname, pitcher_names.lname,
pitcher_names.playerid, eraavg.avgipouts as average_outs_per_year, eraavg.totalwins, CAST(eraavg.avgwhip as DECIMAL(5,3))
FROM
pitcher_names
JOIN
(SELECT
avg(pitching.era) as avgera,
pitching.playerid as idavg,
avg(pitching.ipouts) as avgipouts,
sum(pitching.w) as totalwins,
avg(pitching.w) as avgwins,
avg(pitching.whip) as avgwhip
FROM baseball.pitching
WHERE pitching.yearid >= 1871
AND pitching.yearid <= 2016
GROUP BY idavg) as eraavg
ON eraavg.idavg = pitcher_names.playerid
WHERE eraavg.avgipouts > 659-100
GROUP BY eraavg.avgera, pitcher_names.fname, pitcher_names.lname,
pitcher_names.playerid, average_outs_per_year, eraavg.totalwins, eraavg.avgwhip
ORDER BY eraavg.avgera ASC
LIMIT 5;
--Same query as above ready to be inserted into index.js
SELECT CAST(eraavg.avgera as DECIMAL(5,3)), pitcher_names.fname, pitcher_names.lname,
pitcher_names.playerid, eraavg.avgipouts as average_outs_per_year, eraavg.totalwins, CAST(eraavg.avgwhip as DECIMAL(5,3))
FROM
pitcher_names
JOIN
(SELECT
avg(pitching.era) as avgera,
pitching.playerid as idavg,
avg(pitching.ipouts) as avgipouts,
sum(pitching.w) as totalwins,
avg(pitching.w) as avgwins,
avg(pitching.whip) as avgwhip
FROM baseball.pitching
WHERE pitching.yearid >= ${minYear}
AND pitching.yearid <= ${maxYear}
GROUP BY idavg) as eraavg
ON eraavg.idavg = pitcher_names.playerid
WHERE eraavg.avgipouts > ${ipFloor}
GROUP BY eraavg.avgera, pitcher_names.fname, pitcher_names.lname,
pitcher_names.playerid, average_outs_per_year, eraavg.totalwins, eraavg.avgwhip
ORDER BY eraavg.avgera ASC
LIMIT ${limit};
--same as above except by WHIP
SELECT CAST(eraavg.avgera as DECIMAL(5,3)), pitcher_names.fname, pitcher_names.lname,
pitcher_names.playerid, eraavg.avgipouts as average_outs_per_year, eraavg.totalwins, CAST(eraavg.avgwhip as DECIMAL(5,3))
FROM
pitcher_names
JOIN
(SELECT
avg(pitching.era) as avgera,
pitching.playerid as idavg,
avg(pitching.ipouts) as avgipouts,
sum(pitching.w) as totalwins,
avg(pitching.w) as avgwins,
avg(pitching.whip) as avgwhip
FROM baseball.pitching
WHERE pitching.yearid >= ${minYear}
AND pitching.yearid <= ${maxYear}
GROUP BY idavg) as eraavg
ON eraavg.idavg = pitcher_names.playerid
WHERE eraavg.avgipouts > ${ipFloor}
GROUP BY eraavg.avgera, pitcher_names.fname, pitcher_names.lname,
pitcher_names.playerid, average_outs_per_year, eraavg.totalwins, eraavg.avgwhip
ORDER BY eraavg.avgwhip ASC
LIMIT ${limit};
--Generate the ipouts/wins threshhold by year for starters
SELECT avg(w) as avgwins, avg(ipouts) as avgipouts FROM baseball.pitching WHERE ipouts > 486 AND pitching.yearid >= 2000 AND pitching.yearid <= 2016;
--Query to find bullpen by saves
SELECT CAST(eraavg.avgera as DECIMAL(5,3)), pitcher_names.fname, pitcher_names.lname,
pitcher_names.playerid, CAST(eraavg.avgwhip as DECIMAL(5,3)),
eraavg.totalsaves, eraavg.avgsaves, eraavg.totalso
FROM
pitcher_names
JOIN
(SELECT
avg(pitching.era) as avgera,
pitching.playerid as idavg,
avg(pitching.ipouts) as avgipouts,
sum(pitching.sv) as totalsaves,
avg(pitching.sv) as avgsaves,
avg(pitching.whip) as avgwhip,
sum(pitching.so) as totalso
FROM baseball.pitching
WHERE pitching.yearid >= 1871
AND pitching.yearid <= 1930
GROUP BY idavg) as eraavg
ON eraavg.idavg = pitcher_names.playerid
WHERE eraavg.avgwhip < 2
GROUP BY eraavg.avgera, pitcher_names.fname, pitcher_names.lname,
pitcher_names.playerid, eraavg.avgwhip, eraavg.totalsaves, eraavg.avgsaves, eraavg.totalso
ORDER BY ROUND(totalsaves, -1) desc, eraavg.avgwhip asc
LIMIT 5;
--Same query as above ready to be inserted into index.js
SELECT CAST(eraavg.avgera as DECIMAL(5,3)), pitcher_names.fname, pitcher_names.lname,
pitcher_names.playerid, CAST(eraavg.avgwhip as DECIMAL(5,3)),
eraavg.totalsaves, eraavg.avgsaves, eraavg.totalso
FROM
pitcher_names
JOIN
(SELECT
avg(pitching.era) as avgera,
pitching.playerid as idavg,
avg(pitching.ipouts) as avgipouts,
sum(pitching.sv) as totalsaves,
avg(pitching.sv) as avgsaves,
avg(pitching.whip) as avgwhip,
sum(pitching.so) as totalso
FROM baseball.pitching
WHERE pitching.yearid >= ${minYear}
AND pitching.yearid <= ${maxYear}
GROUP BY idavg) as eraavg
ON eraavg.idavg = pitcher_names.playerid
WHERE eraavg.avgwhip < 2
GROUP BY eraavg.avgera, pitcher_names.fname, pitcher_names.lname,
pitcher_names.playerid, eraavg.avgwhip, eraavg.totalsaves, eraavg.avgsaves, eraavg.totalso
ORDER BY ROUND(totalsaves, -1) desc, eraavg.avgwhip asc
LIMIT 5;
--Bullpen players by WHIP
SELECT CAST(eraavg.avgera as DECIMAL(5,3)), pitcher_names.fname, pitcher_names.lname,
pitcher_names.playerid, CAST(eraavg.avgwhip as DECIMAL(5,3)),
eraavg.totalsaves, eraavg.avgsaves, eraavg.totalso, eraavg.avgwins
FROM
pitcher_names
JOIN
(SELECT
avg(pitching.era) as avgera,
pitching.playerid as idavg,
sum(pitching.sv) as totalsaves,
avg(pitching.sv) as avgsaves,
avg(pitching.whip) as avgwhip,
sum(pitching.so) as totalso,
avg(pitching.w) as avgwins,
sum(pitching.g) as totalgames
FROM baseball.pitching
WHERE pitching.yearid >= 1871
AND pitching.yearid <= 2016
AND pitching.g > 10
GROUP BY idavg) as eraavg
ON eraavg.idavg = pitcher_names.playerid
WHERE eraavg.avgwhip < 2
AND avgwins <12
AND totalgames > 100
GROUP BY eraavg.avgera, pitcher_names.fname, pitcher_names.lname,
pitcher_names.playerid, eraavg.avgwhip, eraavg.totalsaves, eraavg.avgsaves, eraavg.totalso, eraavg.avgwins
ORDER BY eraavg.avgwhip asc
LIMIT 5;
--Same query with parameters
SELECT CAST(eraavg.avgera as DECIMAL(5,3)), pitcher_names.fname, pitcher_names.lname,
pitcher_names.playerid, CAST(eraavg.avgwhip as DECIMAL(5,3)),
eraavg.totalsaves, eraavg.avgsaves, eraavg.totalso, eraavg.avgwins
FROM
pitcher_names
JOIN
(SELECT
avg(pitching.era) as avgera,
pitching.playerid as idavg,
sum(pitching.sv) as totalsaves,
avg(pitching.sv) as avgsaves,
avg(pitching.whip) as avgwhip,
sum(pitching.so) as totalso,
avg(pitching.w) as avgwins,
sum(pitching.g) as totalgames
FROM baseball.pitching
WHERE pitching.yearid >= ${minYear}
AND pitching.yearid <= ${maxYear}
AND pitching.g > 10
GROUP BY idavg) as eraavg
ON eraavg.idavg = pitcher_names.playerid
WHERE eraavg.avgwhip < 2
AND avgwins < 12
AND totalgames > 100
GROUP BY eraavg.avgera, pitcher_names.fname, pitcher_names.lname,
pitcher_names.playerid, eraavg.avgwhip, eraavg.totalsaves, eraavg.avgsaves, eraavg.totalso, eraavg.avgwins
ORDER BY eraavg.avgwhip asc
LIMIT ${limit};
--Bullpen ERA with parameters
SELECT CAST(eraavg.avgera as DECIMAL(5,3)), pitcher_names.fname, pitcher_names.lname,
pitcher_names.playerid, CAST(eraavg.avgwhip as DECIMAL(5,3)),
eraavg.totalsaves, eraavg.avgsaves, eraavg.totalso, eraavg.avgwins
FROM
pitcher_names
JOIN
(SELECT
avg(pitching.era) as avgera,
pitching.playerid as idavg,
sum(pitching.sv) as totalsaves,
avg(pitching.sv) as avgsaves,
avg(pitching.whip) as avgwhip,
sum(pitching.so) as totalso,
avg(pitching.w) as avgwins,
sum(pitching.g) as totalgames
FROM baseball.pitching
WHERE pitching.yearid >= ${minYear}
AND pitching.yearid <= ${maxYear}
AND pitching.g > 10
GROUP BY idavg) as eraavg
ON eraavg.idavg = pitcher_names.playerid
WHERE eraavg.avgwhip < 2
AND avgwins < 12
AND totalgames > 100
GROUP BY eraavg.avgera, pitcher_names.fname, pitcher_names.lname,
pitcher_names.playerid, eraavg.avgwhip, eraavg.totalsaves, eraavg.avgsaves, eraavg.totalso, eraavg.avgwins
ORDER BY eraavg.avgera asc
LIMIT ${limit};
--Bullpen by K/innings, over 100 K
SELECT pitcher_names.fname, pitcher_names.lname,
pitcher_names.playerid, CAST(eraavg.avgwhip as DECIMAL(5,3)),
eraavg.totalsaves, eraavg.avgsaves, eraavg.totalso, eraavg.avgwins, eraavg.soperip
FROM
pitcher_names
JOIN
(SELECT
sum(pitching.so)/sum(pitching.real_ip) as soperip,
pitching.playerid as idavg,
sum(pitching.sv) as totalsaves,
avg(pitching.sv) as avgsaves,
avg(pitching.whip) as avgwhip,
sum(pitching.so) as totalso,
avg(pitching.w) as avgwins,
sum(pitching.g) as totalgames
FROM baseball.pitching
WHERE pitching.yearid >= ${minYear}
AND pitching.yearid <= ${maxYear}
AND pitching.g > 10
GROUP BY idavg) as eraavg
ON eraavg.idavg = pitcher_names.playerid
WHERE eraavg.avgwhip < 2
AND avgwins <10
AND totalgames > 30
AND totalso > 100
GROUP BY pitcher_names.fname, pitcher_names.lname,
pitcher_names.playerid, eraavg.avgwhip, eraavg.totalsaves, eraavg.avgsaves, eraavg.totalso, eraavg.avgwins,eraavg.soperip
ORDER BY eraavg.soperip desc
LIMIT ${limit};