MSSQL查找去年最近一次访问的最近一次访问量

时间:2013-07-17 17:26:22

标签: sql sql-server join nested

我能想到的唯一一个通用例子就是看到医生不断尝试降低患者的体温,所以我希望看到最低的读数。

patients.patient_ID,patients.doc_ID

visits.visit_ID,visits.patient_ID,visits.doc_ID,visits.datetime

vitals.vitals_ID,vitals.visit_ID,vitals.patient_ID,vitals.temp,vitals.datetime

doctor.doc_ID,doctor.name

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'  

我的尝试最终会在我做出的任何尝试中返回太多结果。我不知道我是否应该嵌入连接以获得最近一次访问的最新温度?如果是这样,我首先加入访问,然后加入生命体征,反之亦然?或者我完全不正确地接近这个?

真正赞赏任何新鲜的想法。

2 个答案:

答案 0 :(得分:1)

我会完全不同地解决这个问题。你有3个问题要解决。

  1. 过去12个月每位医生的最高访问量是多少
  2. 每次访问的最新衡量标准
  3. 仅显示琼斯博士作为初级保健医生的结果
  4. 前两个可以通过视图或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'
    

答案 1 :(得分: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;

我发现很难遵循你的逻辑。 。 。这使用命令的生命周期时间戳。你想要那个或访问时间戳吗?