我能想到的唯一通用例子是看医生的人一直试图降低病人的体温,所以我想看到最低的读数。
患者.患者_ID,患者.doc_ID
访问.visit_ID、访问.患者_ID、访问.doc_ID、访问.日期时间
vitals.vitals_ID、vitals.visit_ID、vitals.patient_ID、vitals.temp、vitals.datetime
医生.doc_ID,医生名
select patients.patient_ID, last_visit.temp, last_visit.maxdate
from patients
inner join doctor on patients.doc_ID = doctor.doc_ID
inner join (
select visits.patient_ID, last_vitals.temp, max(visits.datetime) as maxdate
from visits
inner join (
select vitals.visit_ID, vitals.temp, max(vitals.datetime) as maxvitals
from vitals
group by vitals.visit_ID, vitals.temp
) as last_vitals on visits.visit_ID = last_vitals.visit_ID
group by visits.patient_ID, visits.datetime
having visits.datetime >= DATEADD(mm, -12, GETDATE())
) as last_visit on patient.patient_ID = last_visit.patient_ID
where doctor.name = 'Dr. Jones'
我所做的任何尝试最终都会返回太多结果。我不知道我是否应该嵌套连接以获得最近访问的最新温度?如果是这样,我是先加入访问,然后再加入 Vitals,反之亦然?还是我完全错误地处理了这个问题?
任何新鲜的想法都非常感谢。