Skip to content

Instantly share code, notes, and snippets.

@jeffreyaven
Last active January 29, 2023 01:58
Show Gist options
  • Save jeffreyaven/e4601e9d2cb4a1186390d47834d570f8 to your computer and use it in GitHub Desktop.
Save jeffreyaven/e4601e9d2cb4a1186390d47834d570f8 to your computer and use it in GitHub Desktop.
...
insertions as (
select
'insert' as dbt_change_type,
source_data.*,
{#- additional operational metadata fields #}
cast("{{ invocation_id }}" as STRING) as INSERT_PROCESS_ID,
cast(NULL as STRING) as UPDATE_PROCESS_ID,
{{ strategy.updated_at }} as EFFECTIVE_START_TIMESTAMP,
cast("{{ var('default_high_dttm') }}" as TIMESTAMP) as EFFECTIVE_END_TIMESTAMP,
{{ strategy.updated_at }} as INSERT_TIMESTAMP,
cast(NULL as TIMESTAMP) as UPDATE_TIMESTAMP
from insertions_source_data as source_data
left outer join snapshotted_data on snapshotted_data.dbt_unique_key = source_data.dbt_unique_key
where snapshotted_data.dbt_unique_key is null
or (
snapshotted_data.dbt_unique_key is not null
and (
{{ strategy.row_changed }}
)
)
),
updates as (
select
'update' as dbt_change_type,
source_data.*,
snapshotted_data.dbt_scd_id,
{#- additional operational metadata fields #}
snapshotted_data.INSERT_PROCESS_ID,
cast("{{ invocation_id }}" as STRING) as UPDATE_PROCESS_ID,
snapshotted_data.EFFECTIVE_START_TIMESTAMP,
{{ strategy.updated_at }} as EFFECTIVE_END_TIMESTAMP,
snapshotted_data.INSERT_TIMESTAMP,
{{ strategy.updated_at }} UPDATE_TIMESTAMP
from updates_source_data as source_data
join snapshotted_data on snapshotted_data.dbt_unique_key = source_data.dbt_unique_key
where (
{{ strategy.row_changed }}
)
)
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment