-
Notifications
You must be signed in to change notification settings - Fork 66
/
day_of_week.sql
113 lines (80 loc) · 2.62 KB
/
day_of_week.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
{%- macro day_of_week(date, isoweek=true) -%}
{{ adapter.dispatch('day_of_week', 'dbt_date') (date, isoweek) }}
{%- endmacro %}
{%- macro default__day_of_week(date, isoweek) -%}
{%- set dow = dbt_date.date_part('dayofweek', date) -%}
{%- if isoweek -%}
case
-- Shift start of week from Sunday (0) to Monday (1)
when {{ dow }} = 0 then 7
else {{ dow }}
end
{%- else -%}
{{ dow }} + 1
{%- endif -%}
{%- endmacro %}
{%- macro snowflake__day_of_week(date, isoweek) -%}
{%- if isoweek -%}
{%- set dow_part = 'dayofweekiso' -%}
{{ dbt_date.date_part(dow_part, date) }}
{%- else -%}
{%- set dow_part = 'dayofweek' -%}
case
when {{ dbt_date.date_part(dow_part, date) }} = 7 then 1
else {{ dbt_date.date_part(dow_part, date) }} + 1
end
{%- endif -%}
{%- endmacro %}
{%- macro bigquery__day_of_week(date, isoweek) -%}
{%- set dow = dbt_date.date_part('dayofweek', date) -%}
{%- if isoweek -%}
case
-- Shift start of week from Sunday (1) to Monday (2)
when {{ dow }} = 1 then 7
else {{ dow }} - 1
end
{%- else -%}
{{ dow }}
{%- endif -%}
{%- endmacro %}
{%- macro postgres__day_of_week(date, isoweek) -%}
{%- if isoweek -%}
{%- set dow_part = 'isodow' -%}
-- Monday(1) to Sunday (7)
cast({{ dbt_date.date_part(dow_part, date) }} as {{ dbt.type_int() }})
{%- else -%}
{%- set dow_part = 'dow' -%}
-- Sunday(1) to Saturday (7)
cast({{ dbt_date.date_part(dow_part, date) }} + 1 as {{ dbt.type_int() }})
{%- endif -%}
{%- endmacro %}
{%- macro redshift__day_of_week(date, isoweek) -%}
{%- set dow = dbt_date.date_part('dayofweek', date) -%}
{%- if isoweek -%}
case
-- Shift start of week from Sunday (0) to Monday (1)
when {{ dow }} = 0 then 7
else cast({{ dow }} as {{ dbt.type_bigint() }})
end
{%- else -%}
cast({{ dow }} + 1 as {{ dbt.type_bigint() }})
{%- endif -%}
{%- endmacro %}
{%- macro duckdb__day_of_week(date, isoweek) -%}
{{ return(dbt_date.postgres__day_of_week(date, isoweek)) }}
{%- endmacro %}
{%- macro spark__day_of_week(date, isoweek) -%}
{%- set dow = "dayofweek_iso" if isoweek else "dayofweek" -%}
{{ dbt_date.date_part(dow, date) }}
{%- endmacro %}
{%- macro trino__day_of_week(date, isoweek) -%}
{%- set dow = dbt_date.date_part('day_of_week', date) -%}
{%- if isoweek -%}
{{ dow }}
{%- else -%}
case
when {{ dow }} = 7 then 1
else {{ dow }} + 1
end
{%- endif -%}
{%- endmacro %}