I'm writing code to select patients that die within 30 days of a hospital discharge, my issue is that when I have a patient with multiple discharges within that 30 day tolerance it pulls back multiple rows! I've tried to solve this using max discharge date, which worked, yet when I add extra columns it seems to pull certain elements from other rows. Here is my code:
SELECT MAX(IPS.disch_dttm) [Discharge Datetime]
,MAX(IPS.IP_SPELL_ID) [Spell ID]
,pat.PAS_ID [K Number]
,MAX(IPS.DIS_WARD_ID) [Ward ID]
,DSSU.SU_DESCRIPTION [Discharging Ward]
FROM Pat_spell AS IPS
LEFT JOIN PATIENT PAT WITH (NOLOCK) ON PAT.DIM_PATIENT_ID = IPS.DIM_PATIENT_ID
LEFT JOIN SPECIALTY SPEC WITH (NOLOCK) ON SPEC.DIM_SPECIALTY_ID = IPS.DIM_DIS_SPECT_ID
LEFT JOIN SERVICE_UNIT DSSU WITH (NOLOCK) ON IPS.DIM_DIS_WARD_ID = DSSU.DIM_SSU_ID
WHERE (IPS.DISCH_DTTM <= PAT.DEATH_DTTM + 30)
AND IPS.DIM_DIS_SPECT_ID = '7195'
AND IPS.DISCH_DTTM BETWEEN '01/01/2014' AND '30/06/2014'
GROUP BY pat.PAS_ID
,pat.DEATH_DTTM
,IPS.DIM_PATIENT_ID
,DSSU.SSU_DESCRIPTION
ORDER BY pat.PAS_ID
Here is output from the above code for a single row that I've been using to debug:
Disch Date Event_ID Unique ID Ward ID Discharging Ward
2014-06-14 8366113 A123456 77085 WardA
The above gets the ward ID correct, but the "Discharging Ward" is wrong. Also the Event_ID corresponds with a previous attendance. What I'm trying to achieve is to pull only the most recent event within 30 days of a time of death, with 'Event ID' as my unique ID. This is what the output would look like if I wanted multiple rows:
Disch Date Event ID Unique ID Ward ID Discharging Ward
1 2014-06-14 8208846 A123456 77085 Ward B
2 2014-05-16 8366113 A123456 77036 Ward A
This is what my output should look like:
Disch Date Event_ID Unique ID Ward ID Discharging Ward
2014-06-14 8208846 A123456 77085 Ward B
So to sum up, my code pulls the correct "discharge date", the correct "Ward ID" but seems to pull the rest from other rows in the table. Apologies for the huge ask - any help would be appreciated, or if this has been explored to death, please point me in the right direction.
A simplified version of what you need looks like this...
SELECT [DETAIL INFO, no need to MAX or GROUP BY]
FROM Pat_spell AS IPS
LEFT JOIN PATIENT PAT WITH (NOLOCK) ON PAT.DIM_PATIENT_ID = IPS.DIM_PATIENT_ID
LEFT JOIN SPECIALTY SPEC WITH (NOLOCK) ON SPEC.DIM_SPECIALTY_ID = IPS.DIM_DIS_SPECT_ID
LEFT JOIN SERVICE_UNIT DSSU WITH (NOLOCK) ON IPS.DIM_DIS_WARD_ID = DSSU.DIM_SSU_ID
INNER JOIN (
SELECT PatientID, MAX(IPS.disch_dttm) AS DischargeDt
FROM [AllMyTables]
WHERE (IPS.DISCH_DTTM <= PAT.DEATH_DTTM + 30)
AND IPS.DIM_DIS_SPECT_ID = '7195'
AND IPS.DISCH_DTTM BETWEEN '01/01/2014' AND '30/06/2014'
) t1 ON PAT.PatientID = t1.PatientID AND IPS.disch_dttm = t1.DischargeDt
ORDER BY pat.PAS_ID
Since the INNER SQL is returning 1 row per patient, there is no need to group on the OUTER SQL.
If I had more data to work with, I might be able to stitch together the full SQL, but maybe this points you in the right direction.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments