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

Help with query

Status
Not open for further replies.

mbaddar

Programmer
May 10, 2001
120
US
Hi,

I need to write two different queries. The first one will find the latest (most recent date) in one table for a particular patient. The same patients can have multiple entries in the table with different dates. I need to find the most recent date.

Then, I need to write another query to find the date that is the second most recent in the same table for the same patient. Does anyone know how I can do this?

Thanks for any help,
MB
 
Find the latest date.

Select PatientID, Max(date) As MostRecent
From tbl Group By PatientID

Find the date before that.

Select a.PatientID, Max(a.date) As 2ndMostRecent
From tbl As a
Where date<
(Select Max(date)
From tbl As b
Where b.PatientID=a.PatientID
Group By PatientID)
Group By a.PatientID

Terry

;-) USER, n.: The word computer professionals use when they mean &quot;idiot.&quot; -Dave Barry

SQL Article links:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top