--
-- Properly set up CSDB DEVO Instrument Ratings
--

-- 1. Create Template table CT_TEMPLTATE_TABLES.CSDB_DEBT_DAILY

drop table "CT_ET_TEMPLATES"."CSDB_DEBT_DAILY";
CREATE TABLE "CT_ET_TEMPLATES"."CSDB_DEBT_DAILY" (
    "A_KEY"                          NUMBER(38, 0)
        NOT NULL ENABLE,
    "A_WORKFLOW_HISTORY_KEY"         NUMBER(38, 0)
        NOT NULL ENABLE,
    "NEWUPDATED"                     DATE,
    "IDLOADDATE_DIM"                 DATE,
        "EXTERNALCODE_ISIN"              VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "EXTERNALCODETYPE_NC"            VARCHAR2(124 CHAR) COLLATE "USING_NLS_COMP",
        "EXTERNALCODE_NATIONAL"          VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
    "IDIRINSTRUMENT"                 NUMBER(28, 0),
        "SHORTNAME"                      VARCHAR2(255 CHAR) COLLATE "USING_NLS_COMP",
        "IDIRDEPOSITORY"                 VARCHAR2(1 CHAR) COLLATE "USING_NLS_COMP",
        "IDIRDEBTTYPE"                   VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "IDIRASSETSECTYPE"               VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "IDIRCLASSIFICATIONCODE_CFI"     VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "IDIRCLASSIFICATIONCODE_ESAI"    VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "IDIRCLASSIFICATIONCODE_ESAI_DM" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "IDIRCURRENCY_NOMINAL"           VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
    "AMOUNTISSUED"                   NUMBER(28, 10),
    "AMOUNTOUTSTANDING"              NUMBER(28, 10),
    "AMOUNTOUTSTANDING_EUR"          NUMBER(28, 10),
    "POOLFACTOR"                     NUMBER(28, 10),
    "ISSUEPRICE"                     NUMBER(28, 10),
    "IDISSUEDATE"                    DATE,
        "IDIRCOUPONTYPE"                 VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "IDIRCOUPONFREQUENCY"            VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "IDIRCURRENCY_COUPON"            VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
    "COUPONRATE"                     NUMBER(28, 10),
    "COUPONDATE"                     DATE,
        "IDIRREDEMPTIONTYPE"             VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "IDIRREDEMPTIONFREQUENCY"        VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "IDIRCURRENCY_REDEMPTION"        VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
    "REDEMPTIONPRICE"                NUMBER(28, 10),
    "IDMATURITYDATE"                 DATE,
        "IDIRORGANISATIONALIASTYPE_IS"   VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "ISSUERSOURCECODE"               VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "ISSUEREXTERNALCODE_MFI"         VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "ISSUEREXTERNALCODE_BIC"         VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "ISSUEREXTERNALCODE_BEI"         VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
    "IDIRORGANISATION_ISSUER"        NUMBER(28, 0),
        "ISSUERNAME"                     VARCHAR2(255 CHAR) COLLATE "USING_NLS_COMP",
        "IDIRCOUNTRY"                    VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "IDIRCOUNTRY_DM"                 VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "IDIRCLASSIFICATIONCODE_ESAO"    VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "IDIRCLASSIFICATIONCODE_ESAO_DM" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "IDIRCLASSIFICATIONCODE_NACE"    VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
    "PUBLICATIONPRICEDATE"           DATE,
    "PUBLICATIONPRICE"               NUMBER(28, 10),
        "PUBLICATIONPRICETYPE"           VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "PUBLICATIONPRICEQUOTATIONBASIS" VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
    "MONTHLYAVERAGEPRICE"            NUMBER(28, 10),
    "ACCRUALSTARTDATE"               DATE,
    "DEBTACCRUALDEBTOR"              NUMBER(28, 10),
        "DEBTACCRUALDEBTOR_DM"           VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
    "DEBTACCRUALCREDITOR"            NUMBER(28, 10),
        "DEBTACCRUALCREDITOR_TYP"        VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
    "ACCRUEDINTEREST"                NUMBER(28, 10),
    "YTMNONOPTIONADJUSTED"           NUMBER(28, 10),
        "ESCB_ISSUER_IDENT"              VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "ESCB_ISSUER_IDENT_TYP"          VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
    "IDUDCMPPARTY"                   NUMBER(28, 0),
        "AMOUNTOUTSTANDINGTYPE"          VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
    "MARKETCAPITALISATION"           NUMBER(28, 10),
    "MARKETCAPITALISATION_EUR"       NUMBER(28, 10),
        "VA_SECURITYSTATUS"              VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "VA_INSTRSUPPLEMENTARYCLASS"     VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "VA_RESIDUALMATURITYCLASS"       VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "VA_ISINSEC"                     VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "VA_ISELIGIBLEFOREADB"           VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "IDIRCLASSIFICATIONCODE_ESAI10"  VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "IDIRCLASSIFICATIONCODE_ESAO10"  VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "IDIRDEBTTYPE_N"                 VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "SENIORITY"                      VARCHAR2(32 CHAR) COLLATE "USING_NLS_COMP",
        "ISSUEREXTERNALCODE_LEI"         VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "INSTR_ESA2010_CLASS_VALUETYPE"  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "ISS_ESA2010_CLASS_VALUETYPE"    VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
    "SEC_STATUS_DATE"                DATE,
        "GROUP_TYPE"                     VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "HAS_EMBEDDED_OPTION"            VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "VOLUME_TRADED"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PRIMARY_LISTING_NAME"           VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PRIM_LISTING_RESIDENCY_COUNTRY" VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "INSTR_PORTFOLIO_FLAGS"          VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
    "BOND_DURATION"                  NUMBER(28, 10),
        "RESIDUAL_MATURITY"              VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "ORIGINAL_MATURITY"              VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "CFIN_CLASSIFICATION"            VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "COUPONFIRSTPAYMENTDATE"         VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "COUPONLASTPAYMENTDATE"          VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "COUPONRATEUNDERLYINGCODE_ISIN"  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "COUPONRATESPREAD"               VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "COUPONRATEMULTIPLIER"           VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "COUPONRATECAP"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "COUPONRATEFLOOR"                VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "IDISSUEDATE_TRANCHE"            VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "ISSUEPRICE_TRANCHE"             VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "VA_ISPRIVATEPLACEMENT"          VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "RIAD_CODE"                      VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
    "RIAD_OUID"                      NUMBER(38, 0),
        "ESG1"                           VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "ESG2"                           VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "ESG3"                           VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "STRIP"                          VARCHAR2(1 CHAR) COLLATE "USING_NLS_COMP",
        "DEPOSITORY_RECEIPT"             VARCHAR2(1 CHAR) COLLATE "USING_NLS_COMP",
        "RULE_144A"                      VARCHAR2(1 CHAR) COLLATE "USING_NLS_COMP",
        "REG_S"                          VARCHAR2(1 CHAR) COLLATE "USING_NLS_COMP",
        "WARRANT"                        VARCHAR2(1 CHAR) COLLATE "USING_NLS_COMP",
        "CSEC_RELEVANCE_STOCK"           VARCHAR2(1 CHAR) COLLATE "USING_NLS_COMP",
        "CSEC_RELEVANCE_GROSS_ISSUANCE"  VARCHAR2(1 CHAR) COLLATE "USING_NLS_COMP",
        "CSEC_RELEVANCE_REDEMPTION"      VARCHAR2(1 CHAR) COLLATE "USING_NLS_COMP",
    "ACCRUING_COUPON"                NUMBER(28, 10),
    "ACCRUING_DISCOUNT"              NUMBER(28, 10),
        "PLACEHOLDER32"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PLACEHOLDER33"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PLACEHOLDER34"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PLACEHOLDER35"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PLACEHOLDER36"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PLACEHOLDER37"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PLACEHOLDER38"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PLACEHOLDER39"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PLACEHOLDER40"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PLACEHOLDER41"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PLACEHOLDER42"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PLACEHOLDER43"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PLACEHOLDER44"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PLACEHOLDER45"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PLACEHOLDER46"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PLACEHOLDER47"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PLACEHOLDER48"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PLACEHOLDER49"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP",
        "PLACEHOLDER50"                  VARCHAR2(4000 CHAR) COLLATE "USING_NLS_COMP"
) DEFAULT COLLATION "USING_NLS_COMP"
SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 10 MAXTRANS 255 COLUMN STORE COMPRESS FOR QUERY HIGH ROW LEVEL LOCKING LOGGING
    STORAGE ( INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT
    FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT )
TABLESPACE "DATA";


-- 2. Create external tables

EXEC CT_MRDS.FILE_MANAGER.CREATE_EXTERNAL_TABLE('CSDB_DEBT_DAILY_INBOX','CT_ET_TEMPLATES.CSDB_DEBT_DAILY','INBOX/CSDB/DEBT_DAILY/DEBT_DAILY');
EXEC CT_MRDS.FILE_MANAGER.CREATE_EXTERNAL_TABLE('CSDB_DEBT_DAILY_ODS','CT_ET_TEMPLATES.CSDB_DEBT_DAILY','ODS/CSDB/DEBT_DAILY');
EXEC CT_MRDS.FILE_MANAGER.CREATE_EXTERNAL_TABLE('CSDB_DEBT_DAILY_ARCHIVE','CT_ET_TEMPLATES.CSDB_DEBT_DAILY','ARCHIVE/CSDB/DEBT_DAILY');

-- 3. Export table OU_CSDB.DEBT_DAILY

EXEC CT_MRDS.FILE_MANAGER.EXPORT_TABLE_DATA_BY_DATE('OU_CSDB','DEBT_DAILY','A_WORKFLOW_HISTORY_KEY','mrds_inbox_tst','ARCHIVE/CSDB/DEBT_DAILY');

-- 4. Rename table to legacy table

ALTER TABLE OU_CSDB.DEBT_DAILY RENAME TO DEBT_DAILY_LEGACY;

-- 5. Create OU view on top of ODS table

GRANT SELECT ON ods.CSDB_DEBT_DAILY_ODS TO OU_CSDB;
create or replace view OU_CSDB.RATINGS_DEBT_DAILY AS SELECT * from ods.CSDB_DEBT_DAILY_ODS;
show errors
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('CT_ET_TEMPLATES.DEBT_DAILY','NEWUPDATED','DD/MM/YYYY');
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('CT_ET_TEMPLATES.DEBT_DAILY','IDLOADDATE_DIM','DD/MM/YYYY');
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('CT_ET_TEMPLATES.DEBT_DAILY','IDISSUEDATE','DD/MM/YYYY');
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('CT_ET_TEMPLATES.DEBT_DAILY','COUPONDATE','DD/MM/YYYY');
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('CT_ET_TEMPLATES.DEBT_DAILY','IDMATURITYDATE','DD/MM/YYYY');
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('CT_ET_TEMPLATES.DEBT_DAILY','PUBLICATIONPRICEDATE','DD/MM/YYYY');
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('CT_ET_TEMPLATES.DEBT_DAILY','ACCRUALSTARTDATE','DD/MM/YYYY');
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('CT_ET_TEMPLATES.DEBT_DAILY','SEC_STATUS_DATE','DD/MM/YYYY');
