0

I'm trying to create a merge statement using dbt. Here is my code:

{{
    config(
        materialized='incremental',
        unique_key=(['five9_calls_hk', 'effective_from']),
        incremental_strategy='merge'
    )
}}

WITH

using_clause AS (

select 
    ns.five9_calls_hk,
    ns.effective_from,
    ns.next_effective_to::timestamp_tz(9) effective_to
from
(
select 
    five9_calls_hk,
    effective_from,
    effective_to,
    lead(effective_from) over (partition by five9_calls_hk order by load_datetime) lef,
    row_number() over (partition by five9_calls_hk order by load_datetime desc) rnum,
    ifnull(dateadd(millisecond, -1, lead(effective_from) over (partition by five9_calls_hk order by load_datetime)), cast('9999-12-31' as timestamp)) next_effective_to
--select * 
    from {{ ref('five9_calls_sat') }}
--    where call_id = '8344511'
order by five9_calls_hk, effective_from desc
) ns
join {{ ref('five9_calls_sat') }} s
on ns.five9_calls_hk = s.five9_calls_hk and ns.effective_from = s.effective_from
and ns.effective_to <> ns.next_effective_to
except
select five9_calls_hk, effective_from, effective_to from five9_calls_sat
),

updates AS (

    SELECT
        five9_calls_hk,
        effective_from,
        effective_to

    FROM using_clause

)

SELECT *

FROM updates

Running this, under the name "five9_calls_mrg" ultimately creates this statement:

merge into DEV_DW_DB.dbt_loswood.five9_calls_mrg as DBT_INTERNAL_DEST
        using DEV_DW_DB.dbt_loswood.five9_calls_mrg__dbt_tmp as DBT_INTERNAL_SOURCE
        on (
                    DBT_INTERNAL_SOURCE.five9_calls_hk = DBT_INTERNAL_DEST.five9_calls_hk
                ) and (
                    DBT_INTERNAL_SOURCE.effective_from = DBT_INTERNAL_DEST.effective_from
                )

    
    when matched then update set
        "FIVE9_CALLS_HK" = DBT_INTERNAL_SOURCE."FIVE9_CALLS_HK","EFFECTIVE_FROM" = DBT_INTERNAL_SOURCE."EFFECTIVE_FROM","EFFECTIVE_TO" = DBT_INTERNAL_SOURCE."EFFECTIVE_TO"
    

    when not matched then insert
        ("FIVE9_CALLS_HK", "EFFECTIVE_FROM", "EFFECTIVE_TO")
    values
        ("FIVE9_CALLS_HK", "EFFECTIVE_FROM", "EFFECTIVE_TO")

My problem is that I don't want to merge it into five9_calls_mrg, I want to merge it into five9_calls_sat. But I already have a different model called five9_calls_sat (the model that builds and loads that table). So I can't name this five9_calls_sat. How do I deal with that? Is there an element in the config that I can use?

Scott Wood
  • 1,077
  • 3
  • 18
  • 34

1 Answers1

0

Incremental models in DBT handle both the initial creation of the table and the updates over time. You want to move your five9_calls_mrg logic into five_calls_sat. You should be able to significantly simplify your combined model than what I have below, but here is a very ugly version:

{{
    config(
        materialized='incremental',
        unique_key=(['five9_calls_hk', 'effective_from']),
        incremental_strategy='merge'
    )
}}

{% if is_incremental() %}
WITH

using_clause AS (

select 
    ns.five9_calls_hk,
    ns.effective_from,
    ns.next_effective_to::timestamp_tz(9) effective_to
from
(
select 
    five9_calls_hk,
    effective_from,
    effective_to,
    lead(effective_from) over (partition by five9_calls_hk order by load_datetime) lef,
    row_number() over (partition by five9_calls_hk order by load_datetime desc) rnum,
    ifnull(dateadd(millisecond, -1, lead(effective_from) over (partition by five9_calls_hk order by load_datetime)), cast('9999-12-31' as timestamp)) next_effective_to
--select * 
    from {{ this }}
--    where call_id = '8344511'
order by five9_calls_hk, effective_from desc
) ns
join {{ this }} s
on ns.five9_calls_hk = s.five9_calls_hk and ns.effective_from = s.effective_from
and ns.effective_to <> ns.next_effective_to
except
select five9_calls_hk, effective_from, effective_to from {{ this }}
),

updates AS (

    SELECT
        five9_calls_hk,
        effective_from,
        effective_to

    FROM using_clause

)

SELECT *

FROM updates
{% else %}
// logic for five9_calls_sat here
{% endif %}
Adam Kipnis
  • 10,175
  • 10
  • 35
  • 48
  • So this is great. I've tried creating it but the merge statement (in addition to failing for reasons not known at the moment), is updated all of the columns. I just want it to update the effective_to column. – Scott Wood Mar 17 '23 at 16:40
  • I've seen that. But I don't want to update the entire table. I just want to update one column. Sigh. – Scott Wood Mar 17 '23 at 18:07
  • @ScottWood deleted my previous comment. Take a look at this: https://docs.getdbt.com/docs/build/incremental-models#strategy-specific-configs – Adam Kipnis Mar 22 '23 at 17:07
  • Depending on the adapter/version, @ScottWood, I think you are just looking to add the `merge_update_columns` to your config, with the only column you want to update – Shah Mar 23 '23 at 01:12