Search code examples
dbt

How do we use the merge statement inside Incremental model for SCD 1 Implementation


I am trying to implement the scd1 in Data Build Tools using the Merge query. But I am not getting the exact way to write this query in Incremental model.

Can anyone show where we have to write the merge query.

{{
    config(
        materialized='incremental'
    )
}}

<MERGE QUERY HERE> 

{% if is_incremental() %}
where last_modified_date > '2020-07-11'
{% endif %}

Solution

  • You don't actually need to write the merge statement, dbt will take care of that for you. You should write a query that will return the records you want to upsert. You can then check the log for the merge query dbt generates.

    Also check out the dbt docs on configuring incremental models: https://docs.getdbt.com/docs/building-a-dbt-project/building-models/configuring-incremental-models#what-is-an-incremental_strategy