forked from dbt-labs/dbt-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fewer_rows_than.sql
45 lines (32 loc) · 1.08 KB
/
fewer_rows_than.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
{% test fewer_rows_than(model, compare_model) %}
{{ return(adapter.dispatch('test_fewer_rows_than', 'dbt_utils')(model, compare_model)) }}
{% endtest %}
{% macro default__test_fewer_rows_than(model, compare_model) %}
{{ config(fail_calc = 'coalesce(row_count_delta, 0)') }}
with a as (
select count(*) as count_our_model from {{ model }}
),
b as (
select count(*) as count_comparison_model from {{ compare_model }}
),
counts as (
select
count_our_model,
count_comparison_model
from a
cross join b
),
final as (
select *,
case
-- fail the test if we have more rows than the reference model and return the row count delta
when count_our_model > count_comparison_model then (count_our_model - count_comparison_model)
-- fail the test if they are the same number
when count_our_model = count_comparison_model then 1
-- pass the test if the delta is positive (i.e. return the number 0)
else 0
end as row_count_delta
from counts
)
select * from final
{% endmacro %}