{{ config(materialized='table',
    schema='C2D',
    alias='T_UC_AGGREGATED',
    tags=["m_MOPDB_C2D_UC_AGGREGATED"]
) }}

select 'MARKET_ASSET' AS DATASET,
    FILE_CREATION_DATE AS DATABASE_ENTRY_TIMESTAMP,
    SNAPSHOT_DATE, 
    REPORTING_NCB AS NCB,  
    MFI_ID, 
    ISIN_CODE,
    OTHER_REG_NO AS OTHER_REGISTRATION_NUMBER, 
    NOM_AMT_SUBMITTED AS NOMINAL_AMOUNT_SUBMITTED,
    COLL_BEFORE_HAIRCUTS AS COLL_VALUE_BEFORE_HAIRCUTS,  
    COLL_AFTER_HAIRCUTS AS COLL_VALUE_AFTER_HAIRCUTS,  
    TYPE_OF_SYSTEM AS SYSTEM_TYPE, 
    TYPE_OF_OPERATION, 
    DOM_OR_XBORDER AS DOM_OR_CB, 
    ISSUER_CAS AS ISSUER_CRED_ASSESS_SOURCE,
    ISSUER_RATING_ENUM_VALUE,
    ISSUER_RATING_NUMBER_VALUE,
    CAST(NULL AS VARCHAR2(18 CHAR)) AS NONMKT_ASSET_TYPE, 
    CAST(NULL AS NUMBER(10,0)) AS NO_OF_AGGREGATED_DEBTORS, 
    CAST(NULL AS VARCHAR2(50 CHAR)) AS ELIGIBLE_VIA_GUARANTOR,
    CAST(NULL AS VARCHAR2(200 CHAR)) AS DEBTOR_NAME,  
    CAST(NULL AS VARCHAR2(100 CHAR)) AS DEBTOR_ID, 
    CAST(NULL AS VARCHAR2(5 CHAR)) AS DEBTOR_GROUP, 
    CAST(NULL AS VARCHAR2(17 CHAR)) AS DEBTOR_CLASS, 
    CAST(NULL AS VARCHAR2(50 CHAR)) AS DEBTOR_RESIDENCE,
    CAST(NULL AS VARCHAR2(4 CHAR)) AS DEBTOR_CRED_ASSESS_SOURCE,
    CAST(NULL AS VARCHAR2(20 CHAR)) AS DEBTOR_RATING_ENUM_VALUE, 
    CAST(NULL AS NUMBER(9,8)) AS DEBTOR_RATING_NUMBER_VALUE, 
    CAST(NULL AS VARCHAR2(255 CHAR)) AS GUARANTOR_NAME, 
    CAST(NULL AS VARCHAR2(256 CHAR)) AS GUARANTOR_ID, 
    CAST(NULL AS VARCHAR2(50 CHAR)) AS GUARANTOR_RESIDENCE, 
    CAST(NULL AS VARCHAR2(4 CHAR)) AS GUARANTOR_CRED_ASSESS_SOURCE,
    CAST(NULL AS VARCHAR2(20 CHAR)) AS GUARANTOR_RATING_ENUM_VALUE, 
    CAST(NULL AS NUMBER(9,8)) AS GUARANTOR_RATING_NUMBER_VALUE, 
    CAST(NULL AS NUMBER(10,0)) AS NO_OF_AGGREGATED_ASSETS,
    NCB_COMMENT AS NCB_COMMENTS, 
    CAST(NULL AS DATE) AS MATURITY_DATE, 
    CAST(NULL AS VARCHAR2(50 CHAR)) AS GUARANTOR_CLASS, 
    ISSUER_CLASS,
    CAST(NULL AS VARCHAR2(8 CHAR))  AS INTEREST_PAYMENT_TYPE, 
    CAST(NULL AS VARCHAR2(5 CHAR))  AS RESIDUAL_MATURITY, 
    CAST(NULL AS VARCHAR2(22 CHAR))  AS BUCKET_SIZE, 
    CAST(NULL AS VARCHAR2(100 CHAR))  AS DEBTOR_CAS_PROVIDER, 
    CAST(NULL AS VARCHAR2(100 CHAR))  AS GUARANTOR_CAS_PROVIDER,
    ISSUER_CRED_PROVIDER AS ISSUER_CAS_PROVIDER,
    CAST(NULL AS VARCHAR2(1 CHAR))  AS SECURED,
    CAST(NULL AS VARCHAR2(10 CHAR))  AS NON_MARKET_DENOMINATION,
    MOBILISATION_CHANNEL, 
    CCB, 
    INVESTOR_SSS, 
    INTERMEDIARY_SSS, 
    ISSUER_SSS, 
    TRIPARTY_AGENT,
    CAST(NULL AS VARCHAR2(5 CHAR)) AS DEBTOR_IDTYPE,
    CAST(NULL AS VARCHAR2(1 CHAR)) AS CAP,
    CAST(NULL AS VARCHAR2(9 CHAR)) AS REFERENCE_RATE,
    CAST(NULL AS VARCHAR2(200 CHAR)) AS REFERENCE_RATE_COMMENT, 
    CAST(NULL AS VARCHAR2(1 CHAR)) AS RESET_PERIOD_MORE_ONE_YEAR, 
    CAST(NULL AS VARCHAR2(50 CHAR)) AS OA_ID,
    CAST(NULL AS VARCHAR2(60 CHAR)) AS CONTRACT_ID,
    CAST(NULL AS VARCHAR2(60 CHAR)) AS INSTRMNT_ID,
    {{ get_workflow_history_key() }} AS A_WORKFLOW_HISTORY_KEY
from {{ ref('m_MOPDB_C2D_T_UC_AGGREGATED_OU_C2D_UC_MA_DISSEM_SQ') }}

UNION

select 'NON_MARKET_DECC' AS DATASET,
    FILE_CREATION_DATE AS DATABASE_ENTRY_TIMESTAMP,
    SNAPSHOT_DATE,
    REPORTING_NCB AS NCB,
    MFI_ID,
    ISIN_CODE,
    CAST(NULL AS VARCHAR2(100 CHAR)) AS OTHER_REGISTRATION_NUMBER,
    NOM_AMT_SUBMITTED AS NOMINAL_AMOUNT_SUBMITTED,
    NOM_AMT_SUBMITTED AS COLL_VALUE_BEFORE_HAIRCUTS,
    NOM_AMT_SUBMITTED AS COLL_VALUE_AFTER_HAIRCUTS,
    TYPE_OF_SYSTEM AS SYSTEM_TYPE, 
    TYPE_OF_OPERATION, 
    DOM_OR_XBORDER AS DOM_OR_CB, 
    CAST(NULL AS VARCHAR2(50 CHAR)) AS ISSUER_CRED_ASSESS_SOURCE,
    CAST(NULL AS VARCHAR2(50 CHAR))  AS ISSUER_RATING_ENUM_VALUE,
    CAST(NULL AS NUMBER(9,8)) AS ISSUER_RATING_NUMBER_VALUE,
    NON_MKT_ASSET_TYPE AS NONMKT_ASSET_TYPE,
    CAST(NULL AS NUMBER(10,0)) AS NO_OF_AGGREGATED_DEBTORS,
    CAST(NULL AS VARCHAR2(50 CHAR)) AS ELIGIBLE_VIA_GUARANTOR,
    CAST(NULL AS VARCHAR2(200 CHAR)) AS DEBTOR_NAME,  
    CAST(NULL AS VARCHAR2(100 CHAR)) AS DEBTOR_ID, 
    CAST(NULL AS VARCHAR2(5 CHAR)) AS DEBTOR_GROUP, 
    CAST(NULL AS VARCHAR2(17 CHAR)) AS DEBTOR_CLASS, 
    CAST(NULL AS VARCHAR2(50 CHAR)) AS DEBTOR_RESIDENCE,
    CAST(NULL AS VARCHAR2(4 CHAR)) AS DEBTOR_CRED_ASSESS_SOURCE,
    CAST(NULL AS VARCHAR2(20 CHAR)) AS DEBTOR_RATING_ENUM_VALUE, 
    CAST(NULL AS NUMBER(9,8)) AS DEBTOR_RATING_NUMBER_VALUE, 
    CAST(NULL AS VARCHAR2(255 CHAR)) AS GUARANTOR_NAME, 
    CAST(NULL AS VARCHAR2(256 CHAR)) AS GUARANTOR_ID, 
    CAST(NULL AS VARCHAR2(50 CHAR)) AS GUARANTOR_RESIDENCE, 
    CAST(NULL AS VARCHAR2(4 CHAR)) AS GUARANTOR_CRED_ASSESS_SOURCE,
    CAST(NULL AS VARCHAR2(20 CHAR)) AS GUARANTOR_RATING_ENUM_VALUE, 
    CAST(NULL AS NUMBER(9,8)) AS GUARANTOR_RATING_NUMBER_VALUE, 
    CAST(NULL AS NUMBER(10,0)) AS NO_OF_AGGREGATED_ASSETS,
    NCB_COMMENT AS NCB_COMMENTS, 
    CAST(NULL AS DATE) AS MATURITY_DATE,
    CAST(NULL AS VARCHAR2(50 CHAR)) AS GUARANTOR_CLASS, 
    CAST(NULL AS VARCHAR2(8 CHAR)) AS ISSUER_CLASS,
    CAST(NULL AS VARCHAR2(8 CHAR)) AS INTEREST_PAYMENT_TYPE, 
    CAST(NULL AS VARCHAR2(5 CHAR)) AS RESIDUAL_MATURITY, 
    CAST(NULL AS VARCHAR2(22 CHAR)) AS BUCKET_SIZE, 
    CAST(NULL AS VARCHAR2(100 CHAR)) AS DEBTOR_CAS_PROVIDER, 
    CAST(NULL AS VARCHAR2(100 CHAR)) AS GUARANTOR_CAS_PROVIDER,
    CAST(NULL AS VARCHAR2(100 CHAR)) AS ISSUER_CAS_PROVIDER,
    CAST(NULL AS VARCHAR2(1 CHAR)) AS SECURED,
    CAST(NULL AS VARCHAR2(10 CHAR)) AS NON_MARKET_DENOMINATION,
    MOBILISATION_CHANNEL, 
    CCB, 
    INVESTOR_SSS, 
    INTERMEDIARY_SSS, 
    ISSUER_SSS, 
    TRIPARTY_AGENT,
    CAST(NULL AS VARCHAR2(5 CHAR)) AS DEBTOR_IDTYPE,
    CAST(NULL AS VARCHAR2(1 CHAR)) AS CAP, 
    CAST(NULL AS VARCHAR2(9 CHAR)) AS REFERENCE_RATE,  
    CAST(NULL AS VARCHAR2(200 CHAR)) AS REFERENCE_RATE_COMMENT, 
    CAST(NULL AS VARCHAR2(1 CHAR)) AS RESET_PERIOD_MORE_ONE_YEAR,  
    CAST(NULL AS VARCHAR2(50 CHAR)) AS OA_ID, 
    CAST(NULL AS VARCHAR2(60 CHAR)) AS CONTRACT_ID, 
    CAST(NULL AS VARCHAR2(60 CHAR)) AS INSTRMNT_ID,
    {{ get_workflow_history_key() }} AS A_WORKFLOW_HISTORY_KEY
from {{ ref('m_MOPDB_C2D_T_UC_AGGREGATED_OU_C2D_UC_NMA_DECC_DISSEM_SQ') }}

UNION

select 'NON_MARKET_ASSET' AS DATASET,
    FILE_CREATION_DATE AS DATABASE_ENTRY_TIMESTAMP,
    SNAPSHOT_DATE, 
    REPORTING_NCB AS NCB,  
    MFI_ID, 
    CAST(NULL AS VARCHAR2(50 CHAR)) AS ISIN_CODE,
    OTHER_REG_NO AS OTHER_REGISTRATION_NUMBER, 
    NOM_AMT_SUBMITTED AS NOMINAL_AMOUNT_SUBMITTED,
    COLL_BEFORE_HAIRCUTS AS COLL_VALUE_BEFORE_HAIRCUTS, 
    COLL_AFTER_HAIRCUTS AS COLL_VALUE_AFTER_HAIRCUTS, 
    TYPE_OF_SYSTEM AS SYSTEM_TYPE, 
    TYPE_OF_OPERATION, 
    DOM_OR_XBORDER AS DOM_OR_CB, 
    CAST(NULL AS VARCHAR2(50 CHAR)) AS ISSUER_CRED_ASSESS_SOURCE,
    CAST(NULL AS VARCHAR2(50 CHAR)) AS ISSUER_RATING_ENUM_VALUE,
    CAST(NULL AS NUMBER(9,8)) AS ISSUER_RATING_NUMBER_VALUE,
    NON_MKT_ASSET_TYPE AS NONMKT_ASSET_TYPE, 
    NO_AGGR_DEBTORS AS NO_OF_AGGREGATED_DEBTORS, 
    ELIGIBLE_VIA_GUAR AS ELIGIBLE_VIA_GUARANTOR,
    DEBTOR_NAME,  
    DEBTOR_ID, 
    DEBTOR_TYPE AS DEBTOR_GROUP, 
    DEBTOR_CLASS, 
    DEBTOR_RESIDENCE,
    DEBTOR_CAS AS DEBTOR_CRED_ASSESS_SOURCE,
    DEBTOR_RATING_ENUM_VALUE, 
    DEBTOR_RATING_NUMBER_VALUE, 
    GUAR_NAME AS GUARANTOR_NAME, 
    GUAR_ID AS GUARANTOR_ID, 
    GUAR_RESIDENCE AS GUARANTOR_RESIDENCE, 
    GUAR_CRED_CAS AS GUARANTOR_CRED_ASSESS_SOURCE,
    GUAR_RATING_ENUM_VALUE AS GUARANTOR_RATING_ENUM_VALUE, 
    GUAR_RATING_NUMBER_VALUE AS GUARANTOR_RATING_NUMBER_VALUE, 
    NO_AGGR_ASSETS AS NO_OF_AGGREGATED_ASSETS,
    NCB_COMMENT AS NCB_COMMENTS, 
    MATURITY_DATE, 
    GUAR_CLASS AS GUARANTOR_CLASS, 
    CAST(NULL AS VARCHAR2(8 CHAR)) AS ISSUER_CLASS,
    INTEREST_PAYMENT_TYPE, 
    RESIDUAL_MATURITY, 
    BUCKET_SIZE, 
    DEBTOR_CRED_PROV AS DEBTOR_CAS_PROVIDER, 
    GUAR_CRED_PROV AS GUARANTOR_CAS_PROVIDER,
    CAST(NULL AS VARCHAR2(100 CHAR)) AS ISSUER_CAS_PROVIDER,
    SECURED_FLAG AS SECURED,
    DENOMINATION AS NON_MARKET_DENOMINATION,
    MOBILISATION_CHANNEL, 
    CCB, 
    INVESTOR_SSS, 
    INTERMEDIARY_SSS, 
    ISSUER_SSS, 
    CAST(NULL AS VARCHAR2(6 CHAR)) AS TRIPARTY_AGENT,
    DEBTOR_ID_TYPE AS DEBTOR_IDTYPE,
    CAP, 
    REFERENCE_RATE, 
    REFERENCE_RATE_COMMENT, 
    RESET_PERIOD_MORE_ONE_YEAR, 
    OA_ID, 
    CONTRACT_ID, 
    INSTRMNT_ID,
    {{ get_workflow_history_key() }} AS A_WORKFLOW_HISTORY_KEY
from {{ ref('m_MOPDB_C2D_T_UC_AGGREGATED_OU_C2D_UC_NMA_DISSEM_SQ') }}
