X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id EF01CD1C9B7 for ; Wed, 30 Jul 2003 08:52:50 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 44537-03 for ; Wed, 30 Jul 2003 05:51:51 -0300 (ADT) Received: from stud.fbi.fh-darmstadt.de (stud.fbi.fh-darmstadt.de [141.100.40.65]) by svr1.postgresql.org (Postfix) with ESMTP id 6E522D1C4EC for ; Wed, 30 Jul 2003 05:52:49 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by stud.fbi.fh-darmstadt.de (Postfix) with ESMTP id 29A90CE6F7 for ; Wed, 30 Jul 2003 10:52:49 +0200 (CEST) Received: by stud.fbi.fh-darmstadt.de (Postfix, from userid 33) id A4DF2CE6F7; Wed, 30 Jul 2003 10:52:47 +0200 (CEST) Received: from troubadix.lhsystems.com (troubadix.lhsystems.com [193.24.32.38]) by stud.fbi.fh-darmstadt.de (IMP) with HTTP for ; Wed, 30 Jul 2003 10:52:47 +0200 Message-ID: <1059555167.3f27875f7e3da@stud.fbi.fh-darmstadt.de> Date: Wed, 30 Jul 2003 10:52:47 +0200 From: oheinz@stud.fbi.fh-darmstadt.de To: pgsql-sql@postgresql.org Subject: Re: Problem using Subselect results References: <1058964708.3f1e84e407a8d@stud.fbi.fh-darmstadt.de> <200307280947.21658.josh@agliodbs.com> <1059478450.3f265bb210e14@stud.fbi.fh-darmstadt.de> <22142.1059490856@sss.pgh.pa.us> <000401c355e4$11b0cdd0$0f02a8c0@spineofgod> In-Reply-To: <000401c355e4$11b0cdd0$0f02a8c0@spineofgod> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit User-Agent: Internet Messaging Program (IMP) 3.2.1 X-Originating-IP: 193.24.32.38 X-Scanned-By: AMaViS-ng at stud.fbi.fh-darmstadt.de. X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/445 X-Sequence-Number: 14559 I did try the following: SELECT DISTINCT ON (two.two_id) two.two_value FROM one,two WHERE (one.two_id=two.two_id AND one.updatenr > two.updatenr) ORDER BY two.updatenr ASC; I thought this would a) order the result list by updatenr b) return the first record only for records that are equal on the two.two_id field which would return exactly what I need - return the record with the But what I get instead is "ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions" *grrrmpfh* But after a while playing around I noticed that the "initial" in that sentence above is important - adding two.two_id as first argument to the ORDER BY does not change the result but fulfill the requirements - the expressions don't have to be excactly the same - just the initial argument. My view which returns the (hopefully) correct results: SELECT DISTINCT ON (two.two_id) two.two_value FROM one, two WHERE ((one.two_id = two.two_id) AND (one.updatenr > two.updatenr)) ORDER BY two.two_id, two.updatenr; Next step is to include corresponding information from table three - now I need subselects right? no way around with this DISTINCT and ORDER by stuff in it ... Many thanks for your help so far, Oliver Quoting Oliver Heinz : > I'll try this tomorrow - combining DISTINCT ON (two.two_id) and sorting by > two.updatenr could (should) have the desired effect - I never thought about > using ORDER and DISTINCT that way. > > I'll report my success or failure... > > Thanks so far! > > Bye, > Oliver ------------------------------------------------- This mail sent through IMP: http://horde.org/imp/