-
Notifications
You must be signed in to change notification settings - Fork 1
Add Mobility Status Report by District documentation #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
14b3b05
Add Mobility Status Report by District documentation and SQL query
sonzsara 5311030
Refactor SQL query in Mobility Status Report for improved readability…
sonzsara a5f42f6
Update mobility status report query to filter completed responses
sonzsara a30e839
Update mobility_status_report_by_district_kc.md
sonzsara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
|
|
||
| # Mobility Status Report by District | ||
|
sonzsara marked this conversation as resolved.
|
||
|
|
||
| > Count of patients per district by their latest mobility status (Bedbound / Homebound / Independently Active) | ||
|
|
||
| ## Purpose | ||
|
|
||
| For each patient, find the **most recent** answer to the mobility-status question and pivot the counts by the patient's district. | ||
|
sonzsara marked this conversation as resolved.
|
||
|
|
||
| ## Parameters | ||
|
|
||
| | Parameter | Type | Description | Example | | ||
| |-----------|------|-------------|---------| | ||
| | `date` | DATE / range | Metabase date filter (typically bound to `emr_questionnaireresponse.created_date`) | `'2026-06-01'` | | ||
|
|
||
| --- | ||
|
|
||
| ## Query | ||
|
|
||
| ```sql | ||
| WITH latest_mobility AS ( | ||
| SELECT DISTINCT ON (emr_questionnaireresponse.patient_id) | ||
| emr_questionnaireresponse.patient_id, | ||
| emr_patient.organization_cache, | ||
| rs -> 'values' -> 0 ->> 'value' AS mobility_value | ||
| FROM emr_questionnaireresponse | ||
| JOIN emr_patient | ||
| ON emr_patient.id = emr_questionnaireresponse.patient_id | ||
|
|
||
| AND emr_patient.deceased_datetime IS NULL | ||
| CROSS JOIN LATERAL jsonb_array_elements(emr_questionnaireresponse.responses) AS rs | ||
| WHERE emr_questionnaireresponse.questionnaire_id = 69 | ||
| AND emr_questionnaireresponse.status = 'completed' | ||
| AND emr_questionnaireresponse.responses @> '[{"question_id": "e4b0d3f4-77fb-4fb6-9213-9c62fa6b5695"}]'::jsonb | ||
| AND rs ->> 'question_id' = 'e4b0d3f4-77fb-4fb6-9213-9c62fa6b5695' | ||
|
|
||
| --[[AND {{date}}]] | ||
| ORDER BY emr_questionnaireresponse.patient_id, emr_questionnaireresponse.created_date DESC | ||
| ) | ||
| SELECT | ||
| emr_organization.name AS district_name, | ||
| COUNT(*) FILTER (WHERE mobility_value ILIKE '%bedbound%') AS "Bedbound", | ||
| COUNT(*) FILTER (WHERE mobility_value ILIKE '%homebound%') AS "Homebound", | ||
| COUNT(*) FILTER (WHERE mobility_value ILIKE '%independent%') AS "Independently Active" | ||
| FROM latest_mobility | ||
| JOIN emr_organization | ||
| ON emr_organization.id = ANY(latest_mobility.organization_cache) | ||
| AND emr_organization.level_cache = 1 | ||
|
|
||
| GROUP BY emr_organization.name | ||
| ORDER BY emr_organization.name; | ||
| ``` | ||
|
|
||
| ## Notes | ||
|
|
||
| - **Metabase filter:** `[[AND {{date}}]]` is a field filter — bind it to `emr_questionnaireresponse.created_date` in the Metabase variable settings. | ||
| - Results are ordered alphabetically by `district_name`. | ||
|
|
||
| *Last updated: 2026-06-19* | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.