DBT - Merge - Only update condition

41 Views Asked by At

Hi i'm currently working on a dbt solution that is using 2 sources that has to merge to a destination table, i'm using

{{
    config(
      unique_key = ['col1', 'col2', 'col3'],
      merge_exclude_columns = ['col1', 'col2', 'col3']
    )
}}

The thing is that i need to use other two tables to only update some rows (that match on this 3 columns (col1, col2 and col3)). I don't need to use a standard merge that inserts and updates the rows when the join is made but only update the rows when this matches,

How could i make this happen?

Thanks

1

There are 1 best solutions below

5
Klim On

If I understand you correctly, you need the LEFT JOIN operation, not the incremental dbt models. merge_exclude_columns is used for materialized='incremental' models, not views or tables.

I guess you need a dbt model like this:

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

SELECT
  a.*,
  b.*
FROM {{ source('source', 'table_1') }} a
LEFT JOIN {{ source('source', 'table_2') }} b
  ON
    b.col1 = a.col1
    b.col2 = a.col2
    b.col3 = a.col3

dbt will recreate this table from scratch on every run.

Please let me know if I misunderstood your question.