Skip to content

Add detailed query for discharged but incomplete IP encounters at SSMM#88

Open
sonzsara wants to merge 3 commits into
mainfrom
ENG-150
Open

Add detailed query for discharged but incomplete IP encounters at SSMM#88
sonzsara wants to merge 3 commits into
mainfrom
ENG-150

Conversation

@sonzsara
Copy link
Copy Markdown
Contributor

@sonzsara sonzsara commented May 4, 2026

@sonzsara sonzsara requested a review from Copilot May 5, 2026 07:33
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread Care/Encounter/discharged_but_incomplete_ssmm.md
Comment thread Care/Encounter/discharged_but_incomplete_ssmm.md Outdated
Comment thread Care/Encounter/discharged_but_incomplete_ssmm.md Outdated
Comment thread Care/Encounter/discharged_but_incomplete_ssmm.md Outdated
),

latest_bed AS (
SELECT DISTINCT ON (fle.encounter_id)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants