Skip to content
Advertisement

Select every second record then determine earliest date

I have table that looks like the following

enter image description here

I have to select every second record per PatientID that would give the following result (my last query returns this result)

enter image description here

I then have to select the record with the oldest date which would be the following (this is the end result I want)

enter image description here

What I have done so far: I have a CTE that gets all the data I need

I then write a query against the CTE but so far I can only return the second row for each patientID

How do I return the record with the oldest date only? Is there perhaps a MIN() function that I could be including somewhere?

Advertisement

Answer

For simplicity add order desc on date column and use TOP to get the first row only

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement