数据仓库 - hive - 日期维表,日,周,月份,年份,节假日,工作日

数仓中常用的日期维表,每年生成一次,供大家参考。

  • 建表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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值