data-dwh-dbt-project/macros/calculate_aggregation_between_preceeding_x_and_y.sql

20 lines
600 B
MySQL
Raw Normal View History

/*
This macro calculates the aggregation of a metric over a partition of a column,
ordered by another column, with a window of x and y preceeding rows.
It's designed to be placed within a SELECT statement.
*/
{% macro calculate_aggregation_between_preceeding_x_and_y(
metric,
aggregation="sum",
partition_by="id_deal",
order_by="date",
x=12,
y=1
) %}
{{ aggregation }} ({{ metric }}) over (
partition by {{ partition_by }}
order by {{ order_by }} rows between {{ x }} preceding and {{ y }} preceding
) as {{ aggregation }}_{{ metric }}
{% endmacro %}