dbt Analytics Engineering (50 Qs)•Part 2: Advanced Modeling, Incremental Builds & Macros (Mid-Level)
What are dbt Snapshots and how do they implement SCD Type 2?
junior LevelCode ExampledbtAdvanced Modeling
In Simple Words: Source databases often overwrite records (SCD Type 1). dbt Snapshots record historical mutations over time to maintain an immutable audit trail (SCD Type 2).
timestampStrategy: Compares anupdated_atcolumn to detect mutations.checkStrategy: Calculates a checksum over specified columns to detect edits when no timestamp column exists.
sql
-- snapshots/snap_customers.sql
{% snapshot snap_customers %}
{{ config(
target_database='analytics',
target_schema='snapshots',
unique_key='customer_id',
strategy='timestamp',
updated_at='updated_at'
) }}
SELECT * FROM {{ source('crm', 'customers') }}
{% endsnapshot %}
dbt automatically appends columns: dbt_valid_from, dbt_valid_to, and dbt_updated_at.