拉链表,希望能多一条更新数据

问题遇到的现象和发生背景

img


最后一次更新时间是20220101,但我希望多出来一条数据,即20220102那条数据
如图,希望得到表二的效果(新加的一条数据除了时间和期初金额外,其他字段均为0)
麻烦各位给个伪代码,让我知道怎么写就行了

问题相关代码

--建表语句

CREATE TABLE if not EXISTS t_ccc(
    company_key BIGINT ,
    customer_key STRING ,
    action_date STRING ,
    start_date STRING ,
    end_date STRING ,
    amount_initial DECIMAL COMMENT '期初金额',
    amount_last DECIMAL COMMENT '期末金额',
    amount_1 DECIMAL ,
    amount_2 DECIMAL ,
    amount_3 DECIMAL ,
    amount_4 DECIMAL 
);
--我用的方法
INSERT OVERWRITE TABLE t_ccc
SELECT 
        COALESCE(tblA.company_key, tblB.company_key) AS company_key
        , COALESCE(tblA.customer_key, tblB.customer_key) AS customer_key
        , COALESCE(tblA.action_date, tblB.action_date) AS action_date
        , COALESCE(tblA.action_date, '19700101') AS start_date
        , COALESCE(tblB.action_date, '99991231') AS end_date
        , IF(COALESCE(tblA.action_date, tblB.action_date) = '19700101'
            , COALESCE(tblB.amount_initial, tblA.amount_initial)
            , COALESCE(tblA.amount_initial, tblB.amount_initial)
            ) AS amount_initial
        , COALESCE(tblA.amount_last, tblB.amount_last) AS amount_last
        , COALESCE(tblA.amount_1, tblB.amount_1) AS amount_1
        , COALESCE(tblA.amount_2, tblB.amount_2) AS amount_2
        , COALESCE(tblA.amount_3, tblB.amount_3) AS amount_3
        , COALESCE(tblA.amount_4, tblB.amount_4) AS amount_4
    FROM t_tmp_zipper tblA
    LEFT JOIN t_tmp_zipper tblB
    ON tblA.customer_key = tblB.customer_key
        AND tblA.asc_rn + 1 = tblB.asc_rn
-- t_tmp_zipper是我拿数据的表,里面的asc_rn字段是顺序,1,2,3,4,5这样的
insert into 表二
select start_date ,end_date ,amount_initial,amount_last 
from 表一
union
select end_date , 99991231, tmp.amount_last, 0 
from 
(select * from 表一 order by end_date desc limit 1) tmp
select 开始时间,结束时间,期初金额,期末金额,数量 from 表一 where 条件 into temp temp_cursor

insert into temp_cursor select 结束时间,"99991231",期末金额,0,0 from temp_cursor
where 结束时间=(select max(结束时间) from temp_cursor)

select * from temp_cursor

insert into 表二
select start_date ,end_date ,amount_initial,amount_last from 表一
union
select end_date , 99991231, tmp.amount_last, 0 from (select * from 表一 order by end_date desc limit 1) tmp

insert into salary_tb
select 开始时间,
99991231 as 结束时间,
期末金额 as 期初金额
0 as 期末金额
from salary_tb
where tb.期初金额 = (select max(期初金额) from salary_tb)

可以采用union,这样可以去重。
union: 对两个结果集进行并集操作, 不包括重复行,相当于distinct;
union all: 对两个结果集进行并集操作,, 不管是不是重复;

insert into 表二
select start_date ,end_date ,amount_initial,amount_last 
from 表一
union 
select end_date , 99991231, tmp.amount_last, 0 from 
(select * from 表一 order by end_date desc limit 1) tmp