Redshift 更新内连接

Posted

技术标签:

【中文标题】Redshift 更新内连接【英文标题】:Redshift update innerjoin 【发布时间】:2019-10-23 08:49:38 【问题描述】:

我想更新新状态

此选择有效:

select cd.referenceid,  cd.productstate , cd.date, dateadd(day,-1,cd.date) as gestern, cd2.date as cd2date, cd2.productstate, cd.statenew
from public.contractdaily  cd
left join public.contractdaily cd2 on cd2.contractid = cd.contractid and dateadd(day,-1,cd.date) = cd2.date
where cd.productstate != cd2.productstate and cd.referenceid = 201663

这不起作用

update contractdaily  set stateNew = 1
from
(select  cd.stateNew ,cd.referenceid,  cd.productstate as cd1productstate , cd.date, dateadd(day,-1,cd.date) as gestern, 
cd2.date as cd2date, cd2.productstate as cd2productstate, cd.referenceid as cdreferenceid
from contractdaily  cd
left join contractdaily cd2 on cd2.contractid = cd.contractid and dateadd(day,-1,cd.date) = cd2.date )foo
where contractdaily.productstate !=  foo.cd2productstate  and contractdaily.referenceid = 201663;

错误:亚马逊无效操作:目标表必须是一部分 等值连接谓词; [SQL 状态=XX000,数据库错误代码=500310]

【问题讨论】:

【参考方案1】:

也许这会有所帮助:

update contractdaily
set stateNew = 1
where referenceid = 201663
and productstate not in
(
select cd2.productstate 
from contractdaily cd
left join contractdaily cd2 on cd2.contractid = cd.contractid and dateadd(day,-1,cd.date) = cd2.date
);

【讨论】:

以上是关于Redshift 更新内连接的主要内容,如果未能解决你的问题,请参考以下文章

sql-内连接更新多行

使用内连接和聚合函数更新列

内连接嵌套在更新语句 SQL 中

在 Redshift 更新查询中使用左外连接导致错误:目标表必须是等连接谓词的一部分

使用大表连接更新 Amazon Redshift 中的列

SQL 内连接 2 个具有多列条件的表并更新