简体   繁体   English

Redshift 中的 update + case + join 语句

[英]update + case + join statement in Redshift

I have this code from SQL Server that I have to implement in Redshift.我有这个来自 SQL 服务器的代码,我必须在 Redshift 中实现。

update t1 set some_flag = (case when b.some_value is null then false else true end)
from t1 a
left join t2 b on a.code = b.code;

I get an error in Redshift that says:我在 Redshift 中收到一条错误消息:

 Target table must be part of an equijoin predicate

How can I amend the above to make it work in Redshift?如何修改上述内容以使其在 Redshift 中工作? Thanks.谢谢。

Try removing the reference in the from clause:尝试删除from子句中的引用:

update t1
    set some_flag = (case when b.some_value is null then false else true end)
from t2 b 
where t1.code = b.code;

Postgres (and Postgres-derived databases) allow a FROM clause for UPDATE . Postgres(和 Postgres 派生的数据库)允许UPDATEFROM子句。 However, the table references are in addition to the referenced table in the UPDATE .但是,表引用是对UPDATE中引用的表的补充 This differs from SQL Server, where the table reference in the UPDATE is assumed to come from the FROM clause if possible -- even ignoring the alias if necessary.这与 SQL 服务器不同,后者假定UPDATE中的表引用来自FROM子句,如果可能的话——甚至在必要时忽略别名。

You should be able to simplify this to:您应该能够将其简化为:

update t1
    set some_flag = (b.some_value is not null);
from t2 b 
where t1.code = b.code;

You must fully qualify the table name with column instead of alias.您必须使用列而不是别名来完全限定表名。 Redshift does not support table alias in update command i guess.我猜 Redshift 不支持更新命令中的表别名。

update merge_demo1
set lastname = (case when merge_demo1.lastname = merge_demo1.lastname then merge_demo1.lastname else merge_demo2.lastname end)
from merge_demo2
where merge_demo1.id = merge_demo2.id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM