请求协助实现ins_dts与upd_dts字段的动态赋值逻辑
实现ins_dts和upd_dts的动态赋值逻辑
根据需求,我们可以通过CASE语句判断coalesce函数的实际取值来源,从而动态选择对应的插入和更新日期字段。修改后的SQL如下:
select a.schema, a.table, a.column, coalesce(a.col_cmmt_txt, a.col_dscr_txt, a.col_annt_txt, b.target_col_descr) as coldscr, -- 动态选择ins_dts case when a.col_cmmt_txt is not null or a.col_dscr_txt is not null or a.col_annt_txt is not null then a.dw_ins_dts else b.dw_ins_dts end as ins_dts, -- 动态选择upd_dts case when a.col_cmmt_txt is not null or a.col_dscr_txt is not null or a.col_annt_txt is not null then a.dw_upd_dts else b.dw_upd_dts end as upd_dts from test1 a left join test2 b on a.schema = upper(b.schema) and a.table = upper(b.table) and a.column = upper(b.target_col)
逻辑说明
- 当
a.col_cmmt_txt、a.col_dscr_txt、a.col_annt_txt三者中任意一个不为NULL时,coalesce会优先取test1表的字段值,此时对应的日期字段使用test1的dw_ins_dts和dw_upd_dts - 只有当前三个字段全为NULL时,
coalesce才会取b.target_col_descr,此时日期字段使用test2表的对应日期字段 - 由于是左连接,如果test2中没有匹配记录,
b.target_col_descr和对应的日期字段都会为NULL,符合业务逻辑
内容的提问来源于stack exchange,提问作者Ram
相关产品推荐
相关产品推荐

