|
| 1 | +{% macro sqlserver__date_part(datepart, date) -%} |
| 2 | + |
| 3 | + {%- if datepart == "dayofweek" -%} |
| 4 | + datepart = "WEEKDAY" |
| 5 | + {%- elif datepart == "isoweek" -%} |
| 6 | + datepart = "ISO_WEEK" |
| 7 | + {%- endif -%} |
| 8 | + |
| 9 | + datepart({{ datepart }}, {{ date }}) |
| 10 | +{%- endmacro %} |
| 11 | + |
| 12 | +{%- macro sqlserver__week_end(date) -%} |
| 13 | + {%- set dt = dbt_date.week_start(date) -%} |
| 14 | + {{ dbt_date.n_days_away(6, dt) }} |
| 15 | +{%- endmacro %} |
| 16 | + |
| 17 | +{% macro sqlserver__date_spine_sql(datepart, start_date, end_date) %} |
| 18 | + |
| 19 | + |
| 20 | + with |
| 21 | + |
| 22 | + l0 as ( |
| 23 | + |
| 24 | + select c |
| 25 | + from (select 1 union all select 1) as d(c) |
| 26 | + |
| 27 | + ), |
| 28 | + l1 as ( |
| 29 | + |
| 30 | + select |
| 31 | + 1 as c |
| 32 | + from l0 as a |
| 33 | + cross join l0 as b |
| 34 | + |
| 35 | + ), |
| 36 | + |
| 37 | + l2 as ( |
| 38 | + |
| 39 | + select 1 as c |
| 40 | + from l1 as a |
| 41 | + cross join l1 as b |
| 42 | + ), |
| 43 | + |
| 44 | + l3 as ( |
| 45 | + |
| 46 | + select 1 as c |
| 47 | + from l2 as a |
| 48 | + cross join l2 as b |
| 49 | + ), |
| 50 | + |
| 51 | + l4 as ( |
| 52 | + |
| 53 | + select 1 as c |
| 54 | + from l3 as a |
| 55 | + cross join l3 as b |
| 56 | + ), |
| 57 | + |
| 58 | + l5 as ( |
| 59 | + |
| 60 | + select 1 as c |
| 61 | + from l4 as a |
| 62 | + cross join l4 as b |
| 63 | + ), |
| 64 | + |
| 65 | + nums as ( |
| 66 | + |
| 67 | + select row_number() over (order by (select null)) as rownum |
| 68 | + from l5 |
| 69 | + ), |
| 70 | + |
| 71 | + rawdata as ( |
| 72 | + |
| 73 | + select top ({{datediff(start_date, end_date, datepart)}}) rownum -1 as n |
| 74 | + from nums |
| 75 | + order by rownum |
| 76 | + ), |
| 77 | + |
| 78 | + all_periods as ( |
| 79 | + |
| 80 | + select ( |
| 81 | + {{ |
| 82 | + dateadd( |
| 83 | + datepart, |
| 84 | + 'n', |
| 85 | + start_date |
| 86 | + ) |
| 87 | + }} |
| 88 | + ) as date_{{datepart}} |
| 89 | + from rawdata |
| 90 | + ), |
| 91 | + |
| 92 | + filtered as ( |
| 93 | + |
| 94 | + select * |
| 95 | + from all_periods |
| 96 | + where date_{{datepart}} <= {{ end_date }} |
| 97 | + |
| 98 | + ) |
| 99 | + |
| 100 | + select * from filtered |
| 101 | + |
| 102 | +{% endmacro %} |
| 103 | + |
| 104 | + |
| 105 | +{% macro sqlserver__date_spine(datepart, start_date, end_date) -%} |
| 106 | + |
| 107 | + {% set date_spine_query %} |
| 108 | + |
| 109 | + {{tsql_utils.sqlserver__date_spine_sql(datepart, start_date, end_date)}} order by 1 |
| 110 | + |
| 111 | + {% endset %} |
| 112 | + |
| 113 | + |
| 114 | + {% set results = run_query(date_spine_query) %} |
| 115 | + |
| 116 | + {% if execute %} |
| 117 | + |
| 118 | + {% set results_list = results.columns[0].values() %} |
| 119 | + |
| 120 | + {% else %} |
| 121 | + |
| 122 | + {% set results_list = [] %} |
| 123 | + |
| 124 | + {% endif %} |
| 125 | + |
| 126 | + {%- for date_field in results_list %} |
| 127 | + select '{{ date_field }}' as date_{{datepart}} {{ 'union all ' if not loop.last else '' }} |
| 128 | + {% endfor -%} |
| 129 | + |
| 130 | +{% endmacro %} |
| 131 | + |
| 132 | +{% macro sqlserver__get_date_dimension(start_date, end_date) %} |
| 133 | + |
| 134 | +{%- set start_date="cast('" ~ start_date ~ "' as " ~ type_timestamp() ~ ")" -%} |
| 135 | +{%- set end_date="cast('" ~ end_date ~ "' as " ~ type_timestamp() ~ ")" -%} |
| 136 | + |
| 137 | +with base_dates as ( |
| 138 | + |
| 139 | + {{ dbt_utils.date_spine( |
| 140 | + datepart="day", |
| 141 | + start_date=start_date, |
| 142 | + end_date=end_date |
| 143 | + ) |
| 144 | + }} |
| 145 | + |
| 146 | +), |
| 147 | +dates_with_prior_year_dates as ( |
| 148 | + |
| 149 | + select |
| 150 | + cast(d.date_day as date) as date_day, |
| 151 | + cast({{ dateadd('year', -1 , 'd.date_day') }} as date) as prior_year_date_day, |
| 152 | + cast({{ dateadd('day', -364 , 'd.date_day') }} as date) as prior_year_over_year_date_day |
| 153 | + from |
| 154 | + base_dates d |
| 155 | + |
| 156 | +) |
| 157 | +select |
| 158 | + d.date_day, |
| 159 | + {{ dbt_date.yesterday('d.date_day') }} as prior_date_day, |
| 160 | + {{ dbt_date.tomorrow('d.date_day') }} as next_date_day, |
| 161 | + d.prior_year_date_day as prior_year_date_day, |
| 162 | + d.prior_year_over_year_date_day, |
| 163 | + |
| 164 | + DATEPART(WEEKDAY, d.date_day) as day_of_week, |
| 165 | + DATEPART(WEEKDAY, d.date_day) as day_of_week_iso, |
| 166 | + DATENAME(WEEKDAY, d.date_day) as day_of_week_name, |
| 167 | + LEFT(DATENAME(WEEKDAY, d.date_day), 3) as day_of_week_name_short, |
| 168 | + /* |
| 169 | + {{ dbt_date.day_of_week('d.date_day', isoweek=false) }} as day_of_week, |
| 170 | + {{ dbt_date.day_of_week('d.date_day', isoweek=true) }} as day_of_week_iso, |
| 171 | + {{ dbt_date.day_name('d.date_day', short=false) }} as day_of_week_name, |
| 172 | + {{ dbt_date.day_name('d.date_day', short=true) }} as day_of_week_name_short, |
| 173 | + */ |
| 174 | + {{ dbt_date.day_of_month('d.date_day') }} as day_of_month, |
| 175 | + {{ dbt_date.day_of_year('d.date_day') }} as day_of_year, |
| 176 | + |
| 177 | + {{ dbt_date.week_start('d.date_day') }} as week_start_date, |
| 178 | + {{ dbt_date.week_end('d.date_day') }} as week_end_date, |
| 179 | + {{ dbt_date.week_start('d.prior_year_over_year_date_day') }} as prior_year_week_start_date, |
| 180 | + {{ dbt_date.week_end('d.prior_year_over_year_date_day') }} as prior_year_week_end_date, |
| 181 | + {{ dbt_date.week_of_year('d.date_day') }} as week_of_year, |
| 182 | + |
| 183 | + /* |
| 184 | + {{ dbt_date.iso_week_start('d.date_day') }} as iso_week_start_date, |
| 185 | + {{ dbt_date.iso_week_end('d.date_day') }} as iso_week_end_date, |
| 186 | + {{ dbt_date.iso_week_start('d.prior_year_over_year_date_day') }} as prior_year_iso_week_start_date, |
| 187 | + {{ dbt_date.iso_week_end('d.prior_year_over_year_date_day') }} as prior_year_iso_week_end_date, |
| 188 | + {{ dbt_date.iso_week_of_year('d.date_day') }} as iso_week_of_year, |
| 189 | + */ |
| 190 | + |
| 191 | + {{ dbt_date.week_of_year('d.prior_year_over_year_date_day') }} as prior_year_week_of_year, |
| 192 | + /* |
| 193 | + {{ dbt_date.iso_week_of_year('d.prior_year_over_year_date_day') }} as prior_year_iso_week_of_year, |
| 194 | + */ |
| 195 | + cast({{ dbt_date.date_part('month', 'd.date_day') }} as {{ type_int() }}) as month_of_year, |
| 196 | + |
| 197 | + DATENAME(MONTH, d.date_day) as month_name, |
| 198 | + LEFT(DATENAME(MONTH, d.date_day), 3) as month_name_short, |
| 199 | + /* |
| 200 | + {{ dbt_date.month_name('d.date_day', short=false) }} as month_name, |
| 201 | + {{ dbt_date.month_name('d.date_day', short=true) }} as month_name_short, |
| 202 | + */ |
| 203 | + |
| 204 | + cast({{ date_trunc('month', 'd.date_day') }} as date) as month_start_date, |
| 205 | + cast({{ last_day('d.date_day', 'month') }} as date) as month_end_date, |
| 206 | + |
| 207 | + cast({{ date_trunc('month', 'd.prior_year_date_day') }} as date) as prior_year_month_start_date, |
| 208 | + cast({{ last_day('d.prior_year_date_day', 'month') }} as date) as prior_year_month_end_date, |
| 209 | + |
| 210 | + cast({{ dbt_date.date_part('quarter', 'd.date_day') }} as {{ type_int() }}) as quarter_of_year, |
| 211 | + cast({{ date_trunc('quarter', 'd.date_day') }} as date) as quarter_start_date, |
| 212 | + cast({{ last_day('d.date_day', 'quarter') }} as date) as quarter_end_date, |
| 213 | + |
| 214 | + cast({{ dbt_date.date_part('year', 'd.date_day') }} as {{ type_int() }}) as year_number, |
| 215 | + cast({{ date_trunc('year', 'd.date_day') }} as date) as year_start_date, |
| 216 | + cast({{ last_day('d.date_day', 'year') }} as date) as year_end_date |
| 217 | +from |
| 218 | + dates_with_prior_year_dates d |
| 219 | + |
| 220 | +{% endmacro %} |
0 commit comments