Vertica:在MERGE中,在满足条件时更改UPDATE

时间:2016-05-12 19:26:17

标签: sql merge insert vertica

我使用以下方法继续更新vertica数据库中的表:

  • 上传我要在登台表stg_table
  • 中更新的行列表
  • 在我的新表格中操作插入内容:

    MERGE INTO table imb USING stg_table stg ON stg.member_id = imb.member_id
    WHEN MATCHED THEN UPDATE
    SET     next_segment = stg.next_segment,
    untildate = stg.untildate
    
    WHEN NOT MATCHED THEN INSERT
    (member_id,
     previous_segment,
     current_segment,
     next_segment,
     untildate) 
    VALUES
    (stg.member_id,
     stg.previous_segment,
     stg.current_segment,
     stg.next_segment,
     stg.untildate)
    

现在我对另一个条件感兴趣:我希望只有在table和stg_table上满足某些条件时才会出现“UPDATE”。

基本上,我想要类似的东西:

    WHEN MATCHED THEN UPDATE
    SET     next_segment = stg.next_segment,
    untildate = stg.untildate
    WHERE stg.next_segment='x' AND  imb.current_segment='y'

==>这不起作用,因为我不能在Vertica中放置WHERE

我也尝试过:

    WHEN MATCHED THEN UPDATE
    SET     next_segment = IF(stg.next_segment='x' AND  imb.current_segment='x',stg.next_segment,imb.next_segment)
    untildate = IF(stg.next_segment='x' AND  imb.current_segment='x',stg.untildate,imb.untildate)

==>这不起作用,因为我似乎无法在这里使用IF

我还想把我的附加条件放在“ON”中,但我担心当member_id匹配而不是我的附加条件时它会经过“INSERT”。

如果有人知道如何做我想做的事,即

什么时候不匹配然后插入 当匹配然后更新“但有时如果在这些情况下满足某些额外条件则实际上不会更新请不要做任何事情”

什么有用

elirevach指出,CASE而不是IF将起作用:

    WHEN MATCHED THEN UPDATE
    SET     next_segment =  CASE stg.next_segment='x' AND  imb.current_segment='x'  THEN stg.next_segment ELSE imb.next_segment END,
    untildate = CASE stg.next_segment='x' AND  imb.current_segment='x' THEN stg.untildate ELSE imb.untildate END

另一种继续进行,但更重要的方法是简单地发出3个请求:

   MERGE INTO table imb USING stg_table stg ON stg.member_id = imb.member_id  AND  MyOtherCondition
   WHEN MATCHED THEN UPDATE ...

   MERGE INTO table imb USING stg_table stg ON stg.member_id = imb.member_id  AND  NOT MyOtherCondition
   WHEN MATCHED THEN UPDATE ...

插入:

   MERGE INTO table imb USING stg_table stg ON stg.member_id = imb.member_id  
   WHEN NOT MATCHED THEN INSERT ...

1 个答案:

答案 0 :(得分:1)

Somting like(永远不会在真实环境中测试),当匹配然后更新设置next_segment = tg.next_segment ='x'和imb.current_segment ='y'时的情况然后stg.next_segment else next_segment结束,untildate = case when tg.next_segment ='x'AND imb.current_segment ='y'then stg.untildate elae untildate -