Skip to content

Instantly share code, notes, and snippets.

@gnilrets
gnilrets / trange_join.sql
Last active October 27, 2024 12:02
dbt Snapshot Join
{% macro trange_join(left_model, left_fields, left_primary_key, right_models) %}
{#
This macro allows the user to join two or more snapshot models together on a common
key, with the result being a unique record for each distinct time range. For example,
Given left_model:
| {{ join_key }} | left_field | dbt_valid_from | dbt_valid_to |
| - | - | - | - |
| k1 | L1 | 2020-01-01 | 2020-01-05 |
| k1 | L2 | 2020-01-05 | 2999-12-31 |