forked from dbt-msft/dbt-sqlserver-utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
relationships_where.sql
54 lines (34 loc) · 1.08 KB
/
relationships_where.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{% macro test_relationships_where(model, to, field) %}
{{ return(adapter.dispatch('test_relationships_where', packages = dbt_utils._get_utils_namespaces())(model, to, field, **kwargs)) }}
{% endmacro %}
{% macro default__test_relationships_where(model, to, field) %}
{% set column_name = kwargs.get('column_name', kwargs.get('from')) %}
{# T-SQL has no boolean data type so we use 1=1 which returns TRUE #}
{# ref https://stackoverflow.com/a/7170753/3842610 #}
{% set from_condition = kwargs.get('from_condition', "1=1") %}
{% set to_condition = kwargs.get('to_condition', "1=1") %}
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 count(*) from exceptions
{% endmacro %}