forked from dbt-labs/dbt-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelationships_where.sql
More file actions
55 lines (36 loc) · 1.21 KB
/
relationships_where.sql
File metadata and controls
55 lines (36 loc) · 1.21 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{% test relationships_where(
model, column_name, to, field, from_condition="1=1", to_condition="1=1"
) %}
{{
return(
adapter.dispatch("test_relationships_where", "dbt_utils")(
model, column_name, to, field, from_condition, to_condition
)
)
}}
{% endtest %}
{% macro default__test_relationships_where(
model, column_name, to, field, from_condition="1=1", to_condition="1=1"
) %}
{# T-SQL has no boolean data type so we use 1=1 which returns TRUE #}
{# ref https://stackoverflow.com/a/7170753/3842610 #}
with
left_table as (
select {{ column_name }} as id
from {{ model }}
where {{ column_name }} is not null and {{ from_condition }}
),
right_table as (
select {{ field }} as id
from {{ to }}
where {{ field }} is not null and {{ to_condition }}
),
exceptions as (
select left_table.id, right_table.id as right_id
from left_table
left join right_table on left_table.id = right_table.id
where right_table.id is null
)
select *
from exceptions
{% endmacro %}