在数仓中,有两张表,t1和t2,字段相同,user_id可能重复,如果t2表中没有,但是t1表中有,说明这条数据是新增数据,例如t1中user_id=4这一条数据。如果t2表中有,但是t1表中没做,则说明这条数据被删除,如t2中user_id=5。如果user_id在t1和t2表中都存在,但是login_datetime不同,如t2中的第三条数据,则说明这条数据发生变更。现在需要将被删除的数据,新增的数据,变更的数据写入第三张t3表中,t3表的字段为user_id,login_datetime,change_type。change_type为删除,新增,变更其中之一。建表和数据字段如下:
create table t1(
user_id int,
login_datetime string)
insert into table t1 values(1,'2021-10-1');
insert into table t1 values(1,'2021-10-2');
insert into table t1 values(1,'2021-10-4');
insert into table t1 values(2,'2021-10-12');
insert into table t1 values(3,'2021-11-8');
insert into table t1 values(4,'2021-9-30');--新增
create table t2(
user_id int,
login_datetime string)
insert into table t2 values(1,'2021-10-1');
insert into table t2 values(1,'2021-10-2');
insert into table t2 values(1,'2021-10-3');--更改
insert into table t2 values(2,'2021-10-12');
insert into table t2 values(3,'2021-11-8');
insert into table t2 values(5,'2021-8-3');--删除