-
Notifications
You must be signed in to change notification settings - Fork 1
Add detailed query for discharged but incomplete IP encounters at SSMM #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sonzsara
wants to merge
2
commits into
main
Choose a base branch
from
ENG-150
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| ````markdown | ||
| # Discharged but Incomplete IP Encounters - SSMM | ||
|
|
||
| > IP encounters that were marked as discharged more than a day ago but are still not marked as completed | ||
| ## Purpose | ||
|
|
||
| Identifies in-patient (`encounter_class = 'imp'`) encounters at SSMM whose status history shows a `discharged` event more than 1 day ago, yet the encounter's current `status` is **not** `completed`. | ||
|
|
||
| ## Parameters | ||
|
|
||
| *This query has no parameters.* | ||
|
|
||
| --- | ||
|
|
||
| ## Query | ||
|
|
||
| ```sql | ||
| WITH first_discharged AS ( | ||
| 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 | ||
| 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 | ||
| AND discharged_status->>'status' = 'discharged' | ||
| AND e.status != 'completed' | ||
| ORDER BY e.id, (discharged_status->>'moved_at')::timestamp | ||
|
sonzsara marked this conversation as resolved.
Outdated
|
||
| ), | ||
| latest_bed AS ( | ||
| SELECT DISTINCT ON (fle.encounter_id) | ||
| fle.encounter_id, | ||
| fl.name AS bed_name, | ||
| fle.created_date AS bed_assigned_date | ||
| FROM emr_facilitylocationencounter fle | ||
| JOIN emr_facilitylocation fl ON fle.location_id = fl.id | ||
| WHERE fl.form = 'bd' | ||
| AND fl.root_location_id != 300 | ||
| ORDER BY fle.encounter_id, fle.created_date DESC | ||
| ) | ||
| SELECT DISTINCT ON (p.id) | ||
| p.name AS patient_name, | ||
| pi.value AS ssmm_id, | ||
| fd.discharged_datetime, | ||
| fd.current_status, | ||
| lb.bed_name AS latest_bed, | ||
| lb.bed_assigned_date | ||
| FROM first_discharged fd | ||
| JOIN emr_encounter e ON fd.encounter_id = e.id | ||
| JOIN emr_patient p ON e.patient_id = p.id | ||
| LEFT JOIN emr_patientidentifier pi | ||
| ON p.id = pi.patient_id | ||
| AND pi.config_id = 21 | ||
| LEFT JOIN latest_bed lb ON e.id = lb.encounter_id | ||
| WHERE fd.discharged_datetime < CURRENT_TIMESTAMP - INTERVAL '1 day' | ||
| AND lb.bed_name IS NOT NULL | ||
|
sonzsara marked this conversation as resolved.
Outdated
|
||
| ORDER BY p.id, fd.discharged_datetime DESC, p.name; | ||
|
sonzsara marked this conversation as resolved.
Outdated
|
||
| ``` | ||
|
|
||
|
|
||
| ## Notes | ||
|
|
||
| - **Hardcoded values:** | ||
| - `pi.config_id = 21` — SSMM patient identifier configuration. | ||
| - `fl.root_location_id != 300` — excludes fake beds. | ||
| - `fl.form = 'bd'` — only bed-type facility locations. | ||
| - This query has no Metabase parameters; the date threshold is fixed at "more than 1 day ago". | ||
|
|
||
| *Last updated: 2026-05-04* | ||
|
|
||
| ```` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.