Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 7832E6347B7 for ; Thu, 8 Oct 2009 11:24:21 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 27027-04-4 for ; Thu, 8 Oct 2009 14:24:01 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from kuber.nabble.com (kuber.nabble.com [216.139.236.158]) by mail.postgresql.org (Postfix) with ESMTP id 0CA30634AEE for ; Thu, 8 Oct 2009 10:14:04 -0300 (ADT) Received: from isper.nabble.com ([192.168.236.156]) by kuber.nabble.com with esmtp (Exim 4.63) (envelope-from ) id 1Mvsok-00058i-1S for pgsql-sql@postgresql.org; Thu, 08 Oct 2009 06:14:02 -0700 Message-ID: <25802979.post@talk.nabble.com> Date: Thu, 8 Oct 2009 06:14:02 -0700 (PDT) From: sub3 To: pgsql-sql@postgresql.org Subject: Pulling additional columns with aggregate MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Nabble-From: steve@subwest.com X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=-10 required=5 tests=BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 200910/28 X-Sequence-Number: 33227 Hi, I have 2 tables. I want to be able to calculate the closest value in one (tempvalues), to the closest value in the other (points). This closest point, I want to save into the table with its difference. So if I have: create table points ( id integer, center double precision ); insert into points values (1, 1),(2,4),(3,7),(4,12); CREATE TABLE tempvalues ( id serial NOT NULL, "value" double precision, closest_point_id integer, distance_to_point double precision, CONSTRAINT tempvalues_pkey PRIMARY KEY (id), CONSTRAINT tempvalues_closest_point_id_fkey FOREIGN KEY (closest_point_id) REFERENCES points (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION ); insert into tempvalues (value) values (1.1),(2.2),(3.3),(4.4),(5.5),(6.6),(7.7),(8.8),(9.9), (10.1),(11.1),(12.2),(13.3),(14.4),(15.5),(16.6),(17.7),(18.8),(19.9),(20.0); I would like to see each row in tempvalues populated with the closest point from points and its difference. I know I can find the cartesian product of the 2 tables, and get the distance between all values. select tempvalues.id as tid, points.id as pid, min(abs(points.center-tempvalues.value)) from points, tempvalues group by tempvalues.id,points.id order by tid,pid But I can't figure out how to return the result w/only 1 row per tempvalue.id (the minimum) and still get the id column from each table. Any aggregate with force those columns out. I would love to do something like: update tempvalues set closest_point_id,distance_to_point from (above query) but haven't been able to figure this out. Any suggestions? Thanks. -- View this message in context: http://www.nabble.com/Pulling-additional-columns-with-aggregate-tp25802979p25802979.html Sent from the PostgreSQL - sql mailing list archive at Nabble.com.