pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Selecting Most Recent Row
3+ messages / 2 participants
[nested] [flat]

* Selecting Most Recent Row
@ 2000-12-13 19:03 Steve Meynell <steve@candata.com>
  2000-12-13 21:38 ` Re: Selecting Most Recent Row Joel Burton <jburton@scw.org>
  0 siblings, 1 reply; 3+ messages in thread

From: Steve Meynell @ 2000-12-13 19:03 UTC (permalink / raw)
  To: 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.


--
Steve Meynell
Candata Systems

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Selecting Most Recent Row
  2000-12-13 19:03 Selecting Most Recent Row Steve Meynell <steve@candata.com>
@ 2000-12-13 21:38 ` Joel Burton <jburton@scw.org>
  2000-12-13 22:09   ` Re: Selecting Most Recent Row Steve Meynell <steve@candata.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Joel Burton @ 2000-12-13 21:38 UTC (permalink / raw)
  To: Steve Meynell <steve@candata.com>; +Cc: 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)



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Selecting Most Recent Row
  2000-12-13 19:03 Selecting Most Recent Row Steve Meynell <steve@candata.com>
  2000-12-13 21:38 ` Re: Selecting Most Recent Row Joel Burton <jburton@scw.org>
@ 2000-12-13 22:09   ` Steve Meynell <steve@candata.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Steve Meynell @ 2000-12-13 22:09 UTC (permalink / raw)
  To: Joel Burton <jburton@scw.org>; +Cc: pgsql-sql

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)




^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2000-12-13 22:09 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-12-13 19:03 Selecting Most Recent Row Steve Meynell <steve@candata.com>
2000-12-13 21:38 ` Joel Burton <jburton@scw.org>
2000-12-13 22:09   ` Steve Meynell <steve@candata.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox