Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting the first occurence of a rowset

Status
Not open for further replies.

dmlatham

Programmer
Oct 12, 2001
28
0
0
US
I require assistance with the following table:

sample cycle idtype sample_date res1 res2
------- --- ------ ----------- --- ----
6861 1 M 05/17/1999 D <NULL>
6080 1 M 11/17/2000 A P
6080 1 M 05/19/1999 A J
6140 1 C 01/06/2000 D P
6140 1 M 06/23/1999 D J
6806 1 C 08/08/2001 A P

I need to select the first occurence of each sample from the above table. My results should be:

sample cycle idtype sample_date res1 res2
------- --- ------ ----------- --- ----
6861 1 M 05/17/1999 D <NULL>
6080 1 M 11/17/2000 A P
6140 1 C 01/06/2000 D P
6806 1 C 08/08/2001 A P

ALL HELP is greatly appreciated.

Deborah
 
Select *
From Sample_Table ST
WHere Sample_Date.ST = (select max(sample_date)
FROM Sample_table
Where sample = sample.ST)
 
Thanks for help but I also need to take the idtype field in consideration along with the date. In my query that creates this table I am ordering by

sample, idtype, sampledate desc

The sample date is only part of the ordering, I need to include the idtype also. Any clues?
 
Actually, I just found that if I perform the same query that you gave me but used min(idtype) and captured that into a temp table, I can then perform your query and get the correct data. Thanks stsuing. Your help is appreciated.
 
THis example :
Code:
sample   cycle  idtype     sample_date   res1    res2
-------  ---    ------     -----------   ---     
6861      1        M        05/17/1999      D   
6080      1        M        11/17/2000      A         P
6140      1        C        01/06/2000      D         P
6806      1        C        08/08/2001      A
doesn't follow your order scheme of
sample, idtype, sampledate desc
and should be
Code:
order by idtype desc, sampledate asc
or 
order by idtype desc, sample desc
either way the above query from dmlatham will give you the results you must decide how best to order them
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top