{% macro get_sql_t_collateral_ma_dissem() %}
    -- depends_on: {{ ref('m_MOPDB_C2D_T_COLLATERAL_ALL_SOURCES_OU_C2D_UC_MA_DISSEM_SQ') }}
    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,  -- to find
    COLL_AFTER_HAIRCUTS AS COLL_VALUE_AFTER_HAIRCUTS,  -- to find
    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, -- to find
    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,
    'C2D_UC' AS SOURCE,
    {{ get_workflow_history_key() }} AS A_WORKFLOW_HISTORY_KEY
    from {{ ref('m_MOPDB_C2D_T_COLLATERAL_ALL_SOURCES_OU_C2D_UC_MA_DISSEM_SQ') }}
    WHERE (REPORTING_NCB, SNAPSHOT_DATE) in (
        SELECT DISTINCT
                src.reporting_ncb,
                src.snapshot_date
            FROM
                {{ ref('m_MOPDB_C2D_T_COLLATERAL_ALL_SOURCES_OU_C2D_UC_MA_DISSEM_SQ') }} src
                full outer join (SELECT DISTINCT
                                    ncb,
                                    snapshot_date, 
                                    DATABASE_ENTRY_TIMESTAMP
                                FROM
                                {{ this }}
                                WHERE
                                    ( ncb,
                                    snapshot_date ) IN (
                                        SELECT DISTINCT
                                            reporting_ncb,
                                            snapshot_date
                                        FROM
                                            {{ ref('m_MOPDB_C2D_T_COLLATERAL_ALL_SOURCES_OU_C2D_UC_MA_DISSEM_SQ') }}
                                    )
                                AND DATASET = 'MARKET_ASSET'
                                AND SOURCE = 'C2D_UC'
                                GROUP BY
                                    ncb,
                                    snapshot_date,
                                    DATABASE_ENTRY_TIMESTAMP
        ) trg on src.reporting_ncb = trg.ncb
            and TRUNC(src.snapshot_date) = TRUNC(trg.SNAPSHOT_DATE)
            WHERE
                src.max_a_workflow_history_key = src.a_workflow_history_key
                and src.FILE_CREATION_DATE > NVL(trg.DATABASE_ENTRY_TIMESTAMP,src.FILE_CREATION_DATE - 1)
        )
    AND A_WORKFLOW_HISTORY_KEY = MAX_A_WORKFLOW_HISTORY_KEY
{% endmacro %}