From e10750f4b547d627274cf5c47b83a603173f0d46 Mon Sep 17 00:00:00 2001 From: Chuxin Huang Date: Fri, 12 Jul 2024 00:56:12 +1000 Subject: [PATCH] Add a utility staging table for calendar days (#1757) update Co-authored-by: Raymond Cheng --- .../staging/utility/stg_utility__calendar.sql | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 warehouse/dbt/models/staging/utility/stg_utility__calendar.sql diff --git a/warehouse/dbt/models/staging/utility/stg_utility__calendar.sql b/warehouse/dbt/models/staging/utility/stg_utility__calendar.sql new file mode 100644 index 000000000..a2a7ec194 --- /dev/null +++ b/warehouse/dbt/models/staging/utility/stg_utility__calendar.sql @@ -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