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

Help Forming SQL Query

Status
Not open for further replies.

babrandt

Military
Joined
Aug 4, 2004
Messages
1
Location
US
Given that I have the generic fields Person, Number, and Date

I would like to create a SQL query that returns for each unique person, the number associated with the most recent date.

E.g.,
PERSON NUMBER DATE
1 3 1/1/2004
1 5 1/1/2003
1 7 6/1/2004
2 5 1/1/2000
1 10 1/1/2001
2 54 1/1/2002
2 32 1/1/1999

This query would return
PERSON NUMBER
1 7
2 54

Any help would be most appreciated! Thanks!
 
Try the following:

SELECT PERSON, NUMBER, MAX(DATE)
FROM TABLE
WHERE DATE <= SYSDATE
GROUP BY PERSON, NUMBER

If DATE is always less than the current date, delete that row.

Hope this helps!
 
Suppose your table is named SALES. Then the query is:

select s1.person, s1.number from sales s1 where s1.date = (select max(s2.date) from sales s2 where s2.person=s1.person) order by s1.person

Verified and it works :)

HTH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top