Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 2B00A475D12 for ; Thu, 12 Sep 2002 15:19:07 -0400 (EDT) Received: from mail.propertykey.com (propertykey.com [206.196.37.193]) by postgresql.org (Postfix) with ESMTP id 27633475BEC for ; Thu, 12 Sep 2002 15:19:06 -0400 (EDT) Received: from propertykey.com (gotojail.propertykey.com [206.196.37.197]) by mail.propertykey.com (8.11.6/8.9.3) with ESMTP id g8CJJ2M06219; Thu, 12 Sep 2002 14:19:04 -0500 Message-ID: <3D80E8A5.1080107@propertykey.com> Date: Thu, 12 Sep 2002 14:19:01 -0500 From: Jeff Hoffmann User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.1) Gecko/20020826 X-Accept-Language: en-us, en, ja, zh MIME-Version: 1.0 To: Tim Perdue , pgsql-general@postgresql.org Subject: Re: [SQL] Latitude / Longitude References: <20020912124426.A27570@mail.perdue.net> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200209/611 X-Sequence-Number: 30192 Tim Perdue wrote: > Hi Folks - > > I'm working on a global weather database for a client and have hit an issue > which I'm sure has been solved before. Unfortunately, the site search doesn't > pull up anything. > > Essentially, I've got two tables, one with city/county/lat/long and another > with lat/long/weather data. > > None of the lat/longs in the two tables match up directly, so I can't do a > simple join of the two tables. I need to join on closest proximity on the > lat/long fields. > > Any suggestions? It seems to me this will be pretty expensive on CPU resources > unless there's a really elegant trick uncovered. I guess that all depends on how you define expensive on CPU resources. I think the way I would do it is to define an sql function. For example, assume the following table structure: create table a (city text, city_location point, city_weather text); create table b (weather text, weather_location point); Create a function something like: create function closest_weather (point) returns text as 'select b.weather from b order by $1 <-> b.weather_location limit 1;' language sql; Then you could do something like: update a set city_weather = closest_weather(city_location); And if you had the lat/long as seperate numbers, just cast them as a point (i.e., point(long,lat) will return a point type). It's going to do a table scan for each city it updates, but that may or may not be a problem for you. -- Jeff Hoffmann PropertyKey.com