lahmanlite/sql/allstars.sql

80 lines
2.0 KiB
MySQL
Raw Normal View History

2024-05-05 03:46:30 +00:00
/*
Copyright (C) 2024 filifa
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/
pragma foreign_keys = 0;
2024-01-30 02:34:07 +00:00
begin;
2024-05-05 03:11:20 +00:00
attach database 'lahman-raw.db' as 'raw';
2024-01-31 06:09:39 +00:00
create temp table if not exists "allstars" (
2024-01-30 02:34:07 +00:00
"player" text,
"year" text,
"gameNum" text,
"team" text,
"GP" numeric,
"gameID" text
);
insert into allstars
select distinct playerid, yearid, gamenum, teamid, gp, gameid
from "raw".allstarfull;
-- fix game numbers for 1962 all-star games
update allstars
set gamenum = 1
where gameid = 'ALS196207100';
update allstars
set gamenum = 2
where gameid = 'NLS196207300';
alter table allstars drop column "gameid";
2024-01-30 02:55:27 +00:00
-- correct team codes for whole seasons
update allstars
set team = 'ANA'
where year between 1997 and 2004 and team = 'LAA';
update allstars
set team = 'ML4'
where year between 1970 and 1997 and team = 'MIL';
update allstars
set team = 'ML1'
where year between 1953 and 1965 and team = 'MLN';
-- correct team codes for one-off errors
update allstars
set team = 'NYA'
where player = 'chapmbe01' and year = 1933;
update allstars
set team = 'ATL'
where player = 'contrwi02' and year = 2022;
2024-01-31 06:09:39 +00:00
create table if not exists "allstars" (
"player" text,
"year" text,
"gameNum" text,
"team" text,
2024-02-02 02:48:12 +00:00
"GP" numeric check ("GP" in (0, 1)),
2024-01-31 06:09:39 +00:00
primary key("player","year","gameNum"),
foreign key("year","gameNum") references "allstargames"("year","gameNum"),
foreign key("player","year","team") references "appearances"("player","year","team")
);
insert into main.allstars select distinct * from temp.allstars;
2024-01-30 02:34:07 +00:00
end;