使用 Snowflake 中的相关子查询进行以下更新的替代方案

问题描述 投票:0回答:1

在将 Oracle 代码转换为 Snowflake 的过程中,遇到了以下问题,Snowflake 方面的任何替代方法将不胜感激

update trgt_tbl T
set (
T.cur_indicator,
T.exp_dt,
T.updt_dt
) =     (
select  'N',
S.exp_dt- 1/86400, 
sysdate
from    src_tbl S
where   S.src_id= T.src_id
and S.ind_updt  = 'U' AND scd_row_num = 1
    )
where   (T.src_id)
in  (
select  distinct X.stc_id
from    src_tbl X
where   X.ind_updt  = 'U' AND scd_row_num = 1
)
and T.cur_indicator = 'Y'

我尝试了以下两种方法,但没有得到预期的结果。

第一种方法:

update trgt_tbl T
set
cur_indicator = 'N',
exp_dt = DATEADD('SECOND', -1,S.exp_dt),
updt_dt  = CURRENT_TIMESTAMP()
     
FROM src_tbl S
WHERE S.src_id= T.src_id
AND S.ind_updt  = 'U'
AND scd_row_num = 1
AND T.cur_indicator = 'Y';

第二种方法:

update  trgt_tbl T
set T.cur_indicator  =  s.ind,
T.exp_dt = s.dt,
T.updt_dt  = s.ts
from (select    
'N' ind,
DATEADD('SECOND', -1,exp_dt ) dt, 
CURRENT_TIMESTAMP() ts,
src_id ,
ind_updt     ,
scd_row_num  
from     src_tbl ) S
where   S.src_id = T.src_id
and S.ind_updt   = 'U' 
and scd_row_num  = 1
and     (T.src_id )
in  (
select  distinct X.src_id 
from    src_tbl X
where   X.ind_updt   = 'U' AND scd_row_num = 1
)
and T.cur_indicator = 'Y';
snowflake-cloud-data-platform correlated-subquery
1个回答
0
投票

尝试这个查询,我根据我的理解重构了它。
如果仍然无法正常工作,请添加示例输入和预期输出

update trgt_tbl T
set 
T.cur_indicator='N',
T.exp_dt=DATEADD('SECOND', -1,S.exp_dt),
T.updt_dt = getdate()
from    src_tbl S
where   S.src_id= T.src_id
and S.ind_updt  = 'U' AND scd_row_num = 1

and   (T.src_id)
in  (
select  distinct X.src_id
from    src_tbl X
where   X.ind_updt  = 'U' AND scd_row_num = 1
)
and T.cur_indicator = 'Y';
© www.soinside.com 2019 - 2024. All rights reserved.