stellar-models/macros/tests/sequence_gaps.sql
Eric Laurello 96e235632b
Some checks failed
docs_update / run_dbt_jobs (push) Has been cancelled
dbt_test_weekly / run_dbt_jobs (push) Has been cancelled
dbt_test_monthly / run_dbt_jobs (push) Has been cancelled
dbt_run_observability_full / run_dbt_jobs (push) Has been cancelled
2 days on gaps
2025-10-14 16:27:53 -04:00

38 lines
940 B
SQL

{% macro sequence_gaps(
table,
partition_by,
column
) %}
{%- set partition_sql = partition_by | join(", ") -%}
{%- set previous_column = "prev_" ~ column -%}
WITH source AS (
SELECT
{{ partition_sql + "," if partition_sql }}
{{ column }},
LAG(
{{ column }},
1
) over (
{{ "PARTITION BY " ~ partition_sql if partition_sql }}
ORDER BY
{{ column }} ASC
) AS {{ previous_column }}
FROM
{{ table }}
WHERE
block_timestamp::date <= current_date - 2
)
SELECT
{{ partition_sql + "," if partition_sql }}
{{ previous_column }},
{{ column }},
{{ column }} - {{ previous_column }}
- 1 AS gap
FROM
source
WHERE
{{ column }} - {{ previous_column }} <> 1
ORDER BY
gap DESC
{% endmacro %}