Find the farthest point

If it is just 2000 records and only points (no complex polygons or linestrings) it shouldn't take many milliseconds to calculate.

If it would have been a few million points to check distance between you could consider using knn-distance with PostgreSQL 9.1 and PostGIS 2.0.

But I think you should do fine with something like?

SELECT b."OutletName" FROM the_table AS a, the_table AS b 
WHERE a."OutletName" = 'A' AND b."OutletName" in ('B', 'F', 'D') 
ORDER BY ST_Distance(a.geom, b.geom) desc
limit 1;

The bottleneck in this query will not be the spatial calculation since it is only three distance calculations between points, but to find the OutletName fast. Because of that you will maybe save a efw milliseconds if you put an index n the OutletName column.

edit:
From Whubers comment I guess I am misunderstanding something here, but I let it be for now.

HTH

Nicklas


For evaluating this problem with many more points, use a KNN-distance method, with a CROSS JOIN LATERAL to gather each point's closest point. As always, make sure you have a spatial index on the geometry column, and it's recommended to use a projected spatial reference system (not lat/long).

This is a query that returns the top 10 farthest points in my_points with 650,000 rows, taking only one minute to complete:

SELECT a.gid, b.gid, a.geom <-> b.geom AS distance
FROM my_points a
CROSS JOIN LATERAL (
  SELECT b.gid, b.geom
  FROM my_points b
  WHERE a.gid <> b.gid
  ORDER BY a.geom <-> b.geom ASC LIMIT 1
) AS b
ORDER BY a.geom <-> b.geom DESC LIMIT 10;