ERROR: syntax error at or near "group"

ERROR: syntax error at or near "group"

您好,我正在编写一个 sql 查询,但是我在 GROUP BY 行中遇到语法错误。可能是什么问题,请帮助。

UPDATE intersection_points i
  SET nbr_victimes = sum(tue+bl+bg)
    FROM accident_ma a ,intersection_points i 
    WHERE (ST_DWithin(i.st_intersection,a.geom_acc, 10000) group by st_intersection)) ;

GROUP BY 是它自己的子句,它不是 WHERE 子句的一部分。

这是你拥有的:

WHERE (
    ST_DWithin(i.st_intersection,a.geom_acc, 10000)
    group by st_intersection
)

这是您需要的:

WHERE ST_DWithin(i.st_intersection,a.geom_acc, 10000)
group by st_intersection

编辑: 作为对评论的回应,听起来您的 JOINUPDATE ... FROM 语法需要的要复杂一些。看看“注释”部分 on this page:

When a FROM clause is present, what essentially happens is that the target table is joined to the tables mentioned in the from_list, and each output row of the join represents an update operation for the target table. When using FROM you should ensure that the join produces at most one output row for each row to be modified. In other words, a target row shouldn't join to more than one row from the other table(s). If it does, then only one of the join rows will be used to update the target row, but which one will be used is not readily predictable.

Because of this indeterminacy, referencing other tables only within sub-selects is safer, though often harder to read and slower than using a join.

通常 这将涉及将语法更改为如下内容:

UDPATE SomeTable
SET SomeColumn = 'Some Value'
WHERE AnotherColumn = 
  (SELECT AnotherColumn
   FROM AnotherTable
   -- etc.)

但是,在此查询中使用 ST_DWithin() 可能会使事情复杂化很多。如果对 table 结构、关系和此更新的总体意图没有更深入的了解,我可能无法提供更多帮助。从本质上讲,您需要为数据库明确说明需要更新哪些记录以及如何更新它们,这可能涉及以某种方式将查询更改为后一种子 select 语法。

我不明白你的数据结构。我根据您的查询创建了以下 table。请检查 table 结构。

如果table的结构是这样

您的查询必须是

UPDATE intersection_points SET nbr_victimes = (SELECT SUM(a.tue+a.bl+a.bg) FROM accident_ma a WHERE st_dwithin(st_intersection, a.geom_acc, 1000));