X-Original-To: pgsql-sql@postgresql.org Received: from wolff.to (wolff.to [66.93.249.74]) by postgresql.org (Postfix) with SMTP id 43048474E53 for ; Mon, 17 Mar 2003 12:39:06 -0500 (EST) Received: (qmail 21317 invoked by uid 500); 17 Mar 2003 17:53:33 -0000 Date: Mon, 17 Mar 2003 11:53:33 -0600 From: Bruno Wolff III To: Sjors Cc: pgsql-sql@postgresql.org Subject: Re: howto? Message-ID: <20030317175333.GA21282@wolff.to> Mail-Followup-To: Sjors , pgsql-sql@postgresql.org References: <008901c2eca7$fa181110$04a779d9@cc299057a> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <008901c2eca7$fa181110$04a779d9@cc299057a> User-Agent: Mutt/1.3.25i X-Archive-Number: 200303/401 X-Sequence-Number: 12440 On Mon, Mar 17, 2003 at 18:09:31 +0100, Sjors wrote: > Hi, > > Being not able to grasp in full sql, I've decided to call on the list. I've got a table with points, place_id and distance between points and place_id. For each point there can be more place_id's but I want to selesct the one with the minimum distance. Now I can group by the points and select the min(distance) but I can't do this if place_id is involved. I used a statment like this: > > Select points, place_id, distance from table > group by points, place_id, distance > having count(*)>1 and distance=(select min(distance) from table How about: select distinct on (points) points, place_id, distance from table order by points, distance;