You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求协助实现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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 11:50:51