On Mon, Apr 20, 2009 at 7:39 PM, jc_mich <juan.michaca@xxxxxxxxxx> wrote: > > You've understood very well my problem, but also this query works as worse > than everything I did before, it throws as many rows as rows are contained > my tables clients and stores. I only want to find for every client what > store is closer to him, I expect one client to one store and their distance select clients.id as client_id, (select stores.id from stores order by (power(clients.x-stores.x)+power(clients.y-stores.y)) asc limit 1) as store_id from clients; Should do the trick, or at least something very similar. -- - David T. Wilson david.t.wilson@xxxxxxxxx -- Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general