Received: from olympus.scw.org (209-9-156-98.sdsl.cais.net [209.9.156.98]) by mail.postgresql.org (8.11.1/8.11.1) with ESMTP id eBDLcLX45263 for ; Wed, 13 Dec 2000 16:38:21 -0500 (EST) (envelope-from jburton@scw.org) Received: from hades (eos.scw.org [209.9.156.112]) by olympus.scw.org (8.10.1/8.10.1) with ESMTP id eBDLce305742; Wed, 13 Dec 2000 16:38:40 -0500 From: "Joel Burton" Organization: Support Center of Washington To: Steve Meynell Date: Wed, 13 Dec 2000 16:38:23 -0500 MIME-Version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Subject: Re: Selecting Most Recent Row CC: pgsql-sql@postgresql.org Message-ID: <3A37A5FF.26823.13E1C98@localhost> In-reply-to: <3A37C7F2.A39482E4@candata.com> X-mailer: Pegasus Mail for Win32 (v3.12c) X-Archive-Number: 200012/112 X-Sequence-Number: 446 > Ok here is the problem. > Table: Widgets > Fields: Unique_Key, DateStamp, Batch_Number, Instructions. > > Basic Select Statement: > select Unique_Key from Widgets where Batch_Number>='inputedvalue' > > Problem: > Some Batch_Numbers might be duplicated over a period of time. I want > to select the most recent of these Batch Numbers. Will DateStamp being the date of insertion? If so, is it that you want the record for the most recent (largest) datestamp for each batch_number? something like SELECT DISTINCT ON (batch_number) unique_key, datestamp, batch_number, instructions FROM widgets ORDER BY batch_number, datestamp desc; (sort by batch then by date (last first) and show the first (aka 'distinct') row, considering only the batch_number for distinctness) HTH. -- Joel Burton, Director of Information Systems -*- jburton@scw.org Support Center of Washington (www.scw.org)