-
Notifications
You must be signed in to change notification settings - Fork 1
Add IP/OP/Emergency Encounter Count documentation for SSMM #100
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
3
commits into
main
Choose a base branch
from
ENG-320
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,91 @@ | ||
|
|
||
| # IP / OP / Emergency Encounter Count - SSMM | ||
|
|
||
| > Daily counts of occupied inpatient beds, ambulatory (OP) encounters and emergency encounters for the current month-to-date (up to yesterday) | ||
|
|
||
| ## Purpose | ||
|
|
||
| Operational dashboard query giving a one-row-per-day view of patient volume at SSMM for the current month: | ||
|
|
||
| - **IP load** — number of beds occupied that day (one per patient, latest assignment). | ||
| - **OP load** — distinct ambulatory (`amb`) encounters created that day. | ||
| - **Emergency load** — distinct emergency (`emer`) encounters created that day. | ||
|
|
||
|
|
||
| ## Parameters | ||
|
|
||
| *No parameters — the window is always `month-to-date − 1 day`.* | ||
|
|
||
| --- | ||
|
|
||
| ## Query | ||
|
|
||
| ```sql | ||
| WITH dates AS ( | ||
| SELECT generate_series( | ||
| DATE_TRUNC('month', CURRENT_DATE - INTERVAL '1 day'), | ||
| CURRENT_DATE - INTERVAL '1 day', | ||
| '1 day'::interval | ||
| )::date AS report_date | ||
| ), | ||
|
|
||
| daily_occupied_beds AS ( | ||
| SELECT | ||
| d.report_date AS bed_date, | ||
| COUNT(DISTINCT bed_id) AS occupied_bed_count | ||
| FROM dates d | ||
| CROSS JOIN LATERAL ( | ||
| SELECT DISTINCT ON (e.patient_id) | ||
| fl.id AS bed_id | ||
| FROM emr_facilitylocationencounter fle | ||
| INNER JOIN emr_facilitylocation fl ON fle.location_id = fl.id | ||
| INNER JOIN emr_encounter e ON fle.encounter_id = e.id | ||
| WHERE fl.deleted = FALSE | ||
| AND fl.status = 'active' | ||
| AND fl.form = 'bd' | ||
| AND fl.root_location_id != 300 | ||
| AND fl.parent_id NOT IN (19, 44) | ||
| AND fle.deleted = FALSE | ||
| AND fle.start_datetime::date <= d.report_date | ||
| AND (fle.end_datetime IS NULL OR fle.end_datetime::date > d.report_date) | ||
| ORDER BY e.patient_id, fle.start_datetime DESC | ||
| ) occupied | ||
| GROUP BY d.report_date | ||
| ), | ||
|
|
||
| encounters AS ( | ||
| SELECT | ||
| created_date::date AS encounter_date, | ||
| COUNT(DISTINCT patient_id) FILTER (WHERE encounter_class = 'amb') AS ambulatory_count, | ||
| COUNT(DISTINCT patient_id) FILTER (WHERE encounter_class = 'emer') AS emergency_count | ||
| FROM emr_encounter | ||
| WHERE deleted = FALSE | ||
| AND created_date >= DATE_TRUNC('month', CURRENT_DATE - INTERVAL '1 day') | ||
| AND created_date::date <= CURRENT_DATE - INTERVAL '1 day' | ||
| GROUP BY created_date::date | ||
| ) | ||
|
|
||
| SELECT | ||
| d.report_date AS encounter_date, | ||
| TO_CHAR(d.report_date, 'DD, Dy') AS date_label, | ||
| COALESCE(dob.occupied_bed_count, 0) AS occupied_bed_count, | ||
| COALESCE(e.ambulatory_count, 0) AS ambulatory_count, | ||
| COALESCE(e.emergency_count, 0) AS emergency_count | ||
| FROM dates d | ||
| LEFT JOIN daily_occupied_beds dob ON d.report_date = dob.bed_date | ||
| LEFT JOIN encounters e ON d.report_date = e.encounter_date | ||
| ORDER BY d.report_date; | ||
| ``` | ||
|
|
||
| ## Notes | ||
|
|
||
| - **Date window** — `dates` CTE generates one row per day from the **start of the current month** through **yesterday** (`CURRENT_DATE - INTERVAL '1 day'`). Today is intentionally excluded so the report only shows completed days. | ||
| - **Hardcoded values:** | ||
| - `fl.root_location_id != 300` — excludes the fake beds root. Update if the fake-beds root ID changes. | ||
| - `fl.parent_id NOT IN (19, 44)` — excludes specific parent locations. Update if the excluded wards change. | ||
| - `fl.form = 'bd'` and `fl.status = 'active'` — only currently active bed-type locations count. | ||
|
|
||
| *Last updated: 2026-05-25* | ||
|
|
||
|
|
||
| ````` | ||
|
|
||
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.