lahmanlite/sql/appearances.sql

72 lines
2.1 KiB
MySQL
Raw Normal View History

pragma foreign_keys = 0;
2024-01-26 19:27:58 +00:00
begin;
2024-01-31 06:09:39 +00:00
attach database 'baseball-raw.db' as 'raw';
create temp table appearances as
select * from "raw".appearances;
alter table appearances drop column "lgID";
update appearances
set teamid = 'WS9'
where teamid = 'WAS' and yearid between 1891 and 1899;
update appearances
set teamid = 'PHP'
where teamid = 'PH4' and yearid between 1890 and 1891;
2024-01-31 06:09:39 +00:00
2024-02-01 04:37:26 +00:00
-- got this missing data from bbref
insert into appearances values
(1896,'BRO','dailyco01',1,null,1,null,0,1,0,0,0,0,0,0,0,0,0,null,null),
(1875,'WS6','thompfr01',11,null,11,null,0,11,0,0,0,0,0,0,1,1,0,null,null);
2024-02-02 03:45:26 +00:00
update appearances
set
"G_all" = nullif("G_all",''),
"GS" = nullif("GS",''),
"G_batting" = nullif("G_batting",''),
"G_defense" = nullif("G_defense",''),
"G_p" = nullif("G_p",''),
"G_c" = nullif("G_c",''),
"G_1b" = nullif("G_1b",''),
"G_2b" = nullif("G_2b",''),
"G_3b" = nullif("G_3b",''),
"G_ss" = nullif("G_ss",''),
"G_lf" = nullif("G_lf",''),
"G_cf" = nullif("G_cf",''),
"G_rf" = nullif("G_rf",''),
"G_of" = nullif("G_of",''),
"G_dh" = nullif("G_dh",''),
"G_ph" = nullif("G_ph",''),
"G_pr" = nullif("G_pr",'');
2024-01-31 06:09:39 +00:00
CREATE TABLE IF NOT EXISTS "appearances" (
"year" NUMERIC,
"team" TEXT,
"player" TEXT,
"G_all" NUMERIC,
2024-02-02 03:45:26 +00:00
"GS" NUMERIC check ("GS" <= "G_all"),
"G_batting" NUMERIC check ("G_batting" <= "G_all"),
"G_defense" NUMERIC check ("G_defense" <= "G_all"),
"G_p" NUMERIC check ("G_p" <= "G_all"),
"G_c" NUMERIC check ("G_c" <= "G_all"),
"G_1b" NUMERIC check ("G_1b" <= "G_all"),
"G_2b" NUMERIC check ("G_2b" <= "G_all"),
"G_3b" NUMERIC check ("G_3b" <= "G_all"),
"G_ss" NUMERIC check ("G_ss" <= "G_all"),
"G_lf" NUMERIC check ("G_lf" <= "G_all"),
"G_cf" NUMERIC check ("G_cf" <= "G_all"),
"G_rf" NUMERIC check ("G_rf" <= "G_all"),
"G_of" NUMERIC check ("G_of" <= "G_all"),
"G_dh" NUMERIC check ("G_dh" <= "G_all"),
"G_ph" NUMERIC check ("G_ph" <= "G_all"),
"G_pr" NUMERIC check ("G_pr" <= "G_all"),
2024-01-31 06:09:39 +00:00
PRIMARY KEY("year","team","player"),
foreign key("player") references "people"("ID"),
foreign key("year","team") references "teamseasons"("year","team")
);
insert into main.appearances select distinct * from temp.appearances;
2024-01-26 19:27:58 +00:00
commit;