Subject Nearest to a fixed value
Author Alejandro Garcia
Hi! I'm trying to solve this:
I have this table
PatientID DateIni Dateend Days
For each PatientID there are many Dateend but only one DateIni.
Days is the difference between Dateend and DateIni.
I need to find for each PatientID the Dateend which Days value is nearest to 365.

So for each PatientID I can have 3 cases:
1.For all the Dateend all the Days value are less than 365 return the greatest Dateend
2. There are Dateend which Days value is less than 365 and other which are greater than 365, return the greatest Dateend that's less than 365
3. All the Dateend have all the Days value greater than 365 return the less Dateend
I could GROUP BY Patientid and if for each PatientID there's at least one Dateend which Days <365 return MAX(Dateend) else return MIN (Dateend)
But I can't loop in each group and can't create that groups for looping..


LLama Gratis a cualquier PC del Mundo.
Llamadas a fijos y móviles desde 1 céntimo por minuto.

[Non-text portions of this message have been removed]