{% macro get_sql_t_collateral_prehook( model, source_model ) %}
    {% if var('tag_to_run') == 'm_MOPDB_C2D_T_COLLATERAL_ALL_SOURCES_OU_C2D_UC_MA_DISSEM' %}
    
        DELETE FROM {{ model }}  
        WHERE DATASET = 'MARKET_ASSET'
		AND SOURCE = 'C2D_UC' 
        AND (NCB, SNAPSHOT_DATE) in (
        SELECT DISTINCT
                src.reporting_ncb,
                src.snapshot_date
            FROM
                {{ source_model }} src
                full outer join (SELECT DISTINCT
                                    ncb,
                                    snapshot_date, 
                                    DATABASE_ENTRY_TIMESTAMP
                                FROM
                                {{ model }}  
                                WHERE
                                    ( ncb,
                                    snapshot_date ) IN (
                                        SELECT DISTINCT
                                            reporting_ncb,
                                            snapshot_date
                                        FROM
                                            {{ source_model }}
                                    )
                                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)
        )

    {% elif var('tag_to_run') == 'm_MOPDB_C2D_T_COLLATERAL_ALL_SOURCES_OU_C2D_UC_NMA_ACC' %}

        DELETE FROM {{ model }}  
        WHERE DATASET = 'NON_MARKET_ASSET'
        AND (NCB, SNAPSHOT_DATE) IN (
            SELECT DISTINCT reporting_ncb, snapshot_date
            FROM {{ source_model }}
            
    {% elif var('tag_to_run') == 'm_MOPDB_C2D_T_COLLATERAL_ALL_SOURCES_OU_C2D_UC_NMA_DECC_DISSEM' %}
    
        DELETE FROM {{ model }}  
        WHERE DATASET = 'NON_MARKET_DECC'
		AND SOURCE = 'C2D_UC' 
        AND (NCB, SNAPSHOT_DATE) in (
            SELECT DISTINCT
                    src.reporting_ncb,
                    src.snapshot_date
                FROM
                    {{ source_model }} src
                    full outer join (SELECT DISTINCT
                                        ncb,
                                        snapshot_date, 
                                        DATABASE_ENTRY_TIMESTAMP
                                    FROM
                                    {{ model }}  
                                    WHERE
                                        ( ncb,
                                        snapshot_date ) IN (
                                            SELECT DISTINCT
                                                reporting_ncb,
                                                snapshot_date
                                            FROM
                                                {{ source_model }}
                                        )
                                    AND DATASET = 'NON_MARKET_DECC'
                                    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)
            )
    {% elif var('tag_to_run') == 'm_MOPDB_C2D_T_COLLATERAL_ALL_SOURCES_OU_C2D_UC_NMA_DISSEM' %}
    
        DELETE FROM {{ model }}  
        WHERE DATASET = 'NON_MARKET_ASSET'
		AND SOURCE = 'C2D_UC' 
        AND (NCB, SNAPSHOT_DATE) in (
            SELECT DISTINCT
                    src.reporting_ncb,
                    src.snapshot_date
                FROM
                    {{ source_model }} src
                    full outer join (SELECT DISTINCT
                                        ncb,
                                        snapshot_date, 
                                        DATABASE_ENTRY_TIMESTAMP
                                    FROM
                                    {{ model }}  
                                    WHERE
                                        ( ncb,
                                        snapshot_date ) IN (
                                            SELECT DISTINCT
                                                reporting_ncb,
                                                snapshot_date
                                            FROM
                                                {{ source_model }}
                                        )
                                    AND DATASET = 'NON_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)
            )
    {% endif %}
{% endmacro %}