-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrosheet_init_db.py
executable file
·1997 lines (1842 loc) · 60.7 KB
/
retrosheet_init_db.py
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
import sys
import os
allTables = dict()
allViews = dict()
#"aardd001","Aardsma","David Allan","David","12/27/1981","Denver","Colorado","USA","04/06/2004","08/23/2015",,,,,,,,,,,"R","R","6-05",200,,,,,,,,,
allTables["player"]="""CREATE TABLE player (
player_num_id smallint PRIMARY KEY,
player_id char(8),
name_last char(64) NOT NULL,
name_first char(64) NOT NULL,
name_other char(64) NOT NULL,
birth char(12),
birth_city char(64),
birth_state char(64),
birth_country char(64),
debut char(12),
last_game char(12),
manager_debut char(12),
manager_last_game char(12),
coach_debut char(12),
coach_last_game char(12),
ump_debut char(12),
ump_last_game char(12),
death char(12),
death_city char(64),
death_state char(64),
death_country char(64),
bats char(1),
throws char(1),
height char(8),
weight smallint,
cemetary char(64),
cemetary_city char(64),
cemetary_state char(64),
cemetary_country char(64),
cemetary_note char(64),
birth_name char(64),
name_change char(64),
bat_change char(1),
hall_of_fame char(3)
) """ # WITHOUT ROWID
allTables["park"]="""CREATE TABLE park (
park_id char(5) PRIMARY KEY,
park_name varchar(64),
park_aka varchar(64),
park_city varchar(64),
park_state varchar(4),
park_open char(12),
park_close char(12),
park_league varchar(64),
notes varchar(128)
)"""
allTables["team"]="""CREATE TABLE team (
team_id char(3) PRIMARY KEY,
team_league varchar(64),
team_city varchar(64),
team_nickname varchar(64),
team_first smallint,
team_last smallint
)"""
allTables["umpire"] = """CREATE TABLE umpire (
ump_num_id smallint PRIMARY KEY,
ump_id char(8),
ump_name_last char(64),
ump_name_first char(64)
)"""
allTables["scorer"] = """CREATE TABLE scorer (
scorer_num_id smallint PRIMARY KEY,
scorer_id char(8),
scorer_name char(64)
)"""
# cond bits
# 0 : day (0) night(1)
# 1-3: field cond: 000 unknown 001 dry, 010 damp 011 wet 100 Soaked,
# 4-6: precip: 000 U=unknown; 001 N=none, 010 Z=drizzle 011 R=rain, 100 S=showers, 101 W=snow
# 7-9: sky: 000 U=unknown; 001 S=sunny 010 C=cloudy, 011 D=dome, 100 N=night, 101 O=overcast
# 10-13: winddir = 0000 U=unknown. 0001 FC=fromcf, 0010 FL=fromlf, 0011 FR=fromrf,
# 0100 LR=ltor, 0101 RT=rtol, 0110 TC=tocf, 0111 TL=tolf, 1000 TR=torf,
# 14-20: windspeed+1, 0= unknnwn value
# 21-27: temp: 7 bits (0-128) 0 = unknown value Temperature is in degrees Fahrenheit with 0 being the not known value.
# attendance value=0 for 1st game of double-headers also
#flags
# 0: has_pitch_cnt
# 1: has_pitch_seq
# 2: htbf
# 3: use_dh
# 4-5: tiebreakbase, e.g. 2
# 6: completed
# 7-8: forfeit
# 00 = none
# 01 "V" -- the game was forfeited to the visiting team
# 10 "H" -- the game was forfeited to the home team
# 11 "T" -- the game was ruled a no-decision
# 9-11: protest
# 000 = none
# 001 "P" -- the game was protested by an unidentified team
# 010 "V" -- a disallowed protest was made by the visiting team
# 011 "H" -- a disallowed protest was made by the home team
# 100 "X" -- an upheld protest was made by the visiting team
# 101 "Y" -- an upheld protest was made by the home team
# game_type_num
# 0-2: 0=regular, 1= post, 2 (10)=pre, 3 (11) other exhibition
# 3-5: postseason type: 0=not postseason 1:wildcard, 2=division, 3=league champsionship, 4=world series
# 6-7: 0=single game (not double header), 1=game 1 of DH, 2=game 2 of DH, 3=game 3 of TH
# 8-11: innings_sched
# store year/month as separate fields for easier search/indexing
allTables["game_info"]="""CREATE TABLE game_info (
game_id integer PRIMARY KEY,
game_date char(12) NOT NULL,
year smallint NOT NULL,
month smallint NOT NULL,
dow smallint,
start_time smallint,
game_type_num smallint NOT NULL,
away_team char(3) NOT NULL,
away_league char(3),
away_division char(8),
away_game_num smallint,
home_team char(3) NOT NULL,
home_league char(3),
home_division char(8),
home_game_num smallint,
flags smallint NOT NULL,
park char(5) NOT NULL,
attendance integer,
cond integer,
game_duration_min smallint,
scorer_num_id smallint,
ump_home_num_id smallint,
ump_1b_num_id smallint,
ump_2b_num_id smallint,
ump_3b_num_id smallint,
ump_lf_num_id smallint,
ump_rf_num_id smallint,
win_pitcher_num_id smallint,
loss_pitcher_num_id smallint,
save_pitcher_num_id smallint,
gw_rbi_num_id smallint,
FOREIGN KEY(win_pitcher_num_id) REFERENCES player(player_num_id),
FOREIGN KEY(loss_pitcher_num_id) REFERENCES player(player_num_id),
FOREIGN KEY(save_pitcher_num_id) REFERENCES player(player_num_id),
FOREIGN KEY(gw_rbi_num_id) REFERENCES player(player_num_id),
FOREIGN KEY(scorer_num_id) REFERENCES scorer(scorer_num_id),
FOREIGN KEY(ump_home_num_id) REFERENCES ump(ump_num_id),
FOREIGN KEY(ump_1b_num_id) REFERENCES ump(ump_num_id),
FOREIGN KEY(ump_2b_num_id) REFERENCES ump(ump_num_id),
FOREIGN KEY(ump_3b_num_id) REFERENCES ump(ump_num_id),
FOREIGN KEY(ump_lf_num_id) REFERENCES ump(ump_num_id),
FOREIGN KEY(ump_rf_num_id) REFERENCES ump(ump_num_id),
FOREIGN KEY(park) REFERENCES park(park_id),
FOREIGN KEY(away_team) REFERENCES team(team_id),
FOREIGN KEY(home_team) REFERENCES team(team_id)
)
"""
# WITHOUT ROWID
allTables["gamelog"] = """CREATE TABLE gamelog (
game_id integer PRIMARY KEY,
away_score smallint NOT NULL,
home_score smallint NOT NULL,
game_len_outs smallint NOT NULL,
away_at_bats smallint,
away_hits smallint,
away_doubles smallint,
away_triples smallint,
away_home_runs smallint,
away_rbi smallint,
away_sac_hit smallint,
away_sac_fly smallint,
away_hit_by_pitch smallint,
away_walks smallint,
away_int_walks smallint,
away_strikeouts smallint,
away_stolen_bases smallint,
away_caught_stealing smallint,
away_gidp smallint,
away_catcher_interference smallint,
away_left_on_base smallint,
away_pitchers_used smallint,
away_indiv_earned_runs smallint,
away_team_earned_runs smallint,
away_wild_pitches smallint,
away_balks smallint,
away_putouts smallint,
away_assists smallint,
away_errors smallint,
away_passed_balls smallint,
away_double_plays smallint,
away_triple_plays smallint,
home_at_bats smallint,
home_hits smallint,
home_doubles smallint,
home_triples smallint,
home_home_runs smallint,
home_rbi smallint,
home_sac_hit smallint,
home_sac_fly smallint,
home_hit_by_pitch smallint,
home_walks smallint,
home_int_walks smallint,
home_strikeouts smallint,
home_stolen_bases smallint,
home_caught_stealing smallint,
home_gidp smallint,
home_catcher_interference smallint,
home_left_on_base smallint,
home_pitchers_used smallint,
home_indiv_earned_runs smallint,
home_team_earned_runs smallint,
home_wild_pitches smallint,
home_balks smallint,
home_putouts smallint,
home_assists smallint,
home_errors smallint,
home_passed_balls smallint,
home_double_plays smallint,
home_triple_plays smallint
)""" # WITHOUT ROWID
# excluded data from boxscores, not present in game_info
# additional_info varchar(128),
# acq_info char(1),
allTables["score_by_inning"]="""CREATE TABLE score_by_inning (
game_id integer,
inning smallint,
home_score smallint,
away_score smallint,
PRIMARY KEY(game_id, inning),
FOREIGN KEY(game_id) REFERENCES gamelog(game_id)
) """
allTables["completion"]="""CREATE TABLE completion (
game_id integer PRIMARY KEY,
completion_date char(12) NOT NULL,
completion_park char(5) NOT NULL,
visitor_score_int smallint NOT NULL,
home_score_int smallint NOT NULL,
outs_int smallint NOT NULL,
FOREIGN KEY(game_id) REFERENCES game_info(game_id),
FOREIGN KEY(completion_park) REFERENCES park(park_id)
)"""
# bat_pos 0=P, or 1-9
# field_pos 0=DH, 1=pitcher, 2=catcher, 3=first base, 4=second base, 5=third base
# 6=short stop 7=left field 8=center field, 9=right field,
# 10=pinch runner 11=pinch hitter
allTables["event_start"]="""CREATE TABLE event_start (
game_id integer,
event_id smallint,
player_num_id smallint,
team char(3),
bat_pos smallint,
field_pos smallint,
PRIMARY KEY(game_id, event_id),
FOREIGN KEY(player_num_id) REFERENCES player(player_num_id)
)
"""
# play_id - substituted occured after play_id
# for subs, field_pos is same as starters, 0=DH, 10=pinch hit, 11=pinch run
allTables["event_sub"]="""CREATE TABLE event_sub (
game_id integer,
event_id smallint,
player_num_id smallint,
team char(3),
bat_pos smallint,
field_pos smallint,
PRIMARY KEY(game_id, event_id),
FOREIGN KEY(player_num_id) REFERENCES player(player_num_id)
)
"""
# pitch sequence data
# outcome
# + following pickoff throw by the catcher
# * indicates the following pitch was blocked by the catcher
# . marker for play not involving the batter
# 1 pickoff throw to first
# 2 pickoff throw to second
# 3 pickoff throw to third
# > Indicates a runner going on the pitch
# A automatic strike, usually for pitch timer violation
# B ball
# C called strike
# F foul
# H hit batter
# I intentional ball
# K strike (unknown type)
# L foul bunt
# M missed bunt attempt
# N no pitch (on balks and interference calls)
# O foul tip on bunt
# P pitchout
# Q swinging on pitchout
# R foul ball on pitchout
# S swinging strike
# T foul tip
# U unknown or missed pitch
# V called ball because pitcher went to his mouth or automatic ball on intentional walk or
# pitch timer violation
# X ball put into play by batter
# Y ball put into play on pitchout
# communications from broadcasts
# PRIMARY KEY(game_id_event_id)
allTables["event_com"]="""CREATE TABLE event_com (
game_id integer,
event_id smallint,
com char(256),
PRIMARY KEY(game_id, event_id)
)"""
# AP appeal play
# BP pop up bunt
# BG ground ball bunt
# BGDP bunt grounded into double play
# BINT batter interference
# BL line drive bunt
# BOOT batting out of turn
# BP bunt pop up
# BPDP bunt popped into double play
# BR runner hit by batted ball
# C called third strike
# COUB courtesy batter
# COUF courtesy fielder
# COUR courtesy runner
# DP unspecified double play
# E$ error on $
# F fly
# FDP fly ball double play
# FINT fan interference
# FL foul
# FO force out
# G ground ball
# GDP ground ball double play
# GTP ground ball triple play
# IF infield fly rule
# INT interference
# IPHR inside the park home run
# L line drive
# LDP lined into double play
# LTP lined into triple play
# MREV manager challenge of call on the field
# NDP no double play credited for this play
# OBS obstruction (fielder obstructing a runner)
# P pop fly
# PASS a runner passed another runner and was called out
# R$ relay throw from the initial fielder to $ with no out made
# RINT runner interference
# SF sacrifice fly
# SH sacrifice hit (bunt)
# TH throw
# TH% throw to base %
# TP unspecified triple play
# UINT umpire interference
# UREV umpire review of call on the field
# batter-count = (balls << 2) + strikes
# PRIMARY KEY(game_id_event_id),
allTables["event_play"]="""CREATE TABLE event_play (
game_id integer,
event_id smallint,
team char(3),
player_num_id smallint,
inning smallint,
batter_count char(2),
pitch_seq char(32),
play char(64),
PRIMARY KEY(game_id, event_id),
FOREIGN KEY(player_num_id) REFERENCES player(player_num_id)
)"""
# adjustments
# adj_type 0=batter, 1=runner, 2=pitcher, 3:pitcher responsiblity
# PRIMARY KEY(game_id_event_id),
allTables["event_player_adj"]="""CREATE TABLE event_player_adj (
game_id integer,
event_id smallint,
player_num_id smallint,
adj_type smallint,
adj char(1),
PRIMARY KEY(game_id, event_id),
FOREIGN KEY(player_num_id) REFERENCES player(player_num_id)
)
"""
# PRIMARY KEY(game_id_event_id)
allTables["event_lineup_adj"]="""CREATE TABLE event_lineup_adj (
game_id integer,
event_id smallint,
team_id char(3),
adj smallint,
PRIMARY KEY(game_id, event_id)
)
"""
# PRIMARY KEY(game_id_event_id),
allTables["event_data_er"]="""CREATE TABLE event_data_er (
game_id integer,
event_id smallint,
player_num_id smallint,
er smallint,
PRIMARY KEY(game_id, event_id),
FOREIGN KEY(player_num_id) REFERENCES player(player_num_id)
)
"""
###### value tables ######
allTables["day_in_week"] = """CREATE TABLE day_in_week (
num smallint PRIMARY KEY,
d char(3) NOT NULL
)"""
allTables["month"] = """CREATE TABLE month (
num smallint PRIMARY KEY,
d char(3) NOT NULL
)"""
allTables["season"] = """CREATE TABLE season (
num smallint PRIMARY KEY,
d char(16) NOT NULL
)"""
allTables["postseason_series"] = """CREATE TABLE postseason_series (
num smallint PRIMARY KEY,
d char(16) NOT NULL
)"""
allTables["winddir"] = """CREATE TABLE winddir (
num smallint PRIMARY KEY,
d char(16) NOT NULL
)"""
allTables["fieldcond"] = """CREATE TABLE fieldcond (
num smallint PRIMARY KEY,
d char(16) NOT NULL
)"""
allTables["sky"] = """CREATE TABLE sky (
num smallint PRIMARY KEY,
d char(16) NOT NULL
)"""
allTables["precip"] = """CREATE TABLE precip (
num smallint PRIMARY KEY,
d char(16) NOT NULL
)"""
allTables["forfeit"] = """CREATE TABLE forfeit (
num smallint PRIMARY KEY,
d char(16) NOT NULL
)"""
allTables["protest"] = """CREATE TABLE protest (
num smallint PRIMARY KEY,
d char(16) NOT NULL
)"""
allTables["daynight"] = """CREATE TABLE daynight (
num smallint PRIMARY KEY,
d char(16) NOT NULL
)"""
allTables["play_desc"] = """CREATE TABLE play_desc (
k char(5) PRIMARY KEY,
d char(32) NOT NULL
)"""
allTables["mod_desc"] = """CREATE TABLE mod_desc (
k char(5) PRIMARY KEY,
d char(32) NOT NULL
)"""
allTables["field_pos_desc"] = """CREATE TABLE field_pos_desc (
pos smallint PRIMARY KEY,
d char(32) NOT NULL
)"""
##### views #####
allViews["game_info_view"] = """CREATE VIEW game_info_view AS SELECT
i.game_id,
i.game_date,
i.year,
i.month,
i.dow,
i.start_time,
seas.d AS season,
post.d AS post_series,
(3 & (game_type_num >> 6)) AS dh_num,
atm.team_id AS away_team,
atm.team_city AS away_team_city,
atm.team_nickname AS away_team_name,
i.away_league,
i.away_division,
i.away_game_num,
htm.team_id AS home_team,
htm.team_city AS home_team_city,
htm.team_nickname AS home_team_name,
i.home_league,
i.home_division,
i.home_game_num,
i.park AS park_id,
prk.park_name AS park,
i.attendance,
i.game_duration_min AS duration,
dn.d AS daynight,
fc.d AS field_cond,
pr.d AS precip,
sk.d AS sky,
wd.d AS winddir,
127 & (cond >> 14) AS windspeed,
127 & (cond >> 21) AS temp,
(1 & (flags >> 2)) AS home_team_bat_first,
(1 & (flags >> 3)) AS use_dh,
(3 & (flags >> 4)) AS tiebreakbase,
(1 & (flags >> 6)) AS complete,
(15 & (game_type_num >> 8)) AS inn_sched,
forf.d AS forfeit,
prot.d AS protest,
wpit.name_last||', '||SUBSTR(wpit.name_other, 0, 2) AS win_pitcher,
lpit.name_last||', '||SUBSTR(lpit.name_other, 0, 2) AS loss_pitcher,
spit.name_last||', '||SUBSTR(spit.name_other, 0, 2) AS save_pitcher,
gwr.name_last||', '||SUBSTR(gwr.name_other, 0, 2) AS gw_rbi
FROM game_info i
LEFT JOIN (SELECT * FROM season) seas
ON seas.num=(7 & i.game_type_num)
LEFT JOIN (SELECT * FROM postseason_series) post
ON post.num=(7 & (i.game_type_num >> 3) )
LEFT JOIN (SELECT * FROM daynight) dn
ON dn.num=(1 & i.cond)
LEFT JOIN (SELECT * FROM fieldcond) fc
ON fc.num=(7 & (i.cond >> 1))
LEFT JOIN (SELECT * FROM precip) pr
ON pr.num=(7 & (i.cond >> 4))
LEFT JOIN (SELECT * FROM sky) sk
ON sk.num=(7 & (i.cond >> 7) )
LEFT JOIN (SELECT * FROM winddir) wd
ON wd.num=(7 & (i.cond >> 10) )
LEFT JOIN (SELECT * FROM team) htm
ON htm.team_id=i.home_team
LEFT JOIN (SELECT * FROM team) atm
ON atm.team_id=i.away_team
LEFT JOIN (SELECT * FROM player) wpit
ON wpit.player_num_id=i.win_pitcher_num_id
LEFT JOIN (SELECT * FROM player) lpit
ON lpit.player_num_id=i.loss_pitcher_num_id
LEFT JOIN (SELECT * FROM player) spit
ON spit.player_num_id=i.save_pitcher_num_id
LEFT JOIN (SELECT * FROM player) gwr
ON gwr.player_id=i.gw_rbi_num_id
LEFT JOIN (SELECT * FROM park) prk
ON prk.park_id=i.park
LEFT JOIN (SELECT * FROM forfeit) forf
ON forf.num=(3 & (i.flags >> 7))
LEFT JOIN (SELECT * FROM protest) prot
ON prot.num=(7 & (i.flags >> 9))
"""
game_info_view_clickhouse = """CREATE VIEW game_info_view AS SELECT
i.game_id,
i.game_date,
i.year,
i.month,
i.dow,
i.start_time,
seas.d AS season,
post.d AS post_series,
bitAnd(3, bitShiftRight(game_type_num, 6)) AS dh_num,
atm.team_id AS away_team,
atm.team_city AS away_team_city,
atm.team_nickname AS away_team_name,
i.away_league,
i.away_division,
i.away_game_num,
htm.team_id AS home_team,
htm.team_city AS home_team_city,
htm.team_nickname AS home_team_name,
i.home_league,
i.home_division,
i.home_game_num,
i.park AS park_id,
prk.park_name AS park,
i.attendance,
i.game_duration_min AS duration,
dn.d AS daynight,
fc.d AS field_cond,
pr.d AS precip,
sk.d AS sky,
wd.d AS winddir,
bitAnd(127, bitShiftRight(cond, 14)) AS windspeed,
bitAnd(127, bitShiftRight(cond, 21)) AS temp,
bitAnd(1, bitShiftRight(flags, 2)) AS home_team_bat_first,
bitAnd(1, bitShiftRight(flags, 3)) AS use_dh,
bitAnd(3, bitShiftRight(flags, 4)) AS tiebreakbase,
bitAnd(1, bitShiftRight(flags, 6)) AS complete,
forf.d AS forfeit,
prot.d AS protest,
wpit.name_last||', '||SUBSTR(wpit.name_other, 1, 2) AS win_pitcher,
lpit.name_last||', '||SUBSTR(lpit.name_other, 1, 2) AS loss_pitcher,
spit.name_last||', '||SUBSTR(spit.name_other, 1, 2) AS save_pitcher,
gwr.name_last||', '||SUBSTR(gwr.name_other, 1, 2) AS gw_rbi
FROM game_info i
LEFT JOIN (SELECT * FROM season) seas
ON seas.num=bitAnd(7, i.game_type_num)
LEFT JOIN (SELECT * FROM postseason_series) post
ON post.num=bitAnd(7, bitShiftRight(i.game_type_num, 3))
LEFT JOIN (SELECT * FROM daynight) dn
ON dn.num=bitAnd(1, i.cond)
LEFT JOIN (SELECT * FROM fieldcond) fc
ON fc.num=bitAnd(7, bitShiftRight(i.cond, 1))
LEFT JOIN (SELECT * FROM precip) pr
ON pr.num=bitAnd(7, bitShiftRight(i.cond, 4))
LEFT JOIN (SELECT * FROM sky) sk
ON sk.num=bitAnd(7, bitShiftRight(i.cond, 7))
LEFT JOIN (SELECT * FROM winddir) wd
ON wd.num=bitAnd(7, bitShiftRight(i.cond, 10))
LEFT JOIN (SELECT * FROM team) htm
ON htm.team_id=i.home_team
LEFT JOIN (SELECT * FROM team) atm
ON atm.team_id=i.away_team
LEFT JOIN (SELECT * FROM player) wpit
ON wpit.player_num_id=i.win_pitcher_num_id
LEFT JOIN (SELECT * FROM player) lpit
ON lpit.player_num_id=i.loss_pitcher_num_id
LEFT JOIN (SELECT * FROM player) spit
ON spit.player_num_id=i.save_pitcher_num_id
LEFT JOIN (SELECT * FROM player) gwr
ON gwr.player_num_id=i.gw_rbi_num_id
LEFT JOIN (SELECT * FROM park) prk
ON prk.park_id=i.park
LEFT JOIN (SELECT * FROM forfeit) forf
ON forf.num=bitAnd(3, bitShiftRight(i.flags, 7))
LEFT JOIN (SELECT * FROM protest) prot
ON prot.num=bitAnd(7, bitShiftRight(i.flags, 9))
"""
allViews["gamelog_view"] = """CREATE VIEW gamelog_view AS SELECT
l.game_id,
l.away_score,
l.home_score,
l.game_len_outs,
i.game_date,
i.year,
i.month,
i.dow,
i.start_time,
i.season,
i.post_series,
i.dh_num,
i.away_team,
i.away_team_city,
i.away_team_name,
i.away_league,
i.away_game_num,
i.home_team,
i.home_team_city,
i.home_team_name,
i.home_league,
i.home_game_num,
i.park,
i.attendance,
i.game_duration_min AS duration,
i.daynight,
i.field_cond,
i.precip,
i.sky,
i.winddir,
i.windspeed,
i.temp,
i.home_team_bat_first,
i.use_dh,
i.complete,
i.forfeit,
i.protest,
i.win_pitcher,
i.loss_pitcher,
i.save_pitcher,
i.gw_rbi,
l.away_at_bats,
l.away_hits,
l.away_doubles,
l.away_triples,
l.away_home_runs,
l.away_rbi,
l.away_sac_hit,
l.away_sac_fly,
l.away_hit_by_pitch,
l.away_walks,
l.away_int_walks,
l.away_strikeouts,
l.away_stolen_bases,
l.away_caught_stealing,
l.away_gidp,
l.away_catcher_interference,
l.away_left_on_base,
l.away_pitchers_used,
l.away_indiv_earned_runs,
l.away_team_earned_runs,
l.away_wild_pitches,
l.away_balks,
l.away_putouts,
l.away_assists,
l.away_errors,
l.away_passed_balls,
l.away_double_plays,
l.away_triple_plays,
l.home_at_bats,
l.home_hits,
l.home_doubles,
l.home_triples,
l.home_home_runs,
l.home_rbi,
l.home_sac_hit,
l.home_sac_fly,
l.home_hit_by_pitch,
l.home_walks,
l.home_int_walks,
l.home_strikeouts,
l.home_stolen_bases,
l.home_caught_stealing,
l.home_gidp,
l.home_catcher_interference,
l.home_left_on_base,
l.home_pitchers_used,
l.home_indiv_earned_runs,
l.home_team_earned_runs,
l.home_wild_pitches,
l.home_balks,
l.home_putouts,
l.home_assists,
l.home_errors,
l.home_passed_balls,
l.home_double_plays,
l.home_triple_plays,
s1.home_score AS h1,
s1.away_score AS a1,
s2.home_score AS h2,
s2.away_score AS a2,
s3.home_score AS h3,
s3.away_score AS a3,
s4.home_score AS h4,
s4.away_score AS a4,
s5.home_score AS h5,
s5.away_score AS a5,
s6.home_score AS h6,
s6.away_score AS a6,
s7.home_score AS h7,
s7.away_score AS a7,
s8.home_score AS h8,
s8.away_score AS a8,
s9.home_score AS h9,
s9.away_score AS a9,
s10.home_score AS h10,
s10.away_score AS a10,
s11.home_score AS h11,
s11.away_score AS a11,
s12.home_score AS h12,
s12.away_score AS a12,
s13.home_score AS h13,
s13.away_score AS a13,
s14.home_score AS h14,
s14.away_score AS a14,
s15.home_score AS h15,
s15.away_score AS a15,
s16.home_score AS h16,
s16.away_score AS a16,
s17.home_score AS h17,
s17.away_score AS a17,
s18.home_score AS h18,
s18.away_score AS a18,
s19.home_score AS h19,
s19.away_score AS a19,
s20.home_score AS h20,
s20.away_score AS a20,
s21.home_score AS h21,
s21.away_score AS a21,
s22.home_score AS h22,
s22.away_score AS a22,
s23.home_score AS h23,
s23.away_score AS a23,
s24.home_score AS h24,
s24.away_score AS a24,
s25.home_score AS h25,
s25.away_score AS a25
FROM gamelog l
LEFT JOIN (SELECT * FROM game_info_view) i
ON l.game_id=i.game_id
LEFT JOIN (SELECT * FROM score_by_inning) s1
ON l.game_id=s1.game_id AND s1.inning=1
LEFT JOIN (SELECT * FROM score_by_inning) s2
ON l.game_id=s2.game_id AND s2.inning=2
LEFT JOIN (SELECT * FROM score_by_inning) s3
ON l.game_id=s3.game_id AND s3.inning=3
LEFT JOIN (SELECT * FROM score_by_inning) s4
ON l.game_id=s4.game_id AND s4.inning=4
LEFT JOIN (SELECT * FROM score_by_inning) s5
ON l.game_id=s5.game_id AND s5.inning=5
LEFT JOIN (SELECT * FROM score_by_inning) s6
ON l.game_id=s6.game_id AND s6.inning=6
LEFT JOIN (SELECT * FROM score_by_inning) s7
ON l.game_id=s7.game_id AND s7.inning=7
LEFT JOIN (SELECT * FROM score_by_inning) s8
ON l.game_id=s8.game_id AND s8.inning=8
LEFT JOIN (SELECT * FROM score_by_inning) s9
ON l.game_id=s9.game_id AND s9.inning=9
LEFT JOIN (SELECT * FROM score_by_inning) s10
ON l.game_id=s10.game_id AND s10.inning=10
LEFT JOIN (SELECT * FROM score_by_inning) s11
ON l.game_id=s11.game_id AND s11.inning=11
LEFT JOIN (SELECT * FROM score_by_inning) s12
ON l.game_id=s12.game_id AND s12.inning=12
LEFT JOIN (SELECT * FROM score_by_inning) s13
ON l.game_id=s13.game_id AND s13.inning=13
LEFT JOIN (SELECT * FROM score_by_inning) s14
ON l.game_id=s14.game_id AND s14.inning=14
LEFT JOIN (SELECT * FROM score_by_inning) s15
ON l.game_id=s15.game_id AND s15.inning=15
LEFT JOIN (SELECT * FROM score_by_inning) s16
ON l.game_id=s16.game_id AND s16.inning=16
LEFT JOIN (SELECT * FROM score_by_inning) s17
ON l.game_id=s17.game_id AND s17.inning=17
LEFT JOIN (SELECT * FROM score_by_inning) s18
ON l.game_id=s18.game_id AND s18.inning=18
LEFT JOIN (SELECT * FROM score_by_inning) s19
ON l.game_id=s19.game_id AND s19.inning=19
LEFT JOIN (SELECT * FROM score_by_inning) s20
ON l.game_id=s20.game_id AND s20.inning=20
LEFT JOIN (SELECT * FROM score_by_inning) s21
ON l.game_id=s21.game_id AND s21.inning=21
LEFT JOIN (SELECT * FROM score_by_inning) s22
ON l.game_id=s22.game_id AND s22.inning=22
LEFT JOIN (SELECT * FROM score_by_inning) s23
ON l.game_id=s23.game_id AND s23.inning=23
LEFT JOIN (SELECT * FROM score_by_inning) s24
ON l.game_id=s24.game_id AND s24.inning=24
LEFT JOIN (SELECT * FROM score_by_inning) s25
ON l.game_id=s25.game_id AND s25.inning=25
"""
allTables["player_game_batting"] = """CREATE TABLE player_game_batting (
game_id integer,
batter_num_id smallint,
team char(3),
pos smallint,
seq smallint,
AB smallint,
R smallint,
H smallint,
n2B smallint,
n3B smallint,
HR smallint,
RBI smallint,
SH smallint,
SF smallint,
HBP smallint,
BB smallint,
IBB smallint,
K smallint,
SB smallint,
CS smallint,
GIDP smallint,
INTF smallint,
PRIMARY KEY(game_id, batter_num_id, pos),
FOREIGN KEY(batter_num_id) REFERENCES player(player_num_id)
)"""
allTables["player_game_fielding"] = """CREATE TABLE player_game_fielding (
game_id integer,
fielder_num_id smallint,
team char(3),
pos smallint,
seq smallint,
IF3 smallint,
PO smallint,
A smallint,
E smallint,
DP smallint,
TP smallint,
PB smallint,
PRIMARY KEY(game_id, fielder_num_id, pos, seq),
FOREIGN KEY(fielder_num_id) REFERENCES player(player_num_id)
)"""
allTables["player_game_pitching"] = """CREATE TABLE player_game_pitching (
game_id integer,
pitcher_num_id smallint,
team char(3),
seq smallint,
IP3 smallint,
NOOUT smallint,
BF smallint,
H smallint,
n2B smallint,
n3B smallint,
HR smallint,
R smallint,
ER smallint,
BB smallint,
IBB smallint,
K smallint,
HBP smallint,
WP smallint,
BK smallint,
SH smallint,
SF smallint,
PRIMARY KEY(game_id, pitcher_num_id, seq),
FOREIGN KEY(pitcher_num_id) REFERENCES player(player_num_id)
)"""
# inning = inning + (inning_half << 6)
allTables["game_situation"] = """CREATE TABLE game_situation (
game_id integer,
event_id smallint,
batter_num_id smallint NOT NULL,
pitcher_num_id smallint NOT NULL,
inning smallint NOT NULL,
inning_half char(1) NOT NULL,
outs smallint NOT NULL,
bat_pitch_cnt char(2),
bat_team_score smallint NOT NULL,
pitch_team_score smallint NOT NULL,
play_result char(3),
play_base smallint,
hit_loc char(3),
hit_type char(1),
outs_made smallint,
runs_scored smallint,
PRIMARY KEY(game_id, event_id),
FOREIGN KEY(batter_num_id) REFERENCES player(player_num_id),
FOREIGN KEY(pitcher_num_id) REFERENCES player(player_num_id)
)"""
allTables["game_situation_bases"] = """CREATE TABLE game_situation_bases (
game_id integer,
event_id smallint,
bases_first smallint,
bases_second smallint,
bases_third smallint,
PRIMARY KEY(game_id, event_id),
FOREIGN KEY(bases_first) REFERENCES player(player_num_id),
FOREIGN KEY(bases_second) REFERENCES player(player_num_id),
FOREIGN KEY(bases_third) REFERENCES player(player_num_id)
)"""
allTables["game_situation_result2"] = """CREATE TABLE game_situation_result2 (
game_id integer,
event_id smallint,
play_result char(3),
play_base smallint,
PRIMARY KEY(game_id, event_id)
)"""
allTables["game_situation_result3"] = """CREATE TABLE game_situation_result3 (
game_id integer,
event_id smallint,
play_result char(3),
play_base smallint,
PRIMARY KEY(game_id, event_id)
)"""
allTables["game_situation_fielder_assist"] = """CREATE TABLE game_situation_fielder_assist (
game_id integer,
event_id smallint,
fielder_num_id smallint,
seq smallint,
PRIMARY KEY(game_id, event_id, fielder_num_id, seq),
FOREIGN KEY(fielder_num_id) REFERENCES player(player_num_id)
)"""
allTables["game_situation_fielder_putout"] = """CREATE TABLE game_situation_fielder_putout (
game_id integer,
event_id smallint,
fielder_num_id smallint,
seq smallint,
PRIMARY KEY(game_id, event_id, fielder_num_id, seq),
FOREIGN KEY(fielder_num_id) REFERENCES player(player_num_id)
)"""
allTables["game_situation_fielder_error"] = """CREATE TABLE game_situation_fielder_error (
game_id integer,
event_id smallint,
fielder_num_id smallint,