lahmanlite/sql/load.sql

369 lines
7.6 KiB
PL/PgSQL

BEGIN;
attach database 'baseball-transformed.db' as 'transformed';
create table if not exists "awards" (
"ID" text,
primary key("ID")
);
insert into awards
select distinct * from "transformed"."awards";
CREATE TABLE IF NOT EXISTS "franchises" (
"ID" TEXT,
"name" TEXT,
"active" TEXT,
"NAassoc" TEXT,
PRIMARY KEY("ID")
);
insert into franchises
select distinct * from "transformed"."franchises";
create table if not exists "teamseasons" (
"year" NUMERIC,
"league" TEXT,
"team" TEXT,
"division" TEXT,
"Rank" NUMERIC,
"G" NUMERIC,
"Ghome" NUMERIC,
"W" NUMERIC,
"L" NUMERIC,
"DivWin" NUMERIC,
"WCWin" NUMERIC,
"LgWin" NUMERIC,
"WSWin" NUMERIC,
"R" NUMERIC,
"AB" NUMERIC,
"H" NUMERIC,
"2B" NUMERIC,
"3B" NUMERIC,
"HR" NUMERIC,
"BB" NUMERIC,
"SO" NUMERIC,
"SB" NUMERIC,
"CS" NUMERIC,
"HBP" NUMERIC,
"SF" NUMERIC,
"RA" NUMERIC,
"ER" NUMERIC,
"ERA" NUMERIC,
"CG" NUMERIC,
"SHO" NUMERIC,
"SV" NUMERIC,
"IPouts" NUMERIC,
"HA" NUMERIC,
"HRA" NUMERIC,
"BBA" NUMERIC,
"SOA" NUMERIC,
"E" NUMERIC,
"DP" NUMERIC,
"FP" NUMERIC,
"name" NUMERIC,
"park" NUMERIC,
"attendance" NUMERIC,
"BPF" NUMERIC,
"PPF" NUMERIC,
"teamIDBR" TEXT,
"teamIDlahman45" TEXT,
"teamIDretro" TEXT,
PRIMARY KEY("year","team"),
foreign key("year") references "seasons"("year"),
foreign key("league") references "leagues"("ID"),
foreign key("team") references "teams"("ID")
);
insert into teamseasons
select distinct * from "transformed"."teamseasons";
create table if not exists "seasons" (
"year" numeric,
primary key("year")
);
insert into seasons
select distinct * from "transformed"."seasons";
CREATE TABLE "people" (
"ID" text,
"birthYear" NUMERIC,
"birthMonth" NUMERIC,
"birthDay" NUMERIC,
"birthCountry" text,
"birthState" text,
"birthCity" text,
"deathYear" text,
"deathMonth" text,
"deathDay" text,
"deathCountry" text,
"deathState" text,
"deathCity" text,
"nameFirst" text,
"nameLast" text,
"nameGiven" text,
"weight" NUMERIC,
"height" NUMERIC,
"bats" text,
"throws" text,
"debut" text,
"finalGame" text,
"retroID" text,
"bbrefID" text,
primary key("ID")
);
INSERT INTO "people" SELECT DISTINCT * FROM "transformed"."people";
CREATE TABLE IF NOT EXISTS "parks" (
"ID" TEXT,
"name" TEXT,
"city" TEXT,
"state" TEXT,
"country" TEXT,
PRIMARY KEY("ID")
);
insert into "parks" select distinct * from "transformed"."parks";
CREATE TABLE IF NOT EXISTS "parkaliases" (
"park" TEXT,
"alias" TEXT,
PRIMARY KEY("park","alias"),
foreign key("park") references "parks"("ID")
);
insert into "parkaliases" select distinct * from "transformed"."parkaliases";
CREATE TABLE IF NOT EXISTS "collegeplaying" (
"player" TEXT,
"school" TEXT,
"year" NUMERIC,
PRIMARY KEY("player","year","school"),
foreign key("player") references "people"("ID"),
foreign key("school") references "schools"("ID")
);
insert into collegeplaying select distinct * from "transformed"."collegeplaying";
CREATE TABLE IF NOT EXISTS "schools" (
"ID" TEXT,
"name_full" TEXT,
"city" TEXT,
"state" TEXT,
"country" TEXT,
PRIMARY KEY("ID")
);
insert into schools select distinct * from "transformed"."schools";
CREATE TABLE IF NOT EXISTS "salaries" (
"year" NUMERIC,
"team" TEXT,
"player" TEXT,
"salary" NUMERIC,
PRIMARY KEY("year","player","team"),
foreign key("player") references "people"("ID"),
foreign key("team") references "teams"("ID"),
foreign key("year") references "seasons"("year")
);
insert into salaries select distinct * from "transformed"."salaries";
CREATE TABLE IF NOT EXISTS "batting" (
"player" TEXT,
"year" NUMERIC,
"stint" NUMERIC,
"team" TEXT,
"G" NUMERIC,
"AB" NUMERIC,
"R" NUMERIC,
"H" NUMERIC,
"2B" NUMERIC,
"3B" NUMERIC,
"HR" NUMERIC,
"RBI" NUMERIC,
"SB" NUMERIC,
"CS" NUMERIC,
"BB" NUMERIC,
"SO" NUMERIC,
"IBB" NUMERIC,
"HBP" NUMERIC,
"SH" NUMERIC,
"SF" NUMERIC,
"GIDP" NUMERIC,
PRIMARY KEY("player","year","stint","team"),
foreign key("player") references "people"("ID"),
foreign key("team") references "teams"("ID"),
foreign key("year") references "seasons"("year")
);
insert into batting select distinct * from "transformed"."batting";
CREATE TABLE IF NOT EXISTS "fielding" (
"player" TEXT,
"year" NUMERIC,
"stint" NUMERIC,
"team" TEXT,
"POS" TEXT,
"G" NUMERIC,
"GS" NUMERIC,
"InnOuts" NUMERIC,
"PO" NUMERIC,
"A" NUMERIC,
"E" NUMERIC,
"DP" NUMERIC,
"PB" NUMERIC,
"WP" NUMERIC,
"SB" NUMERIC,
"CS" NUMERIC,
"ZR" NUMERIC,
PRIMARY KEY("player","year","stint","team","POS"),
foreign key("player") references "people"("ID"),
foreign key("team") references "teams"("ID"),
foreign key("year") references "seasons"("year")
);
insert into fielding select distinct * from "transformed"."fielding";
CREATE TABLE IF NOT EXISTS "pitching" (
"player" TEXT,
"year" NUMERIC,
"stint" NUMERIC,
"team" TEXT,
"W" NUMERIC,
"L" NUMERIC,
"G" NUMERIC,
"GS" NUMERIC,
"CG" NUMERIC,
"SHO" NUMERIC,
"SV" NUMERIC,
"IPouts" NUMERIC,
"H" NUMERIC,
"ER" NUMERIC,
"HR" NUMERIC,
"BB" NUMERIC,
"SO" NUMERIC,
"BAOpp" NUMERIC,
"ERA" NUMERIC,
"IBB" NUMERIC,
"WP" NUMERIC,
"HBP" NUMERIC,
"BK" NUMERIC,
"BFP" NUMERIC,
"GF" NUMERIC,
"R" NUMERIC,
"SH" NUMERIC,
"SF" NUMERIC,
"GIDP" NUMERIC,
PRIMARY KEY("player","year","stint","team"),
foreign key("player") references "people"("ID"),
foreign key("team") references "teams"("ID"),
foreign key("year") references "seasons"("year")
);
insert into pitching select distinct * from "transformed"."pitching";
CREATE TABLE IF NOT EXISTS "appearances" (
"year" NUMERIC,
"team" TEXT,
"player" TEXT,
"G_all" NUMERIC,
"GS" NUMERIC,
"G_batting" NUMERIC,
"G_defense" NUMERIC,
"G_p" NUMERIC,
"G_c" NUMERIC,
"G_1b" NUMERIC,
"G_2b" NUMERIC,
"G_3b" NUMERIC,
"G_ss" NUMERIC,
"G_lf" NUMERIC,
"G_cf" NUMERIC,
"G_rf" NUMERIC,
"G_of" NUMERIC,
"G_dh" NUMERIC,
"G_ph" NUMERIC,
"G_pr" NUMERIC,
PRIMARY KEY("year","team","player"),
foreign key("player") references "people"("ID"),
foreign key("team") references "teams"("ID"),
foreign key("year") references "seasons"("year")
);
insert into appearances select distinct * from "transformed"."appearances";
CREATE TABLE IF NOT EXISTS "homegames" (
"year" NUMERIC,
"team" TEXT,
"park" TEXT,
"first" TEXT,
"last" TEXT,
"games" NUMERIC,
"openings" NUMERIC,
"attendance" NUMERIC,
PRIMARY KEY("year","team","park"),
foreign key("park") references "parks"("ID"),
foreign key("team") references "teams"("ID"),
foreign key("year") references "seasons"("year")
);
insert into homegames select distinct * from "transformed"."homegames";
CREATE TABLE IF NOT EXISTS "seriespost" (
"year" NUMERIC,
"round" NUMERIC,
"winner" TEXT,
"loser" TEXT,
"wins" NUMERIC,
"losses" NUMERIC,
"ties" NUMERIC,
PRIMARY KEY("year","round"),
foreign key("year") references "seasons"("year")
);
insert into seriespost select distinct * from "transformed"."seriespost";
CREATE TABLE IF NOT EXISTS "fieldingofsplit" (
"player" TEXT,
"year" NUMERIC,
"stint" NUMERIC,
"team" TEXT,
"POS" TEXT,
"G" NUMERIC,
"GS" NUMERIC,
"InnOuts" NUMERIC,
"PO" NUMERIC,
"A" NUMERIC,
"E" NUMERIC,
"DP" NUMERIC,
"PB" NUMERIC,
"WP" NUMERIC,
"SB" NUMERIC,
"CS" NUMERIC,
"ZR" NUMERIC,
PRIMARY KEY("player","year","stint","team","POS"),
foreign key("player") references "people"("ID"),
foreign key("team") references "teams"("ID"),
foreign key("year") references "seasons"("year")
);
insert into fieldingofsplit select distinct * from "transformed"."fieldingofsplit";
create table if not exists teams (
"ID" text,
"franchise" text,
primary key("ID"),
foreign key("franchise") references "franchises"("ID")
);
insert into teams select distinct * from "transformed"."teams";
create table if not exists "leagues" (
"ID" text,
primary key("ID")
);
insert into leagues select distinct * from "transformed"."leagues";
COMMIT;