|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Nearest to a fixed value
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 don't know how to loop in each group and how to create the groups.. |
|
#2
|
|||
|
|||
|
Just to tell that it's done!!! This solves the problem:
Code:
select T1.PatientID, T1.DateIni, min(T1.Dateend) , min(T1.Days) from ThisTable T1 where not exists(select * from ThisTable T2 where T2.PatientID = T1.PatientID and T2.Days between T1.Days+1 and 365) group by 1, 2 |
|
#3
|
||||
|
||||
|
Quote:
Can you help me understand how? With a where clause that appears to exclude any patient who has any Days value between 1 and 365 how do you get the first and second group of results? Quote:
Also, I am curious about what gets returned with two min clauses. Do you get two records per patient? min(T1.Dateend) , min(T1.Days) min(T1.Days),min(T1.Dateend) or What? Thanks, Clive |
![]() |
| Viewing: Dev Shed Forums > Databases > Firebird SQL Development > Nearest to a fixed value |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|