Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6931613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:36:01+00:00 2026-05-27T11:36:01+00:00

I am using postgis to calculate the distance between every ‘a’ and its corresponding

  • 0

I am using postgis to calculate the distance between every ‘a’ and its corresponding ‘b’ type point.

So far I have managed to do this with this query:

SELECT a.name, min(ST_Distance(a.the_geom, b.the_geom)) as distance
FROM a, b
GROUP BY a.name;

This works, it however only lists the name of the a type point and the distance to its closest b type point, it doesn’t list the name of the closest b type point to each a type point as I’d like it to do.

I would usually do this by joining both tables but they are unrelated and share no keys so I wouldn’t know how to join them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:36:02+00:00Added an answer on May 27, 2026 at 11:36 am

    The brute force approach (assuming that PostGIS supports WITH clauses, and assuming I have the syntax of WITH clauses correct – both unchecked assumptions):

    WITH (SELECT a.name AS a_name, b.name AS b_name, ST_Distance(a.the_geom, b.the_geom) as distance
            FROM a, b  -- Cartesian product (not efficient!)
         ) AS distances
    SELECT d1.a_name, d1.b_name, d1.distance
      FROM distances AS d1
      JOIN (SELECT a_name, MIN(distance) AS distance
              FROM distances
             GROUP BY a_name) AS d2
    

    This generates the list of names (a_name and b_name) plus the corresponding distance using a Cartesian product (which is what your original query does before the GROUP BY kicks in). It then selects the two names and the distance from the join of that table with the same table grouped over the a_name and joined on the a_name and the minimum distance.

    Clearly, if WITH is not supported, you can write the WITH clause out twice and hope your optimizer is good.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Rails 3.1, and I have tables with the PostGIS geometry datatype. These
EDIT: I have been using Postgres with PostGIS for a few months now, and
I'm using PostGIS with django. I know how to add a POINT on PostgreSQL
I have two tables representing two different types of imagery. I am using PostGIS
I'm developing a project using PostGIS to hold spatial data where I have records
I'm using PostgreSQL with PostGis and have shapes of all countries loaded. How can
I'm using a lat/long SRID in my PostGIS database (-4326). I would like to
Using online interfaces to a version control system is a nice way to have
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using SolrNet for querying & faceting. I have a combination of int, tdate and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.