Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions Care/Encounter/bed_occupancy_ssmm.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,61 @@ Provides a real-time bed occupancy summary at SSMM, broken down by **floor** . F

```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 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
AND fl.parent_id NOT IN (19,44)

),

occupied_beds AS (

SELECT DISTINCT ON (e.patient_id)
fle.location_id AS bed_id
parent_loc.name AS level1_name,
COALESCE(grandparent_loc.name, parent_loc.name) AS floor,
fl.id AS bed_id
FROM emr_facilitylocationencounter fle
INNER JOIN emr_facilitylocation fl ON fle.location_id = fl.id
Comment thread
sonzsara marked this conversation as resolved.
Outdated
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
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
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 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
LEFT JOIN occupied_beds ob
ON ab.bed_id = ob.bed_id
AND ab.level1_name = ob.level1_name
AND ab.floor = ob.floor
GROUP BY ab.level1_name, ab.floor
)

SELECT * FROM (
SELECT * FROM bed_stats

UNION ALL

SELECT
'Total' AS level1_name,
'Total' AS floor,
Expand All @@ -73,6 +90,7 @@ ORDER BY CASE WHEN floor = 'Total' THEN 1 ELSE 0 END, floor, level1_name;
- **`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.parent_id NOT IN (19, 44)` — excludes specific parent locations (In this case OT and casualty). Update these IDs if the excluded wards change.
- `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`).
Expand Down