日期维表结构如下
思路:
- 依据天数来进行补齐,小于33天的依次用最后日期拼上对应的天数
- 当天数为28天时,年-月-日 29天,
年-月-日 30天,
年-月-日 31天,
年-月-日 32天,
年-月-日 33天, - 使用union all 将其它天数的处理结果上下拼接
- 排除日期为null的行
select
f.zdate,
f.num
from
(select
case when count(t1.zdate) = '28' then concat(t1.yearmonth,'-28') else null end as zdate,
'29' as num
from db_cfg_dev.cfg_period_d t1
group by
yearmonth
union all
select
case when count(t1.zdate) = '28' then concat(t1.yearmonth,'-28') else null end as zdate,
'30' as num
from db_cfg_dev.cfg_period_d t1
group by
yearmonth
union all
select
case when count(t1.zdate) = '28' then concat(t1.yearmonth,'-28') else null end as zdate,
'31' as num
from db_cfg_dev.cfg_period_d t1
group by
yearmonth
union all
select
case when count(t1.zdate) = '28' then concat(t1.yearmonth,'-28') else null end as zdate,
'32' as num
from db_cfg_dev.cfg_period_d t1
group by
yearmonth
union all
select
case when count(t1.zdate) = '28' then concat(t1.yearmonth,'-28') else null end as zdate,
'33' as num
from db_cfg_dev.cfg_period_d t1
group by
yearmonth
union all
select
case when count(t1.zdate) = '29' then concat(t1.yearmonth,'-29') else null end as zdate,
'30' as num
from db_cfg_dev.cfg_period_d t1
group by
yearmonth
union all
select
case when count(t1.zdate) = '29' then concat(t1.yearmonth,'-29') else null end as zdate,
'31' as num
from db_cfg_dev.cfg_period_d t1
group by
yearmonth
union all
select
case when count(t1.zdate) = '29' then concat(t1.yearmonth,'-29') else null end as zdate,
'32' as num
from db_cfg_dev.cfg_period_d t1
group by
yearmonth
union all
select
case when count(t1.zdate) = '29' then concat(t1.yearmonth,'-29') else null end as zdate,
'33' as num
from db_cfg_dev.cfg_period_d t1
group by
yearmonth
union all
select
case when count(t1.zdate) = '30' then concat(t1.yearmonth,'-30') else null end as zdate,
'31' as num
from db_cfg_dev.cfg_period_d t1
group by
yearmonth
union all
select
case when count(t1.zdate) = '30' then concat(t1.yearmonth,'-30') else null end as zdate,
'32' as num
from db_cfg_dev.cfg_period_d t1
group by
yearmonth
union all
select
case when count(t1.zdate) = '30' then concat(t1.yearmonth,'-30') else null end as zdate,
'33' as num
from db_cfg_dev.cfg_period_d t1
group by
yearmonth
union all
select
case when count(t1.zdate) = '31' then concat(t1.yearmonth,'-31') else null end as zdate,
'32' as num
from db_cfg_dev.cfg_period_d t1
group by
yearmonth
union all
select
case when count(t1.zdate) = '31' then concat(t1.yearmonth,'-31') else null end as zdate,
'33' as num
from db_cfg_dev.cfg_period_d t1
group by
yearmonth
union all
select
t1.zdate,
substr(t1.zdate,9,2)
from db_cfg_dev.cfg_period_d t1) f
where f.zdate is not null;