select t1.* from t20240306 t1
inner join(select id,max(h_update_dt) as h_update_dt from t20240306 where h_update_dt<=20230108 group by id) t2
on t1.id=t2.id and t1.h_update_dt=t2.h_update_dt
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/037e3999786c4565b0dbc0a1a9c8368c.png)
这种方法对开发人员来说是友好的,但是对分析人员来说,使用起来就比较麻烦点,需要两两关联才能得到想要结果。
**4.以方案3为数据存储,方案2为对外查询** 这种方案针对存储要求不是很苛刻的应用场景,可以兼顾方案2和方案3的优点.创建一个增加时间戳的表ods.t20240312\_ht,用于记录所有变更的数据,以这个为主表,每天全量更新增加生命周期的拉链表ods.t20240312\_hs
------------------创建表和插入测试数据
create table ods.t20240312(id int,name string,mark string,l_batch_date string);
create table ods.t20240312_ht(id int,name string,mark string,l_is_del string,l_batch_date string);
create table ods.t20240312_hs(id int,name string,mark string,l_is_del string,h_start_dt date,h_end_dt date);
insert into ods.t20240312 values(1,‘张三1’,‘test1’,‘20240