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
| FROM emr_encounter e | ||
| CROSS JOIN LATERAL jsonb_array_elements(e.status_history->'history') AS discharged_status | ||
| WHERE e.encounter_class = 'imp' | ||
| AND e.deleted = FALSE |
There was a problem hiding this comment.
adding a check to see if discharged exists is a good idea (filter down even further first)
AND e.status_history->'history' @> '[{"status": "discharged"}]'::jsonb
…include patient_id and adjust discharged_datetime calculation
https://data-public.ssmmhospital.com/dashboard/19-ip?date=past1days&date_=2026-04-28&tab=68-discharged-with-incomplete-enc