forked from dbt-labs/dbt_metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecondary_calculation_rolling.sql
37 lines (32 loc) · 1.13 KB
/
secondary_calculation_rolling.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
{% macro default__secondary_calculation_rolling(metric_name, dimensions, calc_config) %}
{% set calc_sql %}
{{ adapter.dispatch('aggregate_primary_metric', 'metrics')(calc_config.aggregate, metric_name) }}
over (
{% if dimensions -%}
partition by {{ dimensions | join(", ") }}
{% endif -%}
order by period
rows between {{ calc_config.interval - 1 }} preceding and current row
)
{% endset %}
{% do return (calc_sql) %}
{% endmacro %}
{% macro rolling(aggregate, interval, alias) %}
{% set missing_args = [] %}
{% if not aggregate %}
{% set _ = missing_args.append("aggregate") %}
{% endif %}
{% if not interval %}
{% set _ = missing_args.append("interval") %}
{% endif %}
{% if missing_args | length > 0 %}
{% do exceptions.raise_compiler_error( missing_args | join(", ") ~ ' not provided to period_over_period') %}
{% endif %}
{% do return ({
"calculation": "rolling",
"aggregate": aggregate,
"interval": interval,
"alias": alias
})
%}
{% endmacro %}