I am comparing every row of a table I have with every other row to find the minimum distance. I also want to add two columns one called "closest_distance", and the other "id_of_the_closest_distance". I know that I need to do this in two steps: one- update the first row, and two- evaluate the second row based on the first. I have a query that gives me the closest distance as below:
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;
However I cannot use a simple
UPDATE tablename SET colname = expression;
and use the query above as the expression, because
ERROR: subquery must return only one column
LINE 5: (SELECT DISTINCT ON (a.id)
On the other hand, the subquery cannot return both a.id and the MIN(ST_HausdorffDistance()).
How should I proceed to update this column?
id?