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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find the last record in a sequence of entries

Status
Not open for further replies.

jjk238

Programmer
Jan 16, 2003
14
0
0
US
Hello,

I have a table that has up to 5 entries of the same Application number, with the Status changing on each one. What I'd like to do is get the highest number (last in the sequence) for each application number. Here's what the table looks like:

App Num Sequence Status EntryDate Product
1321 1 12 10/3/09 HELOC
1321 2 8 10/3/09 HELOC
1321 3 32 10/3/09 HELOC
1322 1 33 10/4/09 HEIL
1323 1 12 10/4/09 PLOC
1323 2 32 10/4/09 PLOC

Here's what I want to retrieve:

App Num Sequence Status EntryDate Product
1322 1 33 10/4/09 HEIL
1323 2 32 10/4/09 PLOC
1321 3 32 10/3/09 HELOC

Any idea how to do this?
 
Code:
SELECT t.AppNum     
     , t.Sequence    
     , t.Status    
     , t.EntryDate     
     , t.Product     
  FROM ( SELECT AppNum     
              , MAX(Sequence) AS last_seq
           FROM daTable
         GROUP
             BY AppNum ) AS m
INNER
  JOIN daTable AS t
    ON t.AppNum = m.AppNum
   AND t.Sequence = m.last_seq

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top