我正在将表格的每一行与其他每一行进行比较,以找到最小距离。我还想添加两列,一列称为“ closest_distance”,另一列“ id_of_the_closest_distance”。 我知道我需要分两个步骤进行操作:一个-更新第一行,第二-根据第一行评估第二行。 我有一个查询,可以给我最接近的距离,如下所示:
SELECT DISTINCT ON (a.id)
a.id,
(SELECT
MIN(ST_HausdorffDistance(a.the_geom, b.the_geom))
FROM
gridareas AS b
WHERE
b.id != a.id
)
FROM gridareas AS a;
但是我不能使用简单的
UPDATE tablename SET colname = expression;
并将上面的查询用作表达式,因为
ERROR: subquery must return only one column
LINE 5: (SELECT DISTINCT ON (a.id)
另一方面,子查询不能同时返回a.id和MIN(ST_HausdorffDistance())。
我应该如何继续更新此列?
答案 0 :(得分:2)
这是您想要的吗?
update gridareas ga
set (closest_id, closest_dist) =
(select gs2.id, ST_HausdorffDistance(ga.the_geom, ga2.the_geom) as dist, ga2.id
from gridareas ga2
where ga2.id <> ga.id
order by ST_HausdorffDistance(ga.the_geom, ga2.the_geom)
limit 1
);
答案 1 :(得分:1)
不完全是,但是它很有帮助,谢谢! 这就是解决我的问题的方法:
UPDATE
gridareas AS a
SET
(hausdorffdistance_to_closest_geom) =
(SELECT
MIN(ST_HausdorffDistance(a.the_geom, b.the_geom))
FROM
gridareas AS b
WHERE
b.id != a.id
);