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

-- 1. Create Template table CT_TEMPLTATE_TABLES.CSDB_RT_INSTRUMENT_RATING

CREATE TABLE CT_ET_TEMPLATES.CSDB_RT_INSTRUMENT_RATING
   AS SELECT * FROM OU_CSDB.RATINGS_RT_INSTRUMENT_RATING WHERE 1=2;

-- 2. Create external tables

EXEC CT_MRDS.FILE_MANAGER.CREATE_EXTERNAL_TABLE('CSDB_RT_INSTRUMENT_RATING_INBOX','CT_ET_TEMPLATES.CSDB_RT_INSTRUMENT_RATING','INBOX/CSDB/RT_INSTRUMENT_RATING/RT_INSTRUMENT_RATING');
EXEC CT_MRDS.FILE_MANAGER.CREATE_EXTERNAL_TABLE('CSDB_RT_INSTRUMENT_RATING_ODS','CT_ET_TEMPLATES.CSDB_RT_INSTRUMENT_RATING','ODS/CSDB/RT_INSTRUMENT_RATING');
EXEC CT_MRDS.FILE_MANAGER.CREATE_EXTERNAL_TABLE('CSDB_RT_INSTRUMENT_RATING_ARCHIVE','CT_ET_TEMPLATES.CSDB_RT_INSTRUMENT_RATING','ARCHIVE/CSDB/RT_INSTRUMENT_RATING');

-- 3. Export table OU_CSDB.RT_INSTRUMENT_RATING

EXEC CT_MRDS.FILE_MANAGER.EXPORT_TABLE_DATA_BY_DATE('OU_CSDB','RT_INSTRUMENT_RATING','A_ETL_LOAD_SET_FK','mrds_inbox_tst','ARCHIVE/CSDB/RT_INSTRUMENT_RATING');

-- 4. Rename table to legacy table

ALTER TABLE OU_CSDB.RATINGS_RT_INSTRUMENT_RATING RENAME TO RATINGS_RT_INSTR_RATING_LEGACY;

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

GRANT SELECT ON ods.CSDB_RT_INSTRUMENT_RATING_ODS TO OU_CSDB;
create or replace view OU_CSDB.RATINGS_RT_INSTRUMENT_RATING AS SELECT * from ods.CSDB_RT_INSTRUMENT_RATING_ODS;
show errors
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('RT_INSTRUMENT_RATING','RT_INSTRUMENT_RATING','CORRECTFROM','YYYY-MM-DD HH24:MI:SS');
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('RT_INSTRUMENT_RATING','RT_INSTRUMENT_RATING','VALIDUNTIL','YYYY-MM-DD HH24:MI:SS');
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('RT_INSTRUMENT_RATING','RT_INSTRUMENT_RATING','TEC_EXECUTION_DATE','YYYY-MM-DD HH24:MI:SS');
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('RT_INSTRUMENT_RATING','RT_INSTRUMENT_RATING','TEC_INGESTION_DATE','YYYY-MM-DD HH24:MI:SS');
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('RT_INSTRUMENT_RATING','RT_INSTRUMENT_RATING','VALIDFROM','YYYY-MM-DD HH24:MI:SS');
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('RT_INSTRUMENT_RATING','RT_INSTRUMENT_RATING','BATCH_LAST_UPDATED_DATE','YYYY-MM-DD HH24:MI:SS');
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('RT_INSTRUMENT_RATING','RT_INSTRUMENT_RATING','CORRECTUNTIL','YYYY-MM-DD HH24:MI:SS');
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('RT_INSTRUMENT_RATING','RT_INSTRUMENT_RATING','OUTLOOK_DATE','YYYY-MM-DD HH24:MI:SS');
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('RT_INSTRUMENT_RATING','RT_INSTRUMENT_RATING','RATING_ACTION_DATE','YYYY-MM-DD HH24:MI:SS');
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('RT_INSTRUMENT_RATING','RT_INSTRUMENT_RATING','RATING_DATE','YYYY-MM-DD HH24:MI:SS');
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('RT_INSTRUMENT_RATING','RT_INSTRUMENT_RATING','TEC_BUSINESS_DATE','YYYY-MM-DD HH24:MI:SS');
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('RT_INSTRUMENT_RATING','RT_INSTRUMENT_RATING','BATCH_FIRST_CREATED_DATE','YYYY-MM-DD HH24:MI:SS');
EXEC CT_MRDS.FILE_MANAGER.ADD_COLUMN_DATE_FORMAT('RT_INSTRUMENT_RATING','RT_INSTRUMENT_RATING','WATCHLIST_DATE','YYYY-MM-DD HH24:MI:SS');
