问题遇到的现象和发生背景
最后一次更新时间是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这样的