Question : select minimum value from several rows that is greater or equal to a given a single value

Hi,
I have two tables:
EarliestMedDate (patientID, Source, MedDate, MedPeriodNum)   where the period number corresponds to the MedDate
e.g.
 patientID, source,medDate, PeriodNum
1,C,2001-01-17,3
1,E,2002-12-10,6
2,C,2001-07-23,4
4,C,2002-12-17,6

ValidPeriods(patientID, PeriodNum)
1,7
1,8
1,9
2,7
2,8
2,10
4,2
4,5
4,6
4,7

and I need to get something like:
patienID,NewEarliestPeriod
1,C,7
1,E,7
2,C,7
4,C,6

where the newEarliestperiod is the minimum(periodNum) that is >=MedPeriodNum

Any suggestions? I'm stuck!
Thanks!

Answer : select minimum value from several rows that is greater or equal to a given a single value

what about this:
1:
2:
3:
4:
5:
6:
select emd.patientID, emd.Source, min(vp.PeriodNum) PeriodNum
  from EarliestMedDate emd
  join ValidPeriods vp
    on vp.PatientID = emd.PatientID
   and vp.PeriodNum >= MedPeriodNum
group by emd.patientID, emd.Source
Open in New Window Select All
Random Solutions  
 
programming4us programming4us