0

我有两张与医院活动有关的表格;第一个列出了带有 PATIENT_ID 和 REFERRAL_DATE 的患者转诊,第二个显示了带有 PATIENT_ID、DISCHARGE_DATE 和 DISCHARGE_LOCATION 的出院。每位患者可多次转诊出院

我正在尝试运行一个查询,该查询将为我提供每次转诊以及随后出院的日期和出院地点。

我已经做到了这一点(但无法弄清楚如何使用聚合函数来为 MIN(DISCHARGE_DATE) 提取 DISCHARGE_LOCATION):

SELECT
Referral.PATIENT_ID,
Referral.REFERRAL_DATE,
MIN(Discharge.DISCHARGE_DATE) AS NEXT_DISCHARGE

FROM Referral
LEFT OUTER JOIN Discharge
ON Referral.PATIENT_ID = Discharge.PATIENT_ID
AND Discharge.DISCHARGE_DATE > Referral.REFERRAL_DATE

GROUP BY
Referral.PATIENT_ID,
Referral.REFERRAL_DATE

非常感谢任何帮助!

表格示例:

Referral:
PATIENT_ID  REFERRAL_DATE
Patient1    10/08/2012
Patient1    05/09/2012
. . .

Discharge:
PATIENT_ID  DISCHARGE_DATE  DISCHARGE_LOCATION
Patient1    01/08/2012      Hospital
Patient1    25/08/2012      Home
Patient1    15/09/2012      Care Home
. . .

我正在寻找的结果是:

PATIENT_ID  REFERRAL_DATE   NEXT_DISCHARGE  DISCHARGE_LOCATION
Patient1    10/08/2012      25/08/2012      Home
Patient1    05/09/2012      15/09/2012      Care Home
. . .
4

1 回答 1

1

您可以将结果加入出院表以获得所需的结果。

select 
     results.*,
     discharge.DISCHARGE_LOCATION

from 
(
    SELECT
        Referral.PATIENT_ID,
        Referral.REFERRAL_DATE,
        MIN(Discharge.DISCHARGE_DATE) AS NEXT_DISCHARGE    
    FROM Referral
        LEFT OUTER JOIN Discharge
        ON Referral.PATIENT_ID = Discharge.PATIENT_ID
        AND Discharge.DISCHARGE_DATE > Referral.REFERRAL_DATE    
    GROUP BY
        Referral.PATIENT_ID,
        Referral.REFERRAL_DATE
) results
     left join discharge 
          on results.NEXT_DISCHARGE = discharge.discharge_date
          and results.patient_id = discharge.patient_id
于 2013-08-01T08:34:58.053 回答