Re: Selecting Most Recent Row

From: "Joel Burton" <jburton(at)scw(dot)org>
To: Steve Meynell <steve(at)candata(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Selecting Most Recent Row
Date: 2000-12-13 21:38:23
Message-ID: 3A37A5FF.26823.13E1C98@localhost
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> 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(at)scw(dot)org
Support Center of Washington (www.scw.org)

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Steve Meynell 2000-12-13 22:09:14 Re: Selecting Most Recent Row
Previous Message Peter Eisentraut 2000-12-13 20:25:28 Re: Decimal vs.Numeric vs. Int & type for OID