Skip to content

Commit accfb4c

Browse files
committed
Add documentation for Doctor with Community Nurse Visit Count by Facility query
1 parent b8bf63b commit accfb4c

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
# Doctor with Community Nurse Visit Count by Facility
3+
4+
> Count of completed doctor visits accompanied by a community nurse, grouped by facility for patients belonging to a given organization
5+
6+
## Purpose
7+
8+
Counts completed responses to the doctor visit questionnaire (`questionnaire_id = 68`) where the visit type is `'Doctor with Community Nurse'`, 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+
doctor_visits AS (
36+
SELECT DISTINCT
37+
emr_questionnaireresponse.id AS response_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+
WHERE emr_questionnaireresponse.questionnaire_id = 68
45+
AND emr_questionnaireresponse.status = 'completed'
46+
AND emr_questionnaireresponse.encounter_id IS NOT NULL
47+
AND response_element ->> 'question_id' = '8d5c73c2-b5ba-4b6f-9d0f-295a4c105fec'
48+
AND response_element -> 'values' -> 0 ->> 'value' = 'Doctor with Community Nurse'
49+
--[[AND {{date}}]]
50+
)
51+
SELECT
52+
facility_facility.name AS facility_name,
53+
COUNT(DISTINCT doctor_visits.response_id) AS doctor_only_visit_count
54+
FROM doctor_visits
55+
INNER JOIN emr_encounter
56+
ON emr_encounter.id = doctor_visits.encounter_id
57+
INNER JOIN facility_facility
58+
ON facility_facility.id = emr_encounter.facility_id
59+
AND facility_facility.deleted = FALSE
60+
GROUP BY facility_facility.name
61+
ORDER BY doctor_only_visit_count DESC, facility_facility.name;
62+
```
63+
64+
## Notes
65+
66+
- **Hardcoded IDs:**
67+
- `questionnaire_id = 68` — the doctor visit questionnaire at Karunashraya.
68+
- **`organization_id` is required** (no `[[...]]` wrapper) — the query will not run without a value.
69+
- Results are ordered by visit count descending, then alphabetically by facility name for ties.
70+
71+
*Last updated: 2026-07-15*

0 commit comments

Comments
 (0)