为个人提取不同的价值观

时间:2014-01-16 18:03:35

标签: sql sql-server-2005 group-by distinct

我试图从proc_chron的最近一个日期读出所有患者的结果,并且患者在他们的case_status下是一个(活动的),排除所有四个类别的患者是空的。 SQL server 2005.

select 

pct.patient_id,
pct.clinic_id,
pct.axis_I_II_1,
pct.axis_I_II_2,
pct.axis_I_II_3,
pct.axis_III_1,
pct.proc_chron

from patient_clin_tran pct
join patient p
on p.patient_id = pct.patient_id
group by pct.patient_id, pct.clinic_id, pct.axis_I_II_1,pct.axis_I_II_2, pct.axis_I_II_3, pct.axis_III_1, p.case_status, pct.proc_chron
having p.case_status = 'a' and pct.proc_chron = (select max(pct.proc_chron))
order by pct.patient_id

enter image description here

1 个答案:

答案 0 :(得分:2)

select     
    pct.patient_id,
    pct.clinic_id,
    pct.axis_I_II_1,
    pct.axis_I_II_2,
    pct.axis_I_II_3,
    pct.axis_III_1,
    pct.proc_chron    
from patient_clin_tran pct
    join patient p
    on p.patient_id = pct.patient_id
where p.case_status = 'a' 
    and pct.proc_chron = (select max(proc_chron) from patient_clin_tran pct2 where pct2.patientid = p.patientid)
and (pct.axis_I_II_1 is not null or pct.axis_I_II_2 is not null or pct.axis_I_II_3 is not null or pct.axis_I_II_4 is not null)
order by pct.patient_id