pragma foreign_keys = 0;

begin;
attach database 'baseball-raw.db' as 'raw';
create temp table if not exists "halloffamereqs" (
	"yearID"	numeric,
	"votedBy"	text,
	"ballots"	numeric,
	"needed"	numeric,
	"needed_note"	text
);

insert into halloffamereqs
select distinct yearid, votedby, ballots, needed, "needed_note"
from "raw".halloffame;

update halloffamereqs
set ballots = null
where ballots = "";

update halloffamereqs
set needed = null
where needed = "";

create table if not exists main."halloffamereqs" (
	"year"	numeric,
	"votedBy"	text,
	"ballots"	numeric,
	"needed"	numeric	check ("needed" <= "ballots"),
	"needed_note"	text,
	primary key("year","votedBy"),
	foreign key("year") references "seasons"("year")
);

insert into main.halloffamereqs select distinct * from temp.halloffamereqs;
commit;
