-- Databricks notebook source
-- 依赖事实表 tmp.tmp_ims_tf_fact_sales； 所以一定要放后面。
--基于Fact数据动态计算时间维度 最近五年
insert overwrite table dws.dws_ims_td_date
select distinct 
YM,
left(cast(YM as string),4) as YEAR,
right(cast(YM as string),2) as MONTH,
concat(
  'Q',quarter(
    date(
      concat(
        left(cast(YM as string),4),'-',right(cast(YM as string),2),'-01'
          )))) as QUARTER,
concat(
  left(cast(YM as string),4),'Q',quarter(
    date(
      concat(
        left(cast(YM as string),4),'-',right(cast(YM as string),2),'-01'
          )))) as YQ,
case when YM=(select max(YM) from tmp.tmp_ims_tf_fact_sales) then 'R' else right(cast(YM as string),2) end as DATE_FLAG,
case when YM%100>6 then concat(left(cast(YM as string),4),'H2') else concat(left(cast(YM as string),4),'H1') end as HALF_YEAR,
from_utc_timestamp(current_timestamp(),'UTC+8'),
from_utc_timestamp(current_timestamp(),'UTC+8')
from   tmp.tmp_ims_tf_fact_sales
where YM > (select max(YM)-900 from tmp.tmp_ims_tf_fact_sales)
order by YM desc
;