forked from dbt-labs/dbt-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathat_least_one.sql
More file actions
28 lines (20 loc) · 795 Bytes
/
at_least_one.sql
File metadata and controls
28 lines (20 loc) · 795 Bytes
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
{% test at_least_one(model, column_name, group_by_columns = []) %}
{{ return(adapter.dispatch('test_at_least_one', 'dbt_utils')(model, column_name, group_by_columns)) }}
{% endtest %}
{% macro default__test_at_least_one(model, column_name, group_by_columns) %}
{% if group_by_columns|length() > 0 %}
{% set select_gb_cols = group_by_columns|join(' ,') + ', ' %}
{% set groupby_gb_cols = 'group by ' + group_by_columns|join(',') %}
{% endif %}
select *
from (
select
{# In TSQL, subquery aggregate columns need aliases #}
{# thus: a filler col name, 'filler_column' #}
{{select_gb_cols}}
count({{ column_name }}) as filler_column
from {{ model }}
{{groupby_gb_cols}}
having count({{ column_name }}) = 0
) validation_errors
{% endmacro %}