forked from dbt-labs/dbt-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
last_day.sql
45 lines (33 loc) · 1.18 KB
/
last_day.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
/*
This function has been tested with dateparts of month and quarters. Further
testing is required to validate that it will work on other dateparts.
*/
{% macro last_day(date, datepart) %}
{{ return(adapter.dispatch('last_day', 'dbt_utils') (date, datepart)) }}
{% endmacro %}
{%- macro default_last_day(date, datepart) -%}
cast(
{{dbt_utils.dateadd('day', '-1',
dbt_utils.dateadd(datepart, '1', dbt_utils.date_trunc(datepart, date))
)}}
as date)
{%- endmacro -%}
{% macro default__last_day(date, datepart) -%}
{{dbt_utils.default_last_day(date, datepart)}}
{%- endmacro %}
{% macro postgres__last_day(date, datepart) -%}
{%- if datepart == 'quarter' -%}
-- postgres dateadd does not support quarter interval.
cast(
{{dbt_utils.dateadd('day', '-1',
dbt_utils.dateadd('month', '3', dbt_utils.date_trunc(datepart, date))
)}}
as date)
{%- else -%}
{{dbt_utils.default_last_day(date, datepart)}}
{%- endif -%}
{%- endmacro %}
{# redshift should use default instead of postgres #}
{% macro redshift__last_day(date, datepart) %}
{{ return(dbt_utils.default__last_day(date, datepart)) }}
{% endmacro %}