1

我能想到的唯一通用例子是看医生的人一直试图降低病人的体温,所以我想看到最低的读数。

患者.患者_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,反之亦然?还是我完全错误地处理了这个问题?

任何新鲜的想法都非常感谢。

4

2 回答 2

1

我会完全以不同的方式处理这个问题。你有 3 个问题要解决。

  1. 过去 12 个月内每位医生的最高访问次数是多少
  2. 每次访问的最新测量值是多少
  3. 仅显示琼斯博士作为初级保健医生的结果

前两个可以用视图或 CTE 解决,然后加入反对。我建议使用单独的视图,这样您就可以发展您的应用程序逻辑(如果最近的定义发生了任何变化,则更新该视图)。这是一个 CTE 示例,但可以在一个地方查看所有内容:

旧: http ://sqlfiddle.com/#!3/3552e/8

新基于每位患者每位医生: http ://sqlfiddle.com/#!3/660be/3

with mostrecent_visits_per_patient_per_doctor as (
  select a.patientid,b.doctorid,max(b.visitdate) visitdate
    from patients a
    join visits b on a.patientid = b.patientid
   where b.visitdate >= dateadd(mm,-12,getdate())
   group by a.patientid,b.doctorid
), mostrecent_vitals_per_visit as (
  select a.visitid visitid,max(b.vitalsdate) topvitalsdate
    from visits a
    join vitals b on a.visitid = b.visitid
   group by a.visitid
)

select a.patientid
      ,b.visitid
      ,b.visitdate
      ,p_docs.docname primary_physician
      ,v_docs.docname attending_physician
      ,c.temp
      ,c.vitalsdate
  from patients a
  join visits b on a.patientid = b.patientid
  join vitals c on b.visitid = c.visitid
  join doctors p_docs on a.doctorid = p_docs.doctorid
  join doctors v_docs on b.doctorid = v_docs.doctorid
  join mostrecent_visits_per_patient_per_doctor x
        on a.patientid = x.patientid
       and b.doctorid = x.doctorid
       and b.visitdate = x.visitdate
  join mostrecent_vitals_per_visit z
        on c.visitid = z.visitid
       and c.vitalsdate = z.topvitalsdate
 --where p_docs.docname = 'Dr. Jones'
于 2013-07-17T18:24:10.977 回答
0

对于您想要的,row_number()似乎它应该是解决方案的一部分。这会枚举行(例如针对患者)并对它们进行排序(例如通过重要时间戳)并分配一个序列号。你想把它们倒序排列,然后选择第一个。

查询的结构如下所示:

select t.*
from (select *,
             ROW_NUMBER() over (partition by patient_id order by vitals.datetime desc) as seqnum
      from patients p join
           doctor d
           on p.doc_ID = d.doc_ID join
           visits vis
           on vis.patient_id = p.patient_id join
           vitals v
           on vit.visit_id = vis.visit_id
      where doctor.name = 'Dr. Jones'
     ) t
where seqnum = 1;

我觉得很难遵循你的逻辑。. . 这将使用生命体征时间戳进行排序。例如,您想要那个或访问时间戳吗?

于 2013-07-17T18:56:59.280 回答