forked from dbt-labs/dbt-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequential_values.sql
33 lines (25 loc) · 931 Bytes
/
sequential_values.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{% test sequential_values(model, column_name, interval=1, datepart=None) %}
{{ return(adapter.dispatch('test_sequential_values', 'dbt_utils')(model, column_name, interval, datepart)) }}
{% endtest %}
{% macro default__test_sequential_values(model, column_name, interval=1, datepart=None) %}
with windowed as (
select
{{ column_name }},
lag({{ column_name }}) over (
order by {{ column_name }}
) as previous_{{ column_name }}
from {{ model }}
),
validation_errors as (
select
*
from windowed
{% if datepart %}
where not(cast({{ column_name }} as {{ dbt_utils.type_timestamp() }})= cast({{ dbt_utils.dateadd(datepart, interval, 'previous_' + column_name) }} as {{ dbt_utils.type_timestamp() }}))
{% else %}
where not({{ column_name }} = previous_{{ column_name }} + {{ interval }})
{% endif %}
)
select *
from validation_errors
{% endmacro %}