Sql 错误:目标表必须是equijoin谓词的一部分

Sql 错误:目标表必须是equijoin谓词的一部分,sql,amazon-redshift,Sql,Amazon Redshift,在此查询中: update public.dctable set dc = 1 from ( select d.*, min(time) over (partition by prod, customer, city, num) as mintime, row_number() over (partition by prod, customer, city, num order by time) as seqnum from publi

在此查询中:

update public.dctable
set dc = 1 
from (
        select d.*,
         min(time) over (partition by prod, customer, city, num) as mintime,
         row_number() over (partition by prod, customer, city, num order by 
time) as seqnum
  from public.dctable d
  ) dclk
  where dclk.seqnum <> 1
我得到一个错误:

错误:XX000:目标表必须是equijoin谓词的一部分

看不出我错过了什么。只需将dc=1设置为time>mintime和seqnum1

有什么想法吗


谢谢

您需要像这样将源表连接到from表

update public.dctable as s
set dc = 1 
from (
        select d.*,
         min(time) over (partition by prod, customer, city, num) as mintime,
         row_number() over (partition by prod, customer, city, num order by 
time) as seqnum
  from public.dctable d
  ) dclk
  where dclk.seqnum <> 1 AND s.id=dclk.id

我猜红移在更新时遵循Postgres语义。如果是:


我认为行号对于您想要执行的操作不是必需的。

您会遇到此错误,因为要更新的主表没有直接连接到查询的使用部分中的表。where子句必须将更新后的表作为谓词的一部分。e、 g.按原样进行查询:

update public.dctable set dc = 1 
  from (
    select by prod, customer, city, num, min(time) as time
    from public.dctable group by 1,2,3,4
  ) dclk
where dclk.prod = dctable.prod
and dclk.customer =dctable.customer
and dclk.city=dctable.city
and dclk.num=dctable.num
and dclk.time=dctable.time
update public.dctable set dc = 1 
  from (
    select by prod, customer, city, num, min(time) as time
    from public.dctable group by 1,2,3,4
  ) dclk
where dclk.prod = dctable.prod
and dclk.customer =dctable.customer
and dclk.city=dctable.city
and dclk.num=dctable.num
and dclk.time=dctable.time