--首先在外部建一个统计年月字段的表,并把去年2020年放进去
create table tongjiyearmonth
( tjyearmonth varchar(10) );
delete from tongjiyearmonth where 1=1;
insert into tongjiyearmonth values('2020-01');
insert into tongjiyearmonth values('2020-02');
insert into tongjiyearmonth values('2020-03');
insert into tongjiyearmonth values('2020-04');
insert into tongjiyearmonth values('2020-05');
insert into tongjiyearmonth values('2020-06');
insert into tongjiyearmonth values('2020-07');
insert into tongjiyearmonth values('2020-08');
insert into tongjiyearmonth values('2020-09');
insert into tongjiyearmonth values('2020-10');
insert into tongjiyearmonth values('2020-11');
insert into tongjiyearmonth values('2020-12');
---然后这个写在和计算程序一起的定时脚本里,先把今年的年份取出来,然后把已经存在的统计年月和今年的统计年月放到tongjiyearmonth 中。
select distinct year (today) as myyear
from 数据库中某个表名 into temp z1;
select * from tongjiyearmonth
union
select myyear || '-01' from z1
union
select myyear || '-02' from z1
union
select myyear || '-03' from z1
union
select myyear || '-04' from z1
union
select myyear || '-05' from z1
union
select myyear || '-06' from z1
union
select myyear || '-07' from z1
union
select myyear || '-08' from z1
union
select myyear || '-09' from z1
union
select myyear || '-10' from z1
union
select myyear || '-11' from z1
union
select myyear || '-12' from z1
into temp z2;
select * from z2 order by tjyearmonth desc into temp z3;
delete from tongjiyearmonth where 1=1;
insert into tongjiyearmonth
select * from z3 ;
select * from tongjiyearmonth;
执行结果:
tjyearmonth
2021-12
2021-11
2021-10
2021-09
2021-08
2021-07
2021-06
2021-05
2021-04
2021-03
2021-02
2021-01
2020-10
2020-09
2020-08
2020-07
2020-06
2020-05
2020-04
2020-03
2020-02
2020-01