forked from dbt-labs/dbt-audit-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompare_queries.sql
110 lines (69 loc) · 1.46 KB
/
compare_queries.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
{% macro compare_queries(a_query, b_query, primary_key=None, summarize=true) -%}
{{ return(adapter.dispatch('compare_queries', 'audit_helper')(a_query, b_query, primary_key, summarize)) }}
{%- endmacro %}
{% macro default__compare_queries(a_query, b_query, primary_key=None, summarize=true) %}
with a as (
{{ a_query }}
),
b as (
{{ b_query }}
),
a_intersect_b as (
select * from a
{{ dbt.intersect() }}
select * from b
),
a_except_b as (
select * from a
{{ dbt.except() }}
select * from b
),
b_except_a as (
select * from b
{{ dbt.except() }}
select * from a
),
all_records as (
select
*,
true as in_a,
true as in_b
from a_intersect_b
union all
select
*,
true as in_a,
false as in_b
from a_except_b
union all
select
*,
false as in_a,
true as in_b
from b_except_a
),
{%- if summarize %}
summary_stats as (
select
in_a,
in_b,
count(*) as count
from all_records
group by 1, 2
),
final as (
select
*,
round(100.0 * count / sum(count) over (), 2) as percent_of_total
from summary_stats
order by in_a desc, in_b desc
)
{%- else %}
final as (
select * from all_records
where not (in_a and in_b)
order by {{ primary_key ~ ", " if primary_key is not none }} in_a desc, in_b desc
)
{%- endif %}
select * from final
{% endmacro %}