-
Notifications
You must be signed in to change notification settings - Fork 1
Add documentation for appointments rescheduled more than 5 times query #129
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
Open
sonzsara
wants to merge
2
commits into
main
Choose a base branch
from
ENG-676
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
Care/Encounter/appointments_rescheduled_more_than_5_times_arike.md
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,99 @@ | ||
|
|
||
| # Appointments Rescheduled More Than 5 Times | ||
|
|
||
| > Patients who have had more than 5 rescheduled appointments at Arike, with reschedule details and zone | ||
|
|
||
| ## Purpose | ||
|
|
||
| Identifies patients at Arike (`facility_id = 2`) whose appointments have been rescheduled more than 5 times in total. Each row is one rescheduled booking for such a patient, showing the patient details (name, phone, year of birth, gender, ADM, deceased status, zone), the booking note (reason), the staff who created the rescheduled booking, the slot start datetime, and the total reschedule count. | ||
|
|
||
| ## Parameters | ||
|
|
||
| | Parameter | Type | Description | Example | | ||
| |-----------|------|-------------|---------| | ||
| | `start_datetime` | DATE / range | Metabase date filter (typically bound to `emr_tokenslot.start_datetime`) | `'2026-07-01'` | | ||
| | `patient_name` | TEXT | Metabase text filter (typically bound to `emr_patient.name`) | `'John Doe'` | | ||
| | `zone_filter` | TEXT | Filter by resolved zone tag display (exact match) | `'Zone A'` | | ||
|
|
||
| --- | ||
|
|
||
| ## Query | ||
|
|
||
| ```sql | ||
| WITH rescheduled_bookings AS ( | ||
| SELECT | ||
| emr_patient.id AS patient_id, | ||
| emr_patient.name AS patient_name, | ||
| emr_patient.phone_number, | ||
| emr_patient.year_of_birth, | ||
| emr_patient.gender, | ||
| emr_tokenbooking.note AS reason, | ||
| emr_patientidentifier.value AS adm, | ||
| users_user.first_name || ' ' || users_user.last_name AS created_by_user, | ||
| emr_tokenslot.start_datetime, | ||
| CASE | ||
| WHEN emr_patient.deceased_datetime IS NULL THEN 'No' | ||
| ELSE 'Yes' | ||
| END AS deceased, | ||
| ( | ||
| SELECT COALESCE(MAX(emr_tagconfig.display), 'unassigned') | ||
| FROM unnest(emr_patient.instance_tags) AS tag_id | ||
| JOIN emr_tagconfig ON emr_tagconfig.id = tag_id | ||
| WHERE emr_tagconfig.parent_id = 55 | ||
| ) AS zone | ||
| FROM emr_tokenbooking | ||
| JOIN emr_tokenslot | ||
| ON emr_tokenbooking.token_slot_id = emr_tokenslot.id | ||
| JOIN emr_schedulableresource | ||
| ON emr_tokenslot.resource_id = emr_schedulableresource.id | ||
| JOIN emr_patient | ||
| ON emr_tokenbooking.patient_id = emr_patient.id | ||
| LEFT JOIN emr_patientidentifier | ||
| ON emr_patient.id = emr_patientidentifier.patient_id | ||
| AND emr_patientidentifier.config_id = 2 | ||
| JOIN users_user | ||
| ON emr_tokenbooking.created_by_id = users_user.id | ||
| WHERE emr_tokenbooking.status = 'rescheduled' | ||
| AND LOWER(TRIM(emr_patient.name)) NOT LIKE '%test%' | ||
| AND emr_schedulableresource.facility_id = 2 | ||
| --[[AND {{start_datetime}}]] | ||
| --[[AND {{patient_name}}]] | ||
| ), | ||
| patient_reschedule_count AS ( | ||
| SELECT | ||
| patient_id, | ||
| COUNT(*) AS reschedule_count | ||
| FROM rescheduled_bookings | ||
| GROUP BY patient_id | ||
| HAVING COUNT(*) > 5 | ||
| ) | ||
| SELECT | ||
| rb.patient_name, | ||
| rb.phone_number, | ||
| rb.year_of_birth, | ||
| rb.gender, | ||
| rb.reason, | ||
| rb.adm, | ||
| rb.created_by_user, | ||
| rb.start_datetime, | ||
| rb.deceased, | ||
| prc.reschedule_count, | ||
| rb.zone | ||
| FROM rescheduled_bookings rb | ||
| INNER JOIN patient_reschedule_count prc | ||
| ON rb.patient_id = prc.patient_id | ||
| WHERE 1=1 | ||
| --[[AND rb.zone = {{zone_filter}}]] | ||
| ORDER BY rb.patient_name; | ||
| ``` | ||
|
|
||
| ## Notes | ||
|
|
||
| - **Hardcoded IDs:** | ||
| - `facility_id = 2` — Arike facility. | ||
| - `emr_patientidentifier.config_id = 2` — ADM patient identifier configuration. | ||
| - `parent_id = 55` — parent tag for **zone**. | ||
|
Copilot marked this conversation as resolved.
|
||
| Update if the configuration changes. | ||
| - Results are ordered alphabetically by patient name. | ||
|
|
||
| *Last updated: 2026-07-15* | ||
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.