X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id C7CADDBB3D for ; Fri, 18 Nov 2005 22:50:57 -0400 (AST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 25506-05 for ; Sat, 19 Nov 2005 02:51:00 +0000 (GMT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from mta207-rme.xtra.co.nz (mta207-rme.xtra.co.nz [210.86.15.118]) by svr1.postgresql.org (Postfix) with ESMTP id 83C06DBB42 for ; Fri, 18 Nov 2005 22:50:53 -0400 (AST) Received: from web3-rme.xtra.co.nz ([210.86.15.141]) by mta207-rme.xtra.co.nz with ESMTP id <20051119025058.OCQA16538.mta207-rme.xtra.co.nz@web3-rme.xtra.co.nz>; Sat, 19 Nov 2005 15:50:58 +1300 Received: from creeping ([210.86.84.40]) by web3-rme.xtra.co.nz with ESMTP id <20051119025058.LHSU22512.web3-rme.xtra.co.nz@creeping>; Sat, 19 Nov 2005 15:50:58 +1300 From: "Jeremy Palmer" To: Cc: "'Michael Glaesemann'" Subject: Re: DISTINCT ON Date: Sat, 19 Nov 2005 15:50:54 +1300 MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook, Build 11.0.6353 In-Reply-To: Thread-Index: AcXsl7kfY1LMEAraSzeNPEp7UWjbRQAG6a0g X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 Message-Id: <20051119025058.LHSU22512.web3-rme.xtra.co.nz@creeping> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=1.166 required=5 tests=[SUBJ_ALL_CAPS=1.166] X-Spam-Score: 1.166 X-Spam-Level: * X-Archive-Number: 200511/155 X-Sequence-Number: 23406 Thanks for the reply. Unfortunately that does not work as "id" column needs to be included in the group by statement or be used in an aggregate function. If I did this it definitely would note return the correct answer, as the "id" column is the primary key for the table. Any further suggestions? -----Original Message----- From: Michael Glaesemann [mailto:grzm@myrealbox.com] Sent: Saturday, 19 November 2005 12:28 p.m. To: Jeremy Palmer Cc: pgsql-sql@postgresql.org Subject: Re: [SQL] DISTINCT ON On Nov 19, 2005, at 7:49 , Jeremy Palmer wrote: > SELECT DISTINCT ON (vector_id, obs_type) > id > FROM observation > ORDER BY vector_id, > obs_type, > date DESC; > > However the documentation also states that "DISTINCT ON" is not > part of the > SQL standard and should be avoided when possible, stating that > aggregations > and sub-queries should be used instead... > > How would this be done in this scenario? Something like: select max(date), id from observation group by vector_id, obs_type; Do test to see which is better in your situation. Michael Glaesemann grzm myrealbox com