Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new query documentation page under Care/Encounter for identifying inpatient encounters that were discharged more than a day ago but still have a non-completed status. This fits the repository’s purpose of documenting operational SQL queries used for Care/Metabase reporting.
Changes:
- Adds a new markdown query doc for “Discharged but Incomplete IP Encounters - SSMM”.
- Introduces a SQL query that inspects encounter status history, patient identifier data, and latest bed assignment.
- Documents hardcoded assumptions for patient identifier config, fake-bed exclusion, and bed-form filtering.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| ), | ||
|
|
||
| latest_bed AS ( | ||
| SELECT DISTINCT ON (fle.encounter_id) |
There was a problem hiding this comment.
So here you are looking at all historical encounters, which is unnecessary given you are only interested in a small subset.
go for a left lateral join and join this query to first_discharged's id (encounter id).
| SELECT DISTINCT ON (e.id) | ||
| e.id AS encounter_id, | ||
| e.status AS current_status, | ||
| (discharged_status->>'moved_at')::timestamp + INTERVAL '5 hours 30 minutes' AS discharged_datetime |
There was a problem hiding this comment.
use at timezone
…include patient_id and adjust discharged_datetime calculation
| JOIN emr_patient p ON fd.patient_id = p.id | ||
| LEFT JOIN emr_patientidentifier pi ON p.id = pi.patient_id AND pi.config_id = 21 | ||
| JOIN latest_bed lb ON e.id = lb.encounter_id | ||
| JOIN LATERAL ( |
There was a problem hiding this comment.
Do ensure that this is an ask from product/do they need to know which bed patient is located in, or can they do with just patient info?
If they do need the latter, query below needs to be adjusted slightly since it's going off of latest bed assignment by creation date.
So would work for a case like this:
- Patient assigned to Bed A in General Ward
- Moved to ICU while holding Bed A
- Moved back to Bed A
In this case, latest bed by creation date is ICU Bed, rather than Bed A; you want to go fle active status (there would only be one active per encounter as of now).
https://data-public.ssmmhospital.com/dashboard/19-ip?date=past1days&date_=2026-04-28&tab=68-discharged-with-incomplete-enc