数仓中常用的日期维表,每年生成一次,供大家参考。
- 建表DDL
CREATE TABLE IF NOT EXISTS dim_date
(
calendar_date DATETIME COMMENT '日期',
calendar_date_str STRING COMMENT '日期',
calendar_year STRING COMMENT '自然年',
calendar_year_cn STRING COMMENT '自然年,中文名',
fiscal_year STRING COMMENT '财务年',
fiscal_year_cn STRING COMMENT '财务年,中文名',
calendar_quarter STRING COMMENT '自然季度',
calendar_quarter_cn STRING COMMENT '自然季度,中文名',
fiscal_quarter STRING COMMENT '财务季度',
fiscal_quarter_cn STRING COMMENT '财务季度,中文名',
calendar_month STRING COMMENT '月份',
calendar_month_en STRING COMMENT '月份',
calendar_month_cn STRING COMMENT '月份',
calendar_week STRING COMMENT '周数',
calendar_weekday_en STRING COMMENT '工作日历',
calendar_weekday_cn STRING COMMENT '工作日历',
is_last_day_of_month BIGINT COMMENT '是否是每月最后一天',
is_leap_year BIGINT COMMENT '是否是闰年',
is_cn_holiday BIGINT COMMENT '是中国节假日 ',
is_weekend BIGINT COMMENT '是否周末 ',
is_cn_workday BIGINT COMMENT '是否是工作日',
year_week STRING COMMENT '自然年-周',
is_cn_first_workday_follow_holidy BIGINT COMMENT '是否是节假日后的第一个工作日,0-不是,1-是'
)
COMMENT 'dim层 通用日期维度表,粒度-天'
PARTITIONED BY
(
p_year STRING COMMENT '年分区(yyyy)'
);
- 生成维表脚本
WITH
v_gen_calendar AS
(
SELECT
t1_date.calendar_date
,TO_CHAR(t1_date.calendar_date,'yyyymmdd') AS calendar_date_str
,calendar_year
,CONCAT(t1_date.calendar_year,'年') AS calendar_year_cn
,fiscal_year
,CONCAT(t1_date.fiscal_year,'财年') AS fiscal_year_cn
,t1_date.calendar_quarter
,CASE
WHEN t1_date.calendar_quarter = 'Q1' THEN '第一季度'
WHEN t1_date.calendar_quarter = 'Q2' THEN '第二季度'
WHEN t1_date.calendar_quarter = 'Q3' THEN '第三季度'
WHEN t1_date.calendar_quarter = 'Q4' THEN '第四季度'
END AS calendar_quarter_cn
,t1_date.fiscal_quarter
,CASE
WHEN t1_date.fiscal_quarter = 'Q1' THEN '第一财季'
WHEN t1_date.fiscal_quarter = 'Q2' THEN '第二财季'
WHEN t1_date.fiscal_quarter = 'Q3' THEN '第三财季'
WHEN t1_date.fiscal_quarter = 'Q4' THEN '第四财季'
END AS fiscal_quarter_cn
,t1_date.calendar_month
,t1_date.calendar_month_en
,t1_date.calendar_month_cn
,t1_date.calendar_week
,t1_date.calendar_weekday_en
,t1_date.calendar_weekday_cn
,t1_date.is_last_day_of_month
,t1_date.is_leap_year
,0 AS is_cn_holiday
,t1_date.is_weekend
,0 AS is_cn_workday
,t1_date.year_week
,0 AS is_cn_first_workday_follow_holidy
,TO_CHAR(t1_date.calendar_date,'yyyy') AS