forked from dbt-msft/dbt-sqlserver-utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cardinality_equality.sql
58 lines (45 loc) · 977 Bytes
/
cardinality_equality.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
55
56
57
58
{% macro test_cardinality_equality(model, to, field) %}
{{ return(adapter.dispatch('test_cardinality_equality', packages = dbt_utils._get_utils_namespaces())(model, to, field, **kwargs)) }}
{% endmacro %}
{% macro default__test_cardinality_equality(model, to, field) %}
{# T-SQL doesn't let you use numbers as aliases for columns #}
{# Thus, no "GROUP BY 1" #}
{% set column_name = kwargs.get('column_name', kwargs.get('from')) %}
with table_a as (
select
{{ column_name }},
count(*) as num_rows
from {{ model }}
group by {{ column_name }}
),
table_b as (
select
{{ field }},
count(*) as num_rows
from {{ to }}
group by {{ field }}
),
except_a as (
select *
from table_a
{{ dbt_utils.except() }}
select *
from table_b
),
except_b as (
select *
from table_b
{{ dbt_utils.except() }}
select *
from table_a
),
unioned as (
select *
from except_a
union all
select *
from except_b
)
select count(*)
from unioned
{% endmacro %}