Skip to content

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

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

Add detailed query for discharged but incomplete IP encounters at SSMM#88
sonzsara wants to merge 5 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.

Comment thread Care/Encounter/discharged_but_incomplete_ssmm.md Outdated
…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 (
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.

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).

@sonzsara sonzsara requested a review from Jacobjeevan May 26, 2026 08:41
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