forked from jbader14/CIS5500-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
665 lines (637 loc) · 21.1 KB
/
routes.js
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
const { Pool, types } = require('pg');
const config = require('./config.json');
// Override the default parsing for BIGINT (PostgreSQL type ID 20)
types.setTypeParser(20, val => parseInt(val, 10)); //DO NOT DELETE THIS
// Create PostgreSQL connection using database credentials provided in config.json
// Do not edit. If the connection fails, make sure to check that config.json is filled out correctly
const connection = new Pool({
host: config.rds_host,
user: config.rds_user,
password: config.rds_password,
port: config.rds_port,
database: config.rds_db,
ssl: {
rejectUnauthorized: false,
},
});
connection.connect((err) => err && console.log(err));
// Route 1: GET /avg_passing_yds_weather/:season
const avg_passing_yds_weather = async function (req, res) {
const season = req.params.season;
connection.query(`
SELECT
CASE
WHEN w.weather ILIKE '%rain%' OR w.weather ILIKE '%shower%' OR w.weather ILIKE '%drizzle%' OR w.weather ILIKE '%wet%' THEN 'Rainy'
WHEN w.weather ILIKE '%snow%' OR w.weather ILIKE '%wintry%' OR w.weather ILIKE '%freezing%' OR w.weather ILIKE '%blizzard%' OR w.weather ILIKE '%flurries%' THEN 'Snowy'
WHEN w.weather ILIKE '%fog%' OR w.weather ILIKE '%mist%' OR w.weather ILIKE '%haze%' THEN 'Foggy'
WHEN w.weather ILIKE '%overcast%' OR w.weather ILIKE '%cloud%' OR w.weather ILIKE '%humid%' THEN 'Cloudy'
WHEN w.weather ILIKE '%clear%' OR w.weather ILIKE '%fair%' OR w.weather ILIKE '%sun%' THEN 'Clear/Sunny'
WHEN w.weather ILIKE '%wind%' OR w.weather ILIKE '%breezy%' THEN 'Windy'
ELSE 'Other'
END AS WeatherCondition,
AVG(qb.passing_yards) AS AvgPassingYards
FROM quarterback_stats qb
JOIN weather_nfl_all_games_noplayoffs w
ON qb.season = CAST(w.season AS INTEGER) AND qb.week = CAST(w.week AS INTEGER)
WHERE qb.season = ${season}
GROUP BY WeatherCondition
HAVING COUNT(*) > 5
ORDER BY AvgPassingYards DESC;
`,
(err, data) => {
if (err) {
console.error(err);
res.status(500).json({ error: 'Error: Query Failure.' });
} else {
res.json(data.rows);
}
}
);
};
// Route 2: GET /top_players/:num
const top_players = async function(req, res) {
const num = req.params.num;
connection.query(`
SELECT ws.name, p.position, SUM(ws.fantasy_points) AS total_fantasy_points
FROM weekly_stats ws
JOIN players p ON ws.name = p.name
GROUP BY ws.name, p.position
ORDER BY total_fantasy_points DESC
LIMIT '${num}'
`,
(err, data) => {
if (err) {
console.error(err);
res.status(500).json({ error: 'Error: Query Failure.' });
} else {
res.json(data.rows);
}
}
);
};
// Route 3: GET /adverse_weather_performance/:wind_speed/:limit
const adverse_weather_performance = async function (req, res) {
const windSpeed = req.params.wind_speed;
const limit = req.params.limit;
connection.query(`
WITH CategorizedGames AS (
SELECT
ws.name,
CAST(ws.season AS INTEGER) AS season,
CAST(ws.week AS INTEGER) AS week,
ws.fantasy_points,
CASE
WHEN EXISTS (
SELECT 1
FROM cachedwindygamesfinal wg
WHERE CAST(ws.season AS INTEGER) = wg.season AND CAST(ws.week AS INTEGER) = wg.week
AND wg.wind_speed >= ${windSpeed}
) THEN 'Windy'
ELSE 'Normal'
END AS Condition
FROM weekly_stats ws
),
PlayerPerformance AS (
SELECT
cg.name,
cg.Condition,
AVG(cg.fantasy_points) AS AvgFantasyPoints
FROM CategorizedGames cg
GROUP BY cg.name, cg.Condition
),
PerformanceComparison AS (
SELECT
pp.name,
MAX(CASE WHEN pp.Condition = 'Windy' THEN pp.AvgFantasyPoints ELSE NULL END) AS AvgFantasyWindy,
MAX(CASE WHEN pp.Condition = 'Normal' THEN pp.AvgFantasyPoints ELSE NULL END) AS AvgFantasyNormal
FROM PlayerPerformance pp
GROUP BY pp.name
)
SELECT
pc.name,
pc.AvgFantasyWindy,
pc.AvgFantasyNormal,
(pc.AvgFantasyWindy - pc.AvgFantasyNormal) AS PerformanceDifference
FROM PerformanceComparison pc
WHERE pc.AvgFantasyWindy > pc.AvgFantasyNormal
ORDER BY PerformanceDifference DESC
LIMIT ${limit};
`,
(err, data) => {
if (err) {
console.error(err);
res.status(500).json({ error: 'Error: Query Failure.' });
} else {
res.json(data.rows);
}
}
);
};
// Route 4: GET /adverse_weather_team_comp/:teams
const adverse_weather_team_comp = async function (req, res) {
const teams = req.params.teams;
// Convert into an team names array
const teamList = teams.split(',');
// Wrap single quotes around teams for IN clause
const teamsCompare = teamList.map(team => `'${team}'`).join(',');
connection.query(`
WITH AdverseWeather AS (
SELECT DISTINCT
CAST(w.season AS INTEGER) AS season,
CAST(w.week AS INTEGER) AS week,
w."Home Team",
w."Away Team",
CASE
WHEN w.weather IN ('Rain', 'Snow') THEN 'Rain/Snow'
WHEN w.temperature SIMILAR TO '[0-9]+ F' AND CAST(SPLIT_PART(w.temperature, ' ', 1) AS INTEGER) < 40 THEN 'Cold'
WHEN w.wind SIMILAR TO '[0-9]+' AND CAST(w.wind AS INTEGER) > 20 THEN 'Windy'
ELSE 'Normal'
END AS WeatherCondition
FROM weather_nfl_all_games_noplayoffs w
WHERE w.weather IS NOT NULL
AND w.temperature SIMILAR TO '[0-9]+ F'
AND w.wind SIMILAR TO '[0-9]+'
),
GameResults AS (
SELECT
CAST(w.season AS INTEGER) AS season,
CAST(w.week AS INTEGER) AS week,
w."Home Team" AS Team,
(w."Home Team Score" - w."Away Team Score") AS Margin,
COALESCE(aw.WeatherCondition, 'Normal') AS WeatherCondition
FROM weather_nfl_all_games_noplayoffs w
LEFT JOIN AdverseWeather aw
ON CAST(w.season AS INTEGER) = aw.season
AND CAST(w.week AS INTEGER) = aw.week
AND w."Home Team" = aw."Home Team"
UNION ALL
SELECT
CAST(w.season AS INTEGER) AS season,
CAST(w.week AS INTEGER) AS week,
w."Away Team" AS Team,
(w."Away Team Score" - w."Home Team Score") AS Margin,
COALESCE(aw.WeatherCondition, 'Normal') AS WeatherCondition
FROM weather_nfl_all_games_noplayoffs w
LEFT JOIN AdverseWeather aw
ON CAST(w.season AS INTEGER) = aw.season
AND CAST(w.week AS INTEGER) = aw.week
AND w."Away Team" = aw."Away Team"
),
TeamPerformanceByWeather AS (
SELECT
gr.Team,
gr.WeatherCondition,
AVG(gr.Margin) AS AvgMarginOfVictory
FROM GameResults gr
WHERE gr.Team IN (${teamsCompare})
GROUP BY gr.Team, gr.WeatherCondition
),
PerformanceComparison AS (
SELECT
tp.Team,
COALESCE(MAX(CASE WHEN tp.WeatherCondition = 'Rain/Snow' THEN tp.AvgMarginOfVictory ELSE NULL END), 0) AS AvgMarginRainSnow,
COALESCE(MAX(CASE WHEN tp.WeatherCondition = 'Cold' THEN tp.AvgMarginOfVictory ELSE NULL END), 0) AS AvgMarginCold,
COALESCE(MAX(CASE WHEN tp.WeatherCondition = 'Windy' THEN tp.AvgMarginOfVictory ELSE NULL END), 0) AS AvgMarginWindy,
COALESCE(MAX(CASE WHEN tp.WeatherCondition = 'Normal' THEN tp.AvgMarginOfVictory ELSE NULL END), 0) AS AvgMarginNormal
FROM TeamPerformanceByWeather tp
GROUP BY tp.Team
)
SELECT
pc.Team,
pc.AvgMarginRainSnow,
pc.AvgMarginCold,
pc.AvgMarginWindy,
pc.AvgMarginNormal,
(GREATEST(
COALESCE(pc.AvgMarginRainSnow, 0),
COALESCE(pc.AvgMarginCold, 0),
COALESCE(pc.AvgMarginWindy, 0)
) - pc.AvgMarginNormal) AS PerformanceDifference
FROM PerformanceComparison pc
WHERE pc.AvgMarginNormal > 0
ORDER BY PerformanceDifference DESC
LIMIT 5;
`,
(err, data) => {
if (err) {
console.error(err);
res.status(500).json({ error: 'Error: Query Failure.' });
} else {
res.json(data.rows);
}
}
);
};
// Route 5: GET /cold_weather_qbs/:min_games
const cold_weather_qbs = async function (req, res) {
const minGames = req.params.min_games;
connection.query(`
SELECT
ws.Name AS player_name,
COUNT(*) AS games_played,
AVG(qb.passer_rating) AS avg_rating,
AVG(qb.passing_yards) AS avg_passing_yards,
AVG(qb.passing_tds) AS avg_TDs
FROM weekly_stats ws
JOIN quarterback_stats qb
ON ws.Name = qb.Name
AND CAST(ws.Season AS TEXT) ~ '^[0-9]+$' AND CAST(qb.Season AS TEXT) ~ '^[0-9]+$'
AND CAST(ws.Season AS INTEGER) = CAST(qb.Season AS INTEGER)
AND CAST(ws.Week AS TEXT) ~ '^[0-9]+$' AND CAST(qb.Week AS TEXT) ~ '^[0-9]+$'
AND CAST(ws.Week AS INTEGER) = CAST(qb.Week AS INTEGER)
JOIN weather w
ON CAST(ws.Season AS TEXT) ~ '^[0-9]+$' AND CAST(w.Season AS TEXT) ~ '^[0-9]+$'
AND CAST(ws.Season AS INTEGER) = CAST(w.Season AS INTEGER)
AND CAST(ws.Week AS TEXT) ~ '^[0-9]+$' AND CAST(w.Week AS TEXT) ~ '^[0-9]+$'
AND CAST(ws.Week AS INTEGER) = CAST(w.Week AS INTEGER)
WHERE w.Temperature ~ '^[0-9]+'
AND CAST(REGEXP_REPLACE(w.Temperature, '[^0-9]', '', 'g') AS INTEGER) < 32
GROUP BY ws.Name
HAVING COUNT(*) >= ${minGames}
ORDER BY avg_rating DESC;
`,
(err, data) => {
if (err) {
console.error(err);
res.status(500).json({ error: 'Error: Query Failure.' });
} else {
res.json(data.rows);
}
}
);
};
// Route 6: GET /goal_line_backs/:min_tds/:min_games
const goal_line_backs = async function (req, res) {
const minTds = req.params.min_tds;
const minGames = req.params.min_games;
connection.query(`
SELECT
ws.Name AS player_name,
ws.Season AS season,
COUNT(*) AS games_played,
SUM(rb.rushing_tds) AS TD_count,
ROUND(AVG(rb.rushing_yards)::NUMERIC, 1) AS avg_rushing_yards,
ROUND((SUM(rb.rushing_tds)::FLOAT / NULLIF(SUM(rb.rushing_yards), 0) * 100)::NUMERIC, 2) AS TED_100_yard
FROM weekly_stats ws
JOIN runningback_stats rb ON ws.Name = rb.Name
AND ws.Season = rb.Season
AND ws.Week = rb.Week
WHERE rb.Carries >= 3
GROUP BY ws.Name, ws.Season
HAVING COUNT(*) >= ${minGames}
AND SUM(rb.rushing_tds) >= ${minTds}
ORDER BY td_per_100_yards DESC;
`,
(err, data) => {
if (err) {
console.error(err);
res.status(500).json({ error: 'Error: Query Failure.' });
} else {
res.json(data.rows);
}
}
);
};
// Route 7: GET /consistent_scorers/:position
const consistent_scorers = async function (req, res) {
const position = req.query.position;
connection.query(`
SELECT
ws.Name AS player_name,
p.Position,
COUNT(*) AS games_played,
ROUND(AVG(ws.fantasy_points_ppr), 1) AS avg_points,
ROUND(STDDEV(ws.fantasy_points_ppr), 2) AS point_variability,
ROUND(MIN(ws.fantasy_points_ppr), 1) AS lowest_score,
ROUND(MAX(ws.fantasy_points_ppr), 1) AS highest_score
FROM weekly_stats ws
JOIN players p ON ws.Name = p.name
WHERE p.Position = '${position}'
GROUP BY ws.Name, p.Position
HAVING COUNT(*) >= 10
ORDER BY
p.Position,
point_variability ASC,
avg_points DESC;
`,
(err, data) => {
if (err) {
console.error(err);
res.status(500).json({ error: 'Error: Query Failure.' });
} else {
res.json(data.rows);
}
}
);
};
// Route 8: GET /injury_resilience/:position
const injury_resilience = async function (req, res) {
const position = req.params.position;
connection.query(`
SELECT
ws.Name AS player_name,
p.Position,
COUNT(DISTINCT ws.Season) AS seasons_played,
COUNT(DISTINCT CONCAT(ws.Season, '-', ws.Week)) AS total_games,
COUNT(DISTINCT CONCAT(i.Season, '-', i.Week)) AS total_injuries,
ROUND(
(COUNT(DISTINCT CONCAT(i.Season, '-', i.Week))::FLOAT /
NULLIF(COUNT(DISTINCT ws.Season), 0))::NUMERIC,
2
) AS injuries_per_season
FROM weekly_stats ws
JOIN players p ON ws.Name = p.name
LEFT JOIN Injuries i ON ws.Name = i.player
${position ? `WHERE p.Position = '${position}'` : ''}
GROUP BY ws.Name, p.Position
HAVING COUNT(DISTINCT CONCAT(ws.Season, '-', ws.Week)) >= 16
ORDER BY injuries_per_season DESC, total_injuries DESC;
`,
(err, data) => {
if (err) {
console.error(err);
res.status(500).json({ error: 'Error: Query Failure.' });
} else {
res.json(data.rows);
}
}
);
};
// Route 9: GET /player_performance_tiers/:position
const player_performance_tiers = async function (req, res) {
const position = req.params.position;
connection.query(`
WITH PlayerStatVal AS (
SELECT
ws.Name AS player_name,
ws.Season,
ws.Position,
AVG(ws.fantasy_points) AS avg_fantasy_points,
SUM(ws.fantasy_points) AS total_fantasy_points
FROM weekly_stats ws
GROUP BY ws.Name, ws.Season, ws.Position
),
PerformanceEachPos AS (
SELECT
qb.name,
qb.season,
qb.week,
'QB' AS position,
(qb.passing_yards * 0.04 + qb.passing_tds * 4 + qb.rushing_yards * 0.1 +
qb.rushing_tds * 6 - qb.interceptions * 2) AS scoreval
FROM quarterback_stats qb
UNION ALL
SELECT
rb.name,
rb.season,
rb.week,
'RB' AS position,
(rb.rushing_yards * 0.1 + rb.rushing_tds * 6 + rb.receiving_yards * 0.1 +
rb.receiving_tds * 6) AS scoreval
FROM runningback_stats rb
UNION ALL
SELECT
wo.name,
wo.season,
wo.week,
'WR' AS position,
(wo.receiving_yards * 0.1 + wo.receiving_tds * 6) AS scoreval
FROM wideout_stats wo
),
PerformanceDeviation AS (
SELECT
pe.name,
pe.season,
pe.position,
AVG(pe.scoreval) AS avg_performance,
STDDEV(pe.scoreval) AS performance_stddev
FROM PerformanceEachPos pe
GROUP BY pe.name, pe.season, pe.position
),
PlayerLevelRat AS (
SELECT
pd.name,
pd.season,
pd.position,
pd.avg_performance,
pd.performance_stddev,
psv.avg_fantasy_points,
CASE pd.position
WHEN 'QB' THEN
CASE
WHEN pd.avg_performance > 24 THEN 'Elite'
WHEN pd.avg_performance > 20 THEN 'Above Average'
WHEN pd.avg_performance > 15 THEN 'Average'
ELSE 'Below Average'
END
WHEN 'RB' THEN
CASE
WHEN pd.avg_performance > 18 THEN 'Elite'
WHEN pd.avg_performance > 14 THEN 'Above Average'
WHEN pd.avg_performance > 10 THEN 'Average'
ELSE 'Below Average'
END
WHEN 'WR' THEN
CASE
WHEN pd.avg_performance > 15 THEN 'Elite'
WHEN pd.avg_performance > 12 THEN 'Above Average'
WHEN pd.avg_performance > 8 THEN 'Average'
ELSE 'Below Average'
END
END AS performance_tier
FROM PerformanceDeviation pd
LEFT JOIN PlayerStatVal psv
ON pd.name = psv.player_name
AND pd.season = psv.Season
),
TopPlayers AS (
SELECT *
FROM PlayerLevelRat
ORDER BY avg_performance DESC
LIMIT 1100
),
BetteringPlayer AS (
SELECT
name,
season,
position,
avg_performance,
performance_tier,
LAG(avg_performance) OVER (
PARTITION BY name
ORDER BY season
) AS prev_performance
FROM PlayerLevelRat
),
Combining AS (
SELECT
pt.performance_tier,
pt.position,
AVG(pt.avg_performance) AS group_avg_performance,
COUNT(*) AS player_count
FROM TopPlayers pt
GROUP BY pt.performance_tier, pt.position
)
SELECT
plt.performance_tier,
plt.position,
COUNT(*) AS player_count,
ROUND(AVG(plt.avg_performance)::numeric, 2) AS avg_tier_performance,
ROUND(AVG(plt.performance_stddev)::numeric, 2) AS avg_performance_volatility,
ROUND((AVG(plt.avg_performance) * COALESCE(cm.group_avg_performance, AVG(plt.avg_performance)))::numeric, 2) AS cross_calc,
COALESCE(cm.player_count, COUNT(*)) AS comparison_count,
ROUND(AVG(plt.avg_fantasy_points)::numeric, 2) AS avg_fantasy_points,
STRING_AGG(DISTINCT CASE
WHEN bp.avg_performance > bp.prev_performance THEN bp.name
END, ', ') AS improved_players
FROM PlayerLevelRat plt
LEFT JOIN Combining cm
ON plt.performance_tier = cm.performance_tier
AND plt.position = cm.position
LEFT JOIN BetteringPlayer bp
ON plt.name = bp.name
AND plt.season = bp.season
WHERE plt.position = '${position}'
AND CAST(plt.season AS INTEGER) >= 2018
GROUP BY
plt.performance_tier,
plt.position,
cm.group_avg_performance,
cm.player_count
HAVING COUNT(*) > 5
ORDER BY
plt.position,
CASE
WHEN plt.performance_tier = 'Elite' THEN 1
WHEN plt.performance_tier = 'Above Average' THEN 2
WHEN plt.performance_tier = 'Average' THEN 3
ELSE 4
END;
`,
(err, data) => {
if (err) {
console.error(err);
res.status(500).json({ error: 'Error: Query Failure.' });
} else {
res.json(data.rows);
}
}
);
};
// Route 10: GET /injury_followup_probability/:number
const injury_followup_probability = async function (req, res) {
const windowNumber = req.params.min_seasons;
connection.query(`
WITH RECURSIVE mover AS (
SELECT 1 AS n
UNION ALL
SELECT n + 1 FROM mover WHERE n < ${windowNumber}
),
weekslookat AS (
SELECT
n - 2 AS acceptablerange
FROM mover
WHERE n <= 5
),
injurytimes AS (
SELECT DISTINCT
i.player,
i.Season::text AS season,
i.Week,
EXISTS (
SELECT 1
FROM mv_foggy_conditions fc
CROSS JOIN weekslookat ranging
WHERE fc.season = i.Season::text
AND CAST(fc.week AS INTEGER) = i.Week + ranging.acceptablerange
AND (i.Week + ranging.acceptablerange) >= 1
AND (i.Week + ranging.acceptablerange) <= 18
) AS rainytime
FROM injuries i
WHERE i.Week IS NOT NULL
),
valid_injuries AS (
SELECT
i.player,
i.Season::text AS season,
i.Week
FROM injuries i
JOIN injurytimes it ON
i.player = it.player AND
i.Season::text = it.season AND
i.Week = it.Week
WHERE i.game_status = 'Out'
AND it.rainytime = TRUE
),
playerswithi AS (
SELECT
i.player,
COUNT(DISTINCT i.Season) AS injury_num,
STRING_AGG(DISTINCT p.position, ',' ORDER BY p.position) AS positions
FROM valid_injuries i
LEFT JOIN players p ON i.player = p.name
GROUP BY i.player
),
following_injuries AS (
SELECT
i1.player,
COUNT(DISTINCT i2.Season) AS following_injury_num
FROM valid_injuries i1
JOIN valid_injuries i2 ON i1.player = i2.player
AND (i2.season > i1.season
OR (i2.season = i1.season AND i2.Week > i1.Week))
GROUP BY i1.player
),
prob_calcs AS (
SELECT
(SELECT COUNT(DISTINCT name) FROM players) AS player_number,
(
SELECT COUNT(*)
FROM playerswithi pi
WHERE injury_num > 0
AND EXISTS (
SELECT 1
FROM mv_foggy_conditions ws
WHERE ws.season IN (
SELECT DISTINCT season
FROM valid_injuries vi
WHERE vi.player = pi.player
)
)
) AS injured_number,
(
SELECT COUNT(*)
FROM following_injuries
WHERE following_injury_num > 0
) AS injured_again
)
SELECT
player_number,
injured_number,
injured_again,
CAST(injured_number AS FLOAT) / NULLIF(player_number, 0) AS injury_prob,
CAST(injured_again AS FLOAT) / NULLIF(injured_number, 0) AS another_injury_prob
FROM prob_calcs;
`,
(err, data) => {
if (err) {
console.error(err);
res.status(500).json({ error: 'Error: Query Failure.' });
} else {
res.json(data.rows);
}
}
);
};
module.exports = {
avg_passing_yds_weather,
top_players,
adverse_weather_performance,
adverse_weather_team_comp,
cold_weather_qbs,
goal_line_backs,
consistent_scorers,
injury_resilience,
player_performance_tiers,
injury_followup_probability,
}