-
Notifications
You must be signed in to change notification settings - Fork 1
Add bed occupancy summary documentation #87
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-149
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 all commits
Commits
Show all changes
2 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,83 @@ | ||
|
|
||
| # Bed Occupancy - SSMM (Excluding Fake Beds) | ||
|
|
||
| > Total vs. occupied bed counts grouped by floor excluding beds under the fake beds root | ||
|
|
||
| ## Purpose | ||
|
|
||
| Provides a real-time bed occupancy summary at SSMM, broken down by **floor** . For each group it shows the total number of active beds and how many are currently occupied or were occupied. Beds under the fake beds root location (`root_location_id = 300`) are excluded. | ||
|
|
||
| ## Parameters | ||
|
|
||
| | Parameter | Type | Description | Example | | ||
| |-----------|------|-------------|---------| | ||
| | `report_date` | DATE | As-of date for occupancy. A bed is considered occupied if its assignment started on/before this date and either has no end date or ended after this date. Defaults to current state when omitted. | `'2026-04-30'` | | ||
|
|
||
| --- | ||
|
|
||
| ## Query | ||
|
|
||
| ```sql | ||
| WITH all_beds AS ( | ||
| SELECT | ||
| parent_loc.name AS level1_name, | ||
| COALESCE(grandparent_loc.name, parent_loc.name) AS floor, | ||
| fl.id AS bed_id | ||
| FROM emr_facilitylocation fl | ||
| LEFT JOIN emr_facilitylocation parent_loc ON fl.parent_id = parent_loc.id | ||
| LEFT JOIN emr_facilitylocation grandparent_loc ON parent_loc.parent_id = grandparent_loc.id | ||
| WHERE fl.deleted = FALSE | ||
| AND fl.status = 'active' | ||
| AND fl.form = 'bd' | ||
| AND fl.root_location_id != 300 | ||
| ), | ||
|
|
||
| occupied_beds AS ( | ||
| SELECT DISTINCT ON (e.patient_id) | ||
| fle.location_id AS bed_id | ||
| FROM emr_facilitylocationencounter fle | ||
|
sonzsara marked this conversation as resolved.
|
||
| INNER JOIN emr_encounter e ON fle.encounter_id = e.id | ||
| INNER JOIN all_beds ab ON fle.location_id = ab.bed_id | ||
| WHERE fle.deleted = FALSE | ||
| --AND DATE(fle.start_datetime) <= {{report_date}} | ||
| --AND (fle.end_datetime IS NULL OR DATE(fle.end_datetime) > {{report_date}}) | ||
| ORDER BY e.patient_id, fle.start_datetime DESC | ||
| ), | ||
|
|
||
| bed_stats AS ( | ||
| SELECT | ||
| ab.level1_name, | ||
| ab.floor, | ||
| COUNT(DISTINCT ab.bed_id) AS total_bed_count, | ||
| COUNT(DISTINCT ob.bed_id) AS occupied_bed_count | ||
| FROM all_beds ab | ||
| LEFT JOIN occupied_beds ob ON ab.bed_id = ob.bed_id | ||
| GROUP BY ab.level1_name, ab.floor | ||
| ) | ||
| SELECT * FROM ( | ||
| SELECT * FROM bed_stats | ||
| UNION ALL | ||
| SELECT | ||
| 'Total' AS level1_name, | ||
| 'Total' AS floor, | ||
| SUM(total_bed_count) AS total_bed_count, | ||
| SUM(occupied_bed_count) AS occupied_bed_count | ||
| FROM bed_stats | ||
| ) AS final_result | ||
| ORDER BY CASE WHEN floor = 'Total' THEN 1 ELSE 0 END, floor, level1_name; | ||
| ``` | ||
|
|
||
| ## Notes | ||
|
|
||
| - **`all_beds` CTE** — every active, real bed (`form = 'bd'`, not under the fake root, not deleted, status `active`). | ||
| - **`occupied_beds` CTE** — for each patient, the latest bed assignment. | ||
| - **Hardcoded values:** | ||
| - `fl.root_location_id != 300` — excludes the fake beds root. Update if the fake-beds root ID changes. | ||
| - `fl.form = 'bd'` — only bed-type facility locations. | ||
| - `fl.status = 'active'` — only currently active beds count toward totals. | ||
| - **`report_date` filter** — when provided, treats a bed as occupied if `start_datetime <= report_date` and (`end_datetime IS NULL` OR `end_datetime > report_date`). | ||
|
|
||
|
|
||
| *Last updated: 2026-05-04* | ||
|
|
||
| ```` | ||
|
sonzsara marked this conversation as resolved.
|
||
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.