CREATE TABLE tranche_data (
       QuoteDate date,
       TrancheId integer,
       BasketId integer,
       Maturity	 date,
       Tenor varchar(4),
       RefBasketPrice float,
       BPS varchar(1),
       BasketDuration float,
       TQC varchar(1),
       TrancheDuration float,
       TrancheDelta float,
       CorrAtDetachment float,
       Basis float,
       QuoteSource varchar(2),
       Index varchar(4),
       Series smallint,
       Upfront float,
       Running float,
       Attach smallint,
       Detach smallint,
       IndexFactor float,
       CumulativeLoss float
);

GRANT ALL ON tranche_data TO mlpdb_user;

CREATE TABLE index_desc(
       BasketID integer,
       Index varchar(4),
       Series smallint,
       Maturity date,
       Tenor varchar(4),
       IndexFactor float,
       CumulativeLoss float,
       PRIMARY KEY(BasketID, Tenor)
);

GRANT ALL ON index_desc TO mlpdb_user;

CREATE TABLE tranche_desc(
       TrancheID integer,
       BasketID integer,
       Tenor varchar(4),
       Attach smallint,
       Detach smallint,
       Foreign Key(BasketID, Tenor) REFERENCES index_desc(BasketID, Tenor),
       Primary Key(TrancheID, Tenor)
);

GRANT ALL ON tranche_desc TO mlpdb_user;

CREATE TABLE quotes (
       QuoteDate date,
       TrancheID integer,
       BasketID integer,
       RefBasketPrice float,
       BasketDuration float,
       TrancheDuration float,
       TrancheDelta float,
       CorrAtDetachment float,
       Basis float,
       QuoteSource varchar(4),
       Upfront float,
       Running float,
       Tenor varchar(4),
       FOREIGN KEY(BasketID, Tenor) REFERENCES index_desc(BasketID, Tenor),
       FOREIGN KEY(TrancheID, Tenor) REFERENCES tranche_desc(TrancheID, Tenor)
);

GRANT ALL ON quotes TO mlpdb_user;


-- Idiosyncracies:

-- for Itraxx 9 index:

--     tr.id	coupon type
-- 3-6 98155	all running
--     98154	U+500
-- 6-9 98160	all running
--     166395 	U+300
--     98159	U+500

-- For IG 9 index:

--        tr.id	coupon type
-- 3-7    162290	all running
--        162291	U+500
-- 7-10   162298	all running
--        162300	U+500
-- 10-15  162301	all running
--        162304	U+500
-- 15-30  162305	all running
--        162306	U+500
-- 30-100 162307	all running
--        162308	U+500

-- All other tranches should have a unique trancheid for a given basketid, attach, detach combination
