使用带有exists/not exists的case语句进行更新

ctzwtxfj  于 2021-07-29  发布在  Java
关注(0)|答案(2)|浏览(386)

我尝试使用以下逻辑更新表中的字段
对于符合条件的人口,请进行以下计算:•如果在partner\u priority中找到记录,其中mct.partner=part-ner\u priority.partner\u id,其中status='include',mct.status in(0,0s,0z)set el igible='y'•如果在partner\u priority中找不到记录,其中mct.partner=part-ner\u priority.partner\u id,其中status='include'和mct.status=0s set qualified='y'•否则set qualified='n'
到目前为止,我已经尝试了各种代码组合,但总是以错误告终,in/exists predicate 子查询只能用作过滤器。我使用的代码如下。我相信这只是一个简单的修复,但它让我发疯,任何帮助将不胜感激

--
    -- Update Eligible field
    --
    -- update mc
    update $env_tech_db.tbl_tech_mpxn_cohort  mc
    set eligible =  case when exists (select * from $env_tech_db.tbl_tech_mct mct
                               inner join $env_tech_db.tbl_tech_partner_priority pp
                               on mct.partner = pp.partner_id
                               where mc.mpxn = mct.mpxn and pp.status = 'Include' and mct.status     in ('0','0S','0Z')) then 'Y' 
                         when not exists(select * from $env_tech_db.tbl_tech_mct mct
                               inner join $env_tech_db.tbl_tech_partner_priority pp
                               on mct.partner = pp.partner_id
                               where mc.mpxn = mct.mpxn and pp.status = 'Include' and mct.status in ('0')) then 'Y'
                        else 'N'
                       end
     --from $env_tech_db.tbl_tech_mpxn_cohort mc
bvk5enib

bvk5enib1#

看看这个,看看有没有一个连接的更新。否则,可以使用merge语句,只包含update部分,而不包含insert部分。那可能更容易写。
可能是这样的:

update mc
set eligible = case when pp.partner_id is not null then 'Y' when pp2.partner_id is null then 'N' end
from

$env_tech_db.tbl_tech_mpxn_cohort  mc
left join $env_tech_db.tbl_tech_mct mct 
on mc.mpxn = mct.mpxn 
left join $env_tech_db.tbl_tech_partner_priority pp
on mct.partner = pp.partner_id

left join $env_tech_db.tbl_tech_mct mct2 
on mc.mpxn = mct2.mpxn
left join $env_tech_db.tbl_tech_partner_priority pp2
on mct2.partner = pp2.partner_id

where pp.status = 'Include' and mct.status     in ('0','0S','0Z')) 
and pp2.status = 'Include' and mct2.status in ('0')

您遇到的问题是,要更新的表也用于联接中(或者在这种情况下,在exists子查询的where子句中引用)

wfypjpf4

wfypjpf42#

我不认为databricks支持如此复杂的表达式。您可以使用两个更新:

update $env_tech_db.tbl_tech_mpxn_cohort mc
    set eligible = 'N';

update $env_tech_db.tbl_tech_mpxn_cohort  mc
    set eligible = 'Y'
    where exists (select 1
                  from $env_tech_db.tbl_tech_mct mct join
                       $env_tech_db.tbl_tech_partner_priority pp
                       on mct.partner = pp.partner_id
                  where mc.mpxn = mct.mpxn and pp.status = 'Include' and
                        mct.status in ('0','0S','0Z')
                 ) or
          not exists (select 1
                      from $env_tech_db.tbl_tech_mct mct join
                           $env_tech_db.tbl_tech_partner_priority pp
                           on mct.partner = pp.partner_id
                      where mc.mpxn = mct.mpxn and pp.status = 'Include' and 
                            mct.status in ('0')
                      ) ;

相关问题