-
Notifications
You must be signed in to change notification settings - Fork 1
Add Doctor-wise OP Count report with new vs. revisit consultations #101
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
7
commits into
main
Choose a base branch
from
ENG-321
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.
+113
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6afa526
Add Doctor-wise OP Count report with new vs. revisit consultations
sonzsara b7194c8
Update Doctor-wise OP Count query to improve clarity and accuracy in …
sonzsara 59e9fe6
Fix formatting issue in Doctor-wise OP Count documentation
sonzsara 2ac7955
Update grouping in Doctor-wise OP Count query to include performer_ac…
sonzsara 60d6cbe
Refactor Doctor-wise OP Count query to improve clarity and accuracy i…
sonzsara c6ff8e4
Refactor Doctor-wise OP Count query for improved clarity and consiste…
sonzsara 46fa483
Update doctorwise_op_count_ssmm.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,113 @@ | ||
|
|
||
| # Doctor-wise OP Count - SSMM | ||
|
|
||
| > Per-doctor count of new vs. revisit OP consultations for yesterday, with a Total row | ||
|
|
||
| ## Purpose | ||
|
|
||
| Daily operational report showing each doctor's OP load for the previous day, split into: | ||
|
|
||
| - **New** — first paid/billed consultation between that patient and that doctor. | ||
| - **Revisit** — any subsequent paid/billed consultation between the same patient and doctor. | ||
|
|
||
|
|
||
| ## Parameters | ||
|
|
||
| *No parameters — the report is always for `CURRENT_DATE - INTERVAL '1 day'` (yesterday).* | ||
|
|
||
| --- | ||
|
|
||
|
sonzsara marked this conversation as resolved.
|
||
| ## Query | ||
|
|
||
| ```sql | ||
| WITH yesterday_visits AS ( | ||
| SELECT | ||
| emr_chargeitem.patient_id, | ||
| emr_chargeitem.performer_actor_id, | ||
| TRIM(COALESCE(users_user.prefix || ' ', '') || users_user.first_name || ' ' || users_user.last_name) AS doctor_name, | ||
| emr_tokenslot.start_datetime | ||
| FROM emr_tokenbooking | ||
| JOIN emr_tokenslot | ||
| ON emr_tokenbooking.token_slot_id = emr_tokenslot.id | ||
| JOIN emr_chargeitem | ||
| ON emr_tokenbooking.charge_item_id = emr_chargeitem.id | ||
| JOIN users_user | ||
| ON emr_chargeitem.performer_actor_id = users_user.id | ||
| WHERE emr_chargeitem.deleted = FALSE | ||
| AND emr_chargeitem.status IN ('paid', 'billed') | ||
| AND emr_chargeitem.performer_actor_id != 336 | ||
| AND emr_chargeitem.service_resource = 'appointment' | ||
| AND emr_tokenbooking.status IN ('checked_in', 'in_consultation', 'fulfilled') | ||
| AND emr_tokenslot.start_datetime >= CURRENT_DATE - INTERVAL '1 day' | ||
| AND emr_tokenslot.start_datetime < CURRENT_DATE | ||
| ), | ||
| yesterday_pairs AS ( | ||
| SELECT DISTINCT | ||
| patient_id, | ||
| performer_actor_id | ||
| FROM yesterday_visits | ||
| ), | ||
| first_visits AS ( | ||
| SELECT | ||
| emr_chargeitem.patient_id, | ||
| emr_chargeitem.performer_actor_id, | ||
| MIN(emr_tokenslot.start_datetime) AS first_visit_datetime | ||
| FROM emr_tokenbooking | ||
| JOIN emr_tokenslot | ||
| ON emr_tokenbooking.token_slot_id = emr_tokenslot.id | ||
| JOIN emr_chargeitem | ||
| ON emr_tokenbooking.charge_item_id = emr_chargeitem.id | ||
| JOIN yesterday_pairs | ||
| ON yesterday_pairs.patient_id = emr_chargeitem.patient_id | ||
| AND yesterday_pairs.performer_actor_id = emr_chargeitem.performer_actor_id | ||
| WHERE emr_chargeitem.deleted = FALSE | ||
| AND emr_chargeitem.status IN ('paid', 'billed') | ||
| AND emr_chargeitem.performer_actor_id != 336 | ||
| AND emr_chargeitem.service_resource = 'appointment' | ||
| AND emr_tokenbooking.status IN ('checked_in', 'in_consultation', 'fulfilled') | ||
| GROUP BY emr_chargeitem.patient_id, emr_chargeitem.performer_actor_id | ||
| ) | ||
|
|
||
| SELECT | ||
| performer_actor_id AS doctor_id, | ||
| doctor_name, | ||
| new, | ||
| revisit | ||
| FROM ( | ||
| SELECT | ||
| yesterday_visits.performer_actor_id, | ||
| yesterday_visits.doctor_name, | ||
| COUNT(*) FILTER (WHERE yesterday_visits.start_datetime = first_visits.first_visit_datetime) AS new, | ||
| COUNT(*) FILTER (WHERE yesterday_visits.start_datetime > first_visits.first_visit_datetime) AS revisit | ||
| FROM yesterday_visits | ||
| JOIN first_visits | ||
| ON first_visits.patient_id = yesterday_visits.patient_id | ||
| AND first_visits.performer_actor_id = yesterday_visits.performer_actor_id | ||
| GROUP BY yesterday_visits.performer_actor_id, yesterday_visits.doctor_name | ||
|
|
||
| UNION ALL | ||
|
|
||
| SELECT | ||
| NULL AS performer_actor_id, | ||
| 'Total' AS doctor_name, | ||
| COUNT(*) FILTER (WHERE yesterday_visits.start_datetime = first_visits.first_visit_datetime) AS new, | ||
| COUNT(*) FILTER (WHERE yesterday_visits.start_datetime > first_visits.first_visit_datetime) AS revisit | ||
| FROM yesterday_visits | ||
| JOIN first_visits | ||
| ON first_visits.patient_id = yesterday_visits.patient_id | ||
| AND first_visits.performer_actor_id = yesterday_visits.performer_actor_id | ||
| ) final_result | ||
| ORDER BY CASE WHEN doctor_name = 'Total' THEN 1 ELSE 0 END, doctor_name, performer_actor_id; | ||
| ``` | ||
|
|
||
|
|
||
| ## Notes | ||
|
|
||
| - **Date filter** — only bookings whose **slot** `start_datetime` falls between `CURRENT_DATE - INTERVAL '1 day'` (inclusive) and `CURRENT_DATE` (exclusive) are counted. | ||
| - **Hardcoded values:** | ||
| - `emr_chargeitem.performer_actor_id != 336` — excludes a specific user (casualty). Update or remove if that user changes. | ||
|
|
||
| *Last updated: 2026-05-25* | ||
|
|
||
|
|
||
|
|
||
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.