Thread: Selecting Most Recent Row

Selecting Most Recent Row

From
Steve Meynell
Date:
Ok here is the problem. <br />Table: Widgets <br />Fields:  Unique_Key, DateStamp, Batch_Number, Instructions.
<p>BasicSelect Statement: <br />select Unique_Key from Widgets where Batch_Number>='inputedvalue' <p>Problem: <br
/>SomeBatch_Numbers might be duplicated over a period of time.  I want to select the most recent of these Batch
Numbers.<br />  <pre>-- 
 
Steve Meynell
Candata Systems</pre>  

Re: Selecting Most Recent Row

From
"Joel Burton"
Date:
> 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)


Re: Selecting Most Recent Row

From
Steve Meynell
Date:
Joel,   Thank you very much.  I gave that a try and it worked perfectly.  It
definately was the distinct keyword I was missing.

Thanks Again,
Steve


Joel Burton wrote:

>
> 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)