Received: from davinci.ethosmedia.com (davinci.ethosmedia.com [209.10.40.250]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f6S0JRf12161 for ; Fri, 27 Jul 2001 20:19:27 -0400 (EDT) (envelope-from josh@agliodbs.com) Received: from [209.10.40.250] (account ) by davinci.ethosmedia.com (CommuniGate Pro WebUser 3.4.1) with HTTP id 92889; Fri, 27 Jul 2001 17:19:22 -0700 From: "Josh Berkus" Subject: Re: Re: Restriction by grouping problem. To: Philip Warner Cc: pgsql-sql@postgresql.org X-Mailer: CommuniGate Pro Web Mailer v.3.4.1 Date: Fri, 27 Jul 2001 17:19:22 -0700 Message-ID: In-Reply-To: <3.0.5.32.20010728100308.02c98800@mail.rhyme.com.au> MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit X-Archive-Number: 200107/364 X-Sequence-Number: 4178 Phillip, > What it effectively does is executes the second SELECT for each row > of the > first select, a bit like a column-select expression, but it allows > access > to all rows. Ah! I knew that there had to be a simple way to do what you want to do in PGSQL: SELECT MIN(getfile) as bestfile FROM logs, (SELECT SID, MAX(logtime) as lasttime FROM logs) MDT WHERE logs.SID = MDT.SID AND logs.logtime = MDT.lasttime GROUP BY SID This gives you the "getfile" from the logs with the latest timestamp. In the event of two with the exact same logtime, it selects the first one alphabetically so that you don't get two files for one SID. -Josh ______AGLIO DATABASE SOLUTIONS___________________________ Josh Berkus Complete information technology josh@agliodbs.com and data management solutions (415) 565-7293 for law firms, small businesses fax 621-2533 and non-profit organizations. San Francisco