Re: Selecting Most Recent Row - Mailing list pgsql-sql

From Joel Burton
Subject Re: Selecting Most Recent Row
Date
Msg-id 3A37A5FF.26823.13E1C98@localhost
Whole thread Raw
In response to Selecting Most Recent Row  (Steve Meynell <steve@candata.com>)
List 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@scw.org
Support Center of Washington (www.scw.org)


pgsql-sql by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: Decimal vs.Numeric vs. Int & type for OID
Next
From: Frank Joerdens
Date:
Subject: Re: How to represent a tree-structure in a relational database