|
| 1 | + |
| 2 | +# Count of Patients with High RBS |
| 3 | + |
| 4 | +> Count of patients whose latest recorded RBS value is greater than 150 |
| 5 | +
|
| 6 | +## Purpose |
| 7 | + |
| 8 | +Counts patients whose most recent recorded RBS value from the specified questionnaire is above `150`. |
| 9 | + |
| 10 | +## Parameters |
| 11 | + |
| 12 | +| Parameter | Type | Description | Example | |
| 13 | +|-----------|------|-------------|---------| |
| 14 | +| `date_filter` | DATE / range | Metabase date filter (typically bound to `emr_questionnaireresponse.created_date`) | `'2026-08-01'` | |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## Query |
| 19 | + |
| 20 | +```sql |
| 21 | +WITH latest_sbp AS ( |
| 22 | + SELECT DISTINCT ON (emr_questionnaireresponse.patient_id) |
| 23 | + emr_questionnaireresponse.patient_id AS patient_id, |
| 24 | + NULLIF(regexp_replace(answer_element->>'value', '[^0-9.]', '', 'g'), '')::numeric AS systolic_bp |
| 25 | + FROM emr_questionnaireresponse |
| 26 | + CROSS JOIN LATERAL jsonb_array_elements(emr_questionnaireresponse.responses) AS response_element |
| 27 | + CROSS JOIN LATERAL jsonb_array_elements(response_element->'values') AS answer_element |
| 28 | + WHERE emr_questionnaireresponse.deleted = FALSE |
| 29 | + AND emr_questionnaireresponse.questionnaire_id IN (115) |
| 30 | + AND response_element->>'question_id' = 'b000f459-1efb-4e1b-9579-2e568f9aa510' |
| 31 | + --[[AND {{date_filter}}]] |
| 32 | + ORDER BY emr_questionnaireresponse.patient_id, emr_questionnaireresponse.created_date DESC |
| 33 | +) |
| 34 | +SELECT COUNT(*) AS patients_with_rbs |
| 35 | +FROM latest_sbp |
| 36 | +WHERE latest_sbp.systolic_bp > 150; |
| 37 | +``` |
| 38 | + |
| 39 | +## Notes |
| 40 | + |
| 41 | +- **Hardcoded IDs:** |
| 42 | + - `questionnaire_id = 115` identifies the questionnaire. |
| 43 | + - `question_id = 'b000f459-1efb-4e1b-9579-2e568f9aa510'` identifies the RBS field. |
| 44 | + Update these if the form configuration changes. |
| 45 | +- **Metabase filter:** `[[AND {{date_filter}}]]` is a field filter — bind it to `emr_questionnaireresponse.created_date`. |
| 46 | + |
| 47 | +*Last updated: 2026-08-11* |
0 commit comments