0

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?

3
  • I think some example data would really help. What is id? Commented Oct 10, 2018 at 15:34
  • id is the primary key of the table! and the_geom is the geometry of the area (as this is a postGIS) but it's also irrelevant what the geom is, it could be replaces by any int and the ST_HausdorffDistance() with any other function e.g. LEAST Commented Oct 10, 2018 at 15:37
  • 2
    I really think that you should not persist that information in table columns. It is redundant and hence will "rot" as the table changes. You should use a view that calculates the information "on the fly". If the performance of such a view is too slow (which doesn't seem unlikely if the table is large), use a materialized view and refresh that regularly. Commented Oct 10, 2018 at 15:44

2 Answers 2

2

Is this what you want?

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
            );
Sign up to request clarification or add additional context in comments.

Comments

1

Not exactly, but it helped a lot, thanks! This is what fixed my problem:

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
  );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.