-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a utility staging table for calendar days (#1757)
update Co-authored-by: Raymond Cheng <[email protected]>
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
warehouse/dbt/models/staging/utility/stg_utility__calendar.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{{ config( | ||
materialized='table', | ||
partition_by={ | ||
"field": "date_timestamp", | ||
"data_type": "date", | ||
"granularity": "month", | ||
}, | ||
) }} | ||
|
||
{% set start_date = '2013-01-01' %} | ||
{% set end_date = '2099-12-31' %} | ||
|
||
with date_range as ( | ||
select | ||
date_add('{{ start_date }}', interval day day) as date_timestamp | ||
from | ||
unnest( | ||
generate_array(0, date_diff('{{ end_date }}', '{{ start_date }}', day)) | ||
) | ||
as day | ||
) | ||
|
||
select | ||
date(date_timestamp) as date_timestamp, | ||
date_trunc(date_timestamp, month) as first_day_of_month, | ||
last_day(date_timestamp, month) as last_day_of_month, | ||
format_date('%A', date_timestamp) as day_of_week, | ||
format_date('%B', date_timestamp) as month, | ||
extract(year from date_timestamp) as year, | ||
format_date('%Y-%m', date_timestamp) as year_month, | ||
coalesce( | ||
extract(dayofweek from date_timestamp) in (1, 7), | ||
false | ||
) as is_weekend | ||
from | ||
date_range |