Skip to content

Commit d80f29f

Browse files
committed
Add documentation for Community Nurse Visits by Facility query
1 parent da9f150 commit d80f29f

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
# Community Nurse Visits by Facility
3+
4+
> Count of community nurse visits grouped by facility for patients belonging to a given organization
5+
6+
## Purpose
7+
8+
Counts the number of completed community-nurse visits per facility, scoped to living patients within a specified organization.
9+
10+
## Parameters
11+
12+
| Parameter | Type | Description | Example |
13+
|-----------|------|-------------|---------|
14+
| `organization_id` | TEXT | Organization external ID to scope patients (matched against `emr_organization.external_id`) | `'org-uuid-1234'` |
15+
| `date` | DATE / range | Metabase date filter (typically bound to `emr_questionnaireresponse.created_date`) | `'2026-07-01'` |
16+
17+
---
18+
19+
## Query
20+
21+
```sql
22+
WITH org_id AS (
23+
SELECT emr_organization.id
24+
FROM emr_organization
25+
WHERE emr_organization.deleted = FALSE
26+
-- AND emr_organization.external_id::text = {{organization_id}}
27+
),
28+
filtered_patients AS (
29+
SELECT emr_patient.id
30+
FROM emr_patient
31+
INNER JOIN org_id ON TRUE
32+
WHERE emr_patient.deceased_datetime IS NULL
33+
AND emr_patient.organization_cache && ARRAY[org_id.id]::integer[]
34+
),
35+
community_nurse_visits AS (
36+
SELECT DISTINCT
37+
emr_questionnaireresponse.id AS questionnaireresponse_id,
38+
emr_questionnaireresponse.encounter_id,
39+
DATE(emr_questionnaireresponse.created_date) AS visit_date
40+
FROM emr_questionnaireresponse
41+
INNER JOIN filtered_patients
42+
ON filtered_patients.id = emr_questionnaireresponse.patient_id
43+
CROSS JOIN LATERAL jsonb_array_elements(emr_questionnaireresponse.responses) AS response_element
44+
CROSS JOIN LATERAL jsonb_array_elements(response_element -> 'values') AS answer_element
45+
WHERE emr_questionnaireresponse.questionnaire_id IN (3, 67)
46+
AND emr_questionnaireresponse.status = 'completed'
47+
AND emr_questionnaireresponse.encounter_id IS NOT NULL
48+
AND response_element ->> 'question_id' IN (
49+
'd602b9b2-d4cd-43d7-99d3-3d0169095eba',
50+
'edf6799d-88c4-4f9d-8ced-552dde6ad6af'
51+
)
52+
AND answer_element ->> 'value' = 'Community Nurse'
53+
--[[AND {{date}}]]
54+
),
55+
visit_with_facility AS (
56+
SELECT
57+
community_nurse_visits.visit_date,
58+
facility_facility.id AS facility_id,
59+
facility_facility.name AS facility_name
60+
FROM community_nurse_visits
61+
INNER JOIN emr_encounter
62+
ON emr_encounter.id = community_nurse_visits.encounter_id
63+
INNER JOIN facility_facility
64+
ON facility_facility.id = emr_encounter.facility_id
65+
AND facility_facility.deleted = FALSE
66+
)
67+
SELECT
68+
visit_with_facility.facility_name,
69+
COUNT(*) AS community_nurse_visit_count
70+
FROM visit_with_facility
71+
GROUP BY
72+
visit_with_facility.facility_id,
73+
visit_with_facility.facility_name
74+
ORDER BY
75+
community_nurse_visit_count DESC,
76+
visit_with_facility.facility_name;
77+
```
78+
79+
## Notes
80+
81+
- **Hardcoded IDs:**
82+
- `questionnaire_id IN (3, 67)` — community nurse questionnaires
83+
Update if questionnaires or questions change.
84+
- **`organization_id` is required** (no `[[...]]` wrapper) — the query will not run without a value.
85+
- Results are ordered by visit count descending, then alphabetically by facility name for ties.
86+
87+
*Last updated: 2026-07-15*

0 commit comments

Comments
 (0)