From 1fed01c475c876ee5f0141b533ee3104b406666f Mon Sep 17 00:00:00 2001 From: sonzsara Date: Tue, 11 Aug 2026 12:27:00 +0530 Subject: [PATCH 1/2] Add documentation for counting patients with high RBS --- .../count_of_patients_with_high_rbs.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Care/Clinical/count_of_patients_with_high_rbs.md diff --git a/Care/Clinical/count_of_patients_with_high_rbs.md b/Care/Clinical/count_of_patients_with_high_rbs.md new file mode 100644 index 0000000..b70e748 --- /dev/null +++ b/Care/Clinical/count_of_patients_with_high_rbs.md @@ -0,0 +1,47 @@ + +# Count of Patients with High RBS + +> Count of patients whose latest recorded RBS value is greater than 150 + +## Purpose + +Counts patients whose most recent recorded RBS value from the specified questionnaire is above `150`. + +## Parameters + +| Parameter | Type | Description | Example | +|-----------|------|-------------|---------| +| `date_filter` | DATE / range | Metabase date filter (typically bound to `emr_questionnaireresponse.created_date`) | `'2026-08-01'` | + +--- + +## Query + +```sql +WITH latest_sbp AS ( + SELECT DISTINCT ON (emr_questionnaireresponse.patient_id) + emr_questionnaireresponse.patient_id AS patient_id, + NULLIF(regexp_replace(answer_element->>'value', '[^0-9.]', '', 'g'), '')::numeric AS systolic_bp + FROM emr_questionnaireresponse + CROSS JOIN LATERAL jsonb_array_elements(emr_questionnaireresponse.responses) AS response_element + CROSS JOIN LATERAL jsonb_array_elements(response_element->'values') AS answer_element + WHERE emr_questionnaireresponse.deleted = FALSE + AND emr_questionnaireresponse.questionnaire_id IN (115) + AND response_element->>'question_id' = 'b000f459-1efb-4e1b-9579-2e568f9aa510' + --[[AND {{date_filter}}]] + ORDER BY emr_questionnaireresponse.patient_id, emr_questionnaireresponse.created_date DESC +) +SELECT COUNT(*) AS patients_with_rbs +FROM latest_sbp +WHERE latest_sbp.systolic_bp > 150; +``` + +## Notes + +- **Hardcoded IDs:** + - `questionnaire_id = 115` identifies the questionnaire. + - `question_id = 'b000f459-1efb-4e1b-9579-2e568f9aa510'` identifies the RBS field. + Update these if the form configuration changes. +- **Metabase filter:** `[[AND {{date_filter}}]]` is a field filter — bind it to `emr_questionnaireresponse.created_date`. + +*Last updated: 2026-08-11* From 2c14fbde204eaaff475a233ced3c13fa62f6a34b Mon Sep 17 00:00:00 2001 From: Sona Sara Shibu Date: Wed, 12 Aug 2026 16:46:06 +0530 Subject: [PATCH 2/2] Fix SQL query to use completed status for responses --- Care/Clinical/count_of_patients_with_high_rbs.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Care/Clinical/count_of_patients_with_high_rbs.md b/Care/Clinical/count_of_patients_with_high_rbs.md index b70e748..c6d67fc 100644 --- a/Care/Clinical/count_of_patients_with_high_rbs.md +++ b/Care/Clinical/count_of_patients_with_high_rbs.md @@ -21,11 +21,11 @@ Counts patients whose most recent recorded RBS value from the specified question WITH latest_sbp AS ( SELECT DISTINCT ON (emr_questionnaireresponse.patient_id) emr_questionnaireresponse.patient_id AS patient_id, - NULLIF(regexp_replace(answer_element->>'value', '[^0-9.]', '', 'g'), '')::numeric AS systolic_bp + (answer_element->>'value')::numeric AS systolic_bp FROM emr_questionnaireresponse CROSS JOIN LATERAL jsonb_array_elements(emr_questionnaireresponse.responses) AS response_element CROSS JOIN LATERAL jsonb_array_elements(response_element->'values') AS answer_element - WHERE emr_questionnaireresponse.deleted = FALSE + WHERE emr_questionnaireresponse.status = 'completed' AND emr_questionnaireresponse.questionnaire_id IN (115) AND response_element->>'question_id' = 'b000f459-1efb-4e1b-9579-2e568f9aa510' --[[AND {{date_filter}}]]