diff --git a/Care/Clinical/ESAS-r_pallium.md b/Care/Clinical/ESAS-r_pallium.md new file mode 100644 index 0000000..cf05cab --- /dev/null +++ b/Care/Clinical/ESAS-r_pallium.md @@ -0,0 +1,156 @@ +# ESAS-r Forms Filled (Pallium) + +> Returns the count of ESAS-r forms filled, with optional filters for date, patient, and staff. + +## Purpose + +To track the number of ESAS-r (Edmonton Symptom Assessment System - revised) forms filled for patients, supporting symptom monitoring and quality of care. + +## Parameters + +| Parameter | Type | Description | Example | +|---------------|--------|---------------------------------------------|----------------| +| `date` | DATE | Filter by form date (optional) | '2025-12-01' | +| `patient_name`| TEXT | Filter by patient name (optional) | 'John Doe' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | + +--- + +## Query + +```sql +SELECT + COUNT(*) AS forms_filled_count +FROM emr_questionnaireresponse qr +JOIN emr_patient p ON qr.patient_id = p.id +JOIN users_user u ON qr.created_by_id = u.id +WHERE qr.questionnaire_id = 26 + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{date}} ]] + [[AND p.name = {{patient_name}} ]] + [[AND TRIM(u.first_name || ' ' || COALESCE(u.last_name, '')) = {{staff_name}} ]] + */ +; +``` + + + +## Drill-Down Query + +> Returns detailed ESAS-r form information for each patient, including staff, and all ESAS-r symptom fields. + +### Purpose + +To provide a detailed list of ESAS-r form responses for each patient, supporting clinical review and patient-level analysis. + +### Parameters + +| Parameter | Type | Description | Example | +|---------------|--------|---------------------------------------------|----------------| +| `date` | DATE | Filter by form date (optional) | '2025-12-01' | +| `patient_name`| TEXT | Filter by patient name (optional) | 'John Doe' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | + +--- + +```sql +SELECT + p.name AS patient_name, + p.gender, + EXTRACT(YEAR FROM CURRENT_DATE) - p.year_of_birth AS age, + pi.value AS MRnumber, + p.phone_number, + qr.created_date AS form_created_date, + TRIM(u.first_name || ' ' || COALESCE(u.last_name, '')) AS staff_name, + -- Each question as separate column + ( + SELECT val ->> 'value' + FROM jsonb_array_elements(qr.responses) resp + JOIN LATERAL jsonb_array_elements(resp -> 'values') val ON TRUE + WHERE resp ->> 'question_id' = 'f960e788-a131-4764-a3ea-b25e8586f004' + LIMIT 1 + ) AS pain, + ( + SELECT val ->> 'value' + FROM jsonb_array_elements(qr.responses) resp + JOIN LATERAL jsonb_array_elements(resp -> 'values') val ON TRUE + WHERE resp ->> 'question_id' = '9147b7b1-db85-44ad-b071-34ac4aff3019' + LIMIT 1 + ) AS tiredness, + ( + SELECT val ->> 'value' + FROM jsonb_array_elements(qr.responses) resp + JOIN LATERAL jsonb_array_elements(resp -> 'values') val ON TRUE + WHERE resp ->> 'question_id' = '8db8d2b6-3660-4b19-9a80-7f7cd8f7de03' + LIMIT 1 + ) AS drowsiness, + ( + SELECT val ->> 'value' + FROM jsonb_array_elements(qr.responses) resp + JOIN LATERAL jsonb_array_elements(resp -> 'values') val ON TRUE + WHERE resp ->> 'question_id' = 'c2fb0cbe-6e74-486f-ba02-1f9f81291739' + LIMIT 1 + ) AS nausea, + ( + SELECT val ->> 'value' + FROM jsonb_array_elements(qr.responses) resp + JOIN LATERAL jsonb_array_elements(resp -> 'values') val ON TRUE + WHERE resp ->> 'question_id' = '6d1836fb-d163-4524-a3a2-3dc7c99054dc' + LIMIT 1 + ) AS shortness_of_breath, + ( + SELECT val ->> 'value' + FROM jsonb_array_elements(qr.responses) resp + JOIN LATERAL jsonb_array_elements(resp -> 'values') val ON TRUE + WHERE resp ->> 'question_id' = '7e95e2c7-8c98-482a-8cd6-448e2fd4fca6' + LIMIT 1 + ) AS depression, + ( + SELECT val ->> 'value' + FROM jsonb_array_elements(qr.responses) resp + JOIN LATERAL jsonb_array_elements(resp -> 'values') val ON TRUE + WHERE resp ->> 'question_id' = 'cdaf2000-7fc6-4bee-9a73-c6ed106f3397' + LIMIT 1 + ) AS anxiety, + ( + SELECT val ->> 'value' + FROM jsonb_array_elements(qr.responses) resp + JOIN LATERAL jsonb_array_elements(resp -> 'values') val ON TRUE + WHERE resp ->> 'question_id' = '9ecc4269-8d7a-4822-9921-7ef0c1e63515' + LIMIT 1 + ) AS best_wellbeing, + ( + SELECT val ->> 'value' + FROM jsonb_array_elements(qr.responses) resp + JOIN LATERAL jsonb_array_elements(resp -> 'values') val ON TRUE + WHERE resp ->> 'question_id' = '668ad395-4a92-47f0-89a8-58cd83a599a7' + LIMIT 1 + ) AS no_other_problem +FROM emr_questionnaireresponse qr +JOIN emr_patient p ON qr.patient_id = p.id +LEFT JOIN emr_patientidentifier pi ON pi.patient_id = p.id AND pi.config_id = 5 +LEFT JOIN users_user u ON qr.created_by_id = u.id +WHERE qr.questionnaire_id = 26 + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{date}} ]] + [[AND p.name = {{patient_name}} ]] + [[AND TRIM(u.first_name || ' ' || COALESCE(u.last_name, '')) = {{staff_name}} ]] + */ +ORDER BY qr.created_date DESC, patient_name; +``` + + +## Notes + +- Metabase-specific filters (`[[...]]`) allow dynamic filtering in dashboards. +- The queries use `questionnaire_id = 26` to identify the ESAS-r form. +- Each question_id (e.g., `f960e788-a131-4764-a3ea-b25e8586f004`) corresponds to a specific ESAS-r symptom and is displayed as a separate column in the drill-down output. +- The `config_id = 5` condition is used to select the correct Medical Record number (MRnumber) for each patient. +- Ensure all referenced tables and fields exist and are mapped correctly. +- All filters are optional and applied dynamically by Metabase. + +*Last updated: 2025-12-15* diff --git a/Care/Clinical/bladdercathetermethod_pallium.md b/Care/Clinical/bladdercathetermethod_pallium.md new file mode 100644 index 0000000..fc90a45 --- /dev/null +++ b/Care/Clinical/bladdercathetermethod_pallium.md @@ -0,0 +1,192 @@ +# Bladder Catheter Method + +> Returns the count of patients by bladder catheter status, with optional filters for visit date, patient, and staff. + +## Purpose + +To analyze the distribution of bladder catheter methods/status among patients, supporting clinical reporting and device management. + +## Parameters + +| Parameter | Type | Description | Example | +|---------------|--------|---------------------------------------------|----------------| +| `visit_date` | DATE | Filter by visit date (optional) | '2025-12-01' | +| `patient_name` | TEXT | Filter by patient name (optional) | 'John Doe' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | + +--- + +## Query + +```sql +WITH latest_responses AS ( + SELECT DISTINCT ON (patient_id) + emr_questionnaireresponse.patient_id, + emr_questionnaireresponse.responses, + emr_questionnaireresponse.created_by_id + FROM emr_questionnaireresponse + WHERE emr_questionnaireresponse.questionnaire_id IN (17) + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{visit_date}}]] + */ + ORDER BY patient_id, created_date DESC +), +responses AS ( + SELECT + lr.patient_id, + lr.created_by_id, + UPPER( + TRIM( + REPLACE( + COALESCE(val ->> 'value', val -> 'coding' ->> 'display'), + '_', + ' ' + ) + ) + ) AS status + FROM + latest_responses lr, + jsonb_array_elements(lr.responses) AS resp, + jsonb_array_elements(resp -> 'values') AS val + WHERE + resp ->> 'question_id' IN ( + '808f625b-3df1-4b4e-830e-9ee08f6a01b9' + ) +) +SELECT + r.status AS "Status", + COUNT(DISTINCT r.patient_id) AS "Patient Count" +FROM + responses r +JOIN emr_patient p ON r.patient_id = p.id +LEFT JOIN users_user u ON r.created_by_id = u.id +WHERE 1=1 + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND p.name = {{patient_name}} ]] + [[AND TRIM(u.first_name || ' ' || COALESCE(u.last_name, '')) = {{staff_name}} ]] + */ +GROUP BY + r.status +ORDER BY + "Patient Count" DESC; +``` + + + +## Drill-Down Query + +> Returns detailed patient information for each bladder catheter status, including demographics, staff, and device details. + +### Purpose + +To provide a detailed list of patients for each bladder catheter status, supporting patient-level review and device management. + +### Parameters + +| Parameter | Type | Description | Example | +|---------------|--------|---------------------------------------------|----------------| +| `visit_date` | DATE | Filter by visit date (optional) | '2025-12-01' | +| `catheter_type`| TEXT | Filter by catheter type (optional) | 'FOLEY' | +| `status` | TEXT | Filter by bladder catheter status (optional)| 'CBD' | +| `patient_name` | TEXT | Filter by patient name (optional) | 'John Doe' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | + +--- + +```sql +WITH latest_responses AS ( + SELECT DISTINCT ON (patient_id) + emr_questionnaireresponse.patient_id, + emr_questionnaireresponse.responses, + emr_questionnaireresponse.created_date, + emr_questionnaireresponse.created_by_id + FROM + emr_questionnaireresponse + WHERE + emr_questionnaireresponse.questionnaire_id = 17 + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{visit_date}}]] + */ + ORDER BY patient_id, created_date DESC +), +drill_data AS ( + SELECT + p.name AS patient_name, + p.gender, + p.phone_number, + EXTRACT(YEAR FROM CURRENT_DATE) - p.year_of_birth AS age, + pi.value AS MRnumber, + lr.created_date, + TRIM(u.first_name || ' ' || COALESCE(u.last_name, '')) AS staff_name, + UPPER( + TRIM( + REPLACE( + COALESCE( + MAX(CASE WHEN resp->>'question_id' = '808f625b-3df1-4b4e-830e-9ee08f6a01b9' THEN val->>'value' END), + MAX(CASE WHEN resp->>'question_id' = '808f625b-3df1-4b4e-830e-9ee08f6a01b9' THEN val->'coding'->>'display' END) + ), + '_', ' ' + ) + ) + ) AS status, + MAX(CASE + WHEN resp->>'question_id' = 'c64c2638-6ce0-4814-97b2-5fda4832c30a' THEN val->>'value' + WHEN resp->>'question_id' = 'd9708732-385a-4559-9169-2989837aa0fb' THEN val->>'value' + END + ) AS catheter_type, + MAX(CASE WHEN resp->>'question_id' = 'ec38712c-21d2-4fae-b9b6-d29b788c2c06' THEN val->>'value' END) AS issues, + MAX(CASE WHEN resp->>'question_id' = 'dc94d0fd-8986-4fa9-b76e-79223cb73f3a' THEN val->>'value' END) AS size, + MAX(CASE WHEN resp->>'question_id' = '30f32642-f04a-48b5-8cf3-36eeb0f5c5d5' THEN val->>'value' END) AS sterile_water_uses, + MAX(CASE WHEN resp->>'question_id' = '1086dea0-5aa4-4e4f-9209-533cc20f4d7a' THEN val->>'value' END) AS change_on, + MAX(CASE WHEN resp->>'question_id' = '7b2b8458-3a12-4f85-b142-2efc7ef1a6c3' THEN val->>'value' END) AS next_catheter_change_date + FROM latest_responses lr + JOIN emr_patient p ON p.id = lr.patient_id + LEFT JOIN emr_patientidentifier pi ON pi.patient_id = p.id AND pi.config_id = 5 + LEFT JOIN users_user u ON lr.created_by_id = u.id + LEFT JOIN LATERAL jsonb_array_elements(lr.responses) AS resp ON TRUE + LEFT JOIN LATERAL jsonb_array_elements(COALESCE(resp->'values', '[]'::jsonb)) AS val ON TRUE + WHERE resp->>'question_id' IN ( + '808f625b-3df1-4b4e-830e-9ee08f6a01b9', + 'c64c2638-6ce0-4814-97b2-5fda4832c30a', + 'd9708732-385a-4559-9169-2989837aa0fb', + 'ec38712c-21d2-4fae-b9b6-d29b788c2c06', + 'dc94d0fd-8986-4fa9-b76e-79223cb73f3a', + '30f32642-f04a-48b5-8cf3-36eeb0f5c5d5', + '1086dea0-5aa4-4e4f-9209-533cc20f4d7a', + '7b2b8458-3a12-4f85-b142-2efc7ef1a6c3' + ) + AND ( + (val->>'value' IS NOT NULL AND val->>'value' <> '') OR + (val->'coding'->>'display' IS NOT NULL AND val->'coding'->>'display' <> '') + ) + GROUP BY p.id, p.name, p.gender, p.phone_number, p.meta, pi.value, lr.created_date, u.first_name, u.last_name +) +SELECT * +FROM drill_data +--The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: +/* +WHERE 1=1 + [[AND catheter_type ILIKE '%' || {{catheter_type}} || '%']] + [[AND status ILIKE '%' || {{status}} || '%']] + [[AND patient_name ={{patient_name}} ]] + [[AND staff_name {{staff_name}} ]] +*/ +ORDER BY created_date DESC, patient_name; +``` + + +## Notes + +- Metabase-specific filters (`[[...]]`) allow dynamic filtering in dashboards. +- The queries use `questionnaire_id = 17` and specific question_ids to extract bladder catheter status and related details. +- The `config_id = 5` condition is used to select the correct Medical Record number (MRnumber) for each patient. +- Ensure all referenced tables and fields exist and are mapped correctly. +- All filters are optional and applied dynamically by Metabase. + +*Last updated: 2025-12-15* diff --git a/Care/Clinical/diagnosis_pallium.md b/Care/Clinical/diagnosis_pallium.md new file mode 100644 index 0000000..5684224 --- /dev/null +++ b/Care/Clinical/diagnosis_pallium.md @@ -0,0 +1,103 @@ +# Diagnosis Counts by Condition + +> Returns the count of patients by diagnosis, with optional filters for date, patient, and staff. + +## Purpose + +To analyze the distribution of diagnoses among patients, supporting clinical reporting and disease burden analysis. + +## Parameters + +| Parameter | Type | Description | Example | +|---------------|--------|---------------------------------------------|----------------| +| `date` | DATE | Filter by diagnosis date (optional) | '2025-12-01' | +| `patient_name`| TEXT | Filter by patient name (optional) | 'John Doe' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | + +--- + +## Query + +```sql +SELECT + c.code #>> ARRAY['display'] AS diagnosis, + COUNT(DISTINCT c.patient_id) AS count +FROM + public.emr_condition c +JOIN public.emr_encounter e ON c.encounter_id = e.id +JOIN public.emr_patient p ON e.patient_id = p.id +LEFT JOIN public.users_user u ON c.created_by_id = u.id +WHERE + c.category IN ('encounter_diagnosis', 'chronic_condition') + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{date}}]] + [[AND p.name = {{patient_name}} ]] + [[AND TRIM(u.first_name || ' ' || COALESCE(u.last_name, '')) = {{staff_name}} ]] + */ +GROUP BY + diagnosis +ORDER BY + count DESC; +``` + + +## Drill-Down Query + +> Returns detailed patient information for each diagnosis, including demographics, staff, and MR number. + +### Purpose + +To provide a detailed list of patients for each diagnosis, supporting patient-level review and clinical follow-up. + +### Parameters + +| Parameter | Type | Description | Example | +|---------------|--------|---------------------------------------------|----------------| +| `date` | DATE | Filter by diagnosis date (optional) | '2025-12-01' | +| `patient_name`| TEXT | Filter by patient name (optional) | 'John Doe' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | +| `diagnosis` | TEXT | Filter by diagnosis (optional) | 'Diabetes' | + +--- + +```sql +SELECT + cond.code #>> ARRAY['display'] AS diagnosis, + p.name AS patient_name, + p.gender, + p.phone_number, + EXTRACT(YEAR FROM CURRENT_DATE) - p.year_of_birth AS age, + p.address AS address, + TRIM(s.first_name || ' ' || COALESCE(s.last_name, '')) AS staff_name, + p.created_date, + pi.value AS MRnumber +FROM + public.emr_condition cond +JOIN public.emr_encounter e ON cond.encounter_id = e.id +JOIN public.emr_patient p ON e.patient_id = p.id +LEFT JOIN public.users_user s ON cond.created_by_id = s.id +LEFT JOIN public.emr_patientidentifier pi ON p.id = pi.patient_id AND pi.config_id = 5 +WHERE + p.deleted = false + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{date}}]] + [[AND p.name = {{patient_name}} ]] + [[AND TRIM(s.first_name || ' ' || COALESCE(s.last_name, '')) = {{staff_name}} ]] + [[AND cond.code #>> ARRAY['display'] = {{diagnosis}} ]] + */ +ORDER BY + patient_name; +``` + +## Notes + +- Metabase-specific filters (`[[...]]`) allow dynamic filtering in dashboards. +- The `config_id = 5` condition is used to select the correct Medical Record number (MRnumber) for each patient. +- Ensure all referenced tables and fields exist and are mapped correctly. +- All filters are optional and applied dynamically by Metabase. + +*Last updated: 2025-12-15* diff --git a/Care/Clinical/fimscore_pallium.md b/Care/Clinical/fimscore_pallium.md new file mode 100644 index 0000000..336e279 --- /dev/null +++ b/Care/Clinical/fimscore_pallium.md @@ -0,0 +1,174 @@ +# Patients with FIM Scores + +> Returns the count of unique patients with FIM scores recorded, with optional filters for visit date, form name, patient, and staff. + +## Purpose + +To track the number of patients who have Functional Independence Measure (FIM) scores recorded, supporting clinical quality and patient progress monitoring. + +## Parameters + +| Parameter | Type | Description | Example | +|---------------|--------|---------------------------------------------|----------------| +| `visit_date` | DATE | Filter by visit date (optional) | '2025-12-01' | +| `form_name` | TEXT | Filter by form name (optional) | 'Physiotherapy followup Form' | +| `patient_name` | TEXT | Filter by patient name (optional) | 'John Doe' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | + +--- + +## Query + +```sql +SELECT + COUNT(DISTINCT qr.patient_id) AS patients_with_fim_scores +FROM + emr_questionnaireresponse qr + JOIN emr_questionnaire q ON qr.questionnaire_id = q.id + JOIN emr_patient p ON qr.patient_id = p.id + LEFT JOIN users_user u ON qr.created_by_id = u.id + , jsonb_array_elements(qr.responses) AS resp + , jsonb_array_elements(resp -> 'values') AS val +WHERE + qr.questionnaire_id IN (19, 20) + AND resp ->> 'question_id' IN ( + -- Form 19 + '9d2b7f24-d0b8-40af-8477-20e25a1ac4bb', + 'b80b8d6e-ba90-49f3-9bbf-f21f70c049a5', + '63c57f69-2f83-4871-89de-91e0e689478f', + -- Form 20 + 'a2a6190e-62e8-497f-8e73-677e89c6bb71', + '82a6c01e-6b41-4dde-982a-0a27747039d5', + '57a929ca-8ed3-49f3-9bbf-f21f70c049a5' + ) + AND COALESCE(val ->> 'value', val -> 'coding' ->> 'display') IS NOT NULL + AND COALESCE(val ->> 'value', val -> 'coding' ->> 'display') <> '' + + ---The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{visit_date}}]] + [[AND q.title = {{form_name}}]] + [[AND p.name = {{patient_name}} ]] + [[AND TRIM(u.first_name || ' ' || COALESCE(u.last_name, '')) = {{staff_name}} ]] + */ +; +``` + + + +## Drill-Down Query + +> Returns detailed FIM score information for each patient, including form, staff, and score breakdowns. + +### Purpose + +To provide a detailed list of FIM scores for each patient, supporting clinical review and patient-level analysis. + +### Parameters + +| Parameter | Type | Description | Example | +|---------------|--------|---------------------------------------------|----------------| +| `visit_date` | DATE | Filter by visit date (optional) | '2025-12-01' | +| `form_name` | TEXT | Filter by form name (optional) | 'Physiotherapy followup Form' | +| `patient_name` | TEXT | Filter by patient name (optional) | 'John Doe' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | + +--- + +```sql +SELECT + q.title AS form_title, + p.name AS patient_name, + p.gender, + p.phone_number, + EXTRACT(YEAR FROM CURRENT_DATE) - p.year_of_birth AS age, + p.address, + pi.value AS MRnumber, + TRIM(u.first_name || ' ' || COALESCE(u.last_name, '')) AS staff_name, + qr.created_date, + MAX(CASE WHEN resp ->> 'question_id' IN ( + '9d2b7f24-d0b8-40af-8477-20e25a1ac4bb', + 'a2a6190e-62e8-497f-8e73-677e89c6bb71' + ) THEN (val ->> 'value')::integer END) AS admission_score, + MAX(CASE WHEN resp ->> 'question_id' IN ( + 'b80b8d6e-ba90-49f3-9bbf-f21f70c049a5', + '82a6c01e-6b41-4dde-982a-0a27747039d5' + ) THEN (val ->> 'value')::integer END) AS discharge_score, + MAX(CASE WHEN resp ->> 'question_id' IN ( + '63c57f69-2f83-4871-89de-91e0e689478f', + '57a929ca-8ed3-49f3-9bbf-f21f70c049a5' + ) THEN (val ->> 'value')::integer END) AS follow_up_score, + COALESCE( + MAX(CASE WHEN resp ->> 'question_id' IN ( + '9d2b7f24-d0b8-40af-8477-20e25a1ac4bb', + 'a2a6190e-62e8-497f-8e73-677e89c6bb71' + ) THEN (val ->> 'value')::integer END), 0 + ) + + COALESCE( + MAX(CASE WHEN resp ->> 'question_id' IN ( + 'b80b8d6e-ba90-49f3-9bbf-f21f70c049a5', + '82a6c01e-6b41-4dde-982a-0a27747039d5' + ) THEN (val ->> 'value')::integer END), 0 + ) + + COALESCE( + MAX(CASE WHEN resp ->> 'question_id' IN ( + '63c57f69-2f83-4871-89de-91e0e689478f', + '57a929ca-8ed3-49f3-9bbf-f21f70c049a5' + ) THEN (val ->> 'value')::integer END), 0 + ) AS total_score +FROM + emr_questionnaireresponse qr + JOIN emr_questionnaire q ON qr.questionnaire_id = q.id + JOIN emr_patient p ON qr.patient_id = p.id + LEFT JOIN emr_patientidentifier pi ON pi.patient_id = p.id AND pi.config_id = 5 + LEFT JOIN users_user u ON qr.created_by_id = u.id + , jsonb_array_elements(qr.responses) AS resp + , jsonb_array_elements(resp -> 'values') AS val +WHERE + qr.questionnaire_id IN (19, 20) + AND resp ->> 'question_id' IN ( + '9d2b7f24-d0b8-40af-8477-20e25a1ac4bb', + 'b80b8d6e-ba90-49f3-9bbf-f21f70c049a5', + '63c57f69-2f83-4871-89de-91e0e689478f', + 'a2a6190e-62e8-497f-8e73-677e89c6bb71', + '82a6c01e-6b41-4dde-982a-0a27747039d5', + '57a929ca-8ed3-49f3-9bbf-f21f70c049a5' + ) + AND COALESCE(val ->> 'value', val -> 'coding' ->> 'display') IS NOT NULL + AND COALESCE(val ->> 'value', val -> 'coding' ->> 'display') <> '' + + ---The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{visit_date}}]] + [[AND q.title = {{form_name}}]] + [[AND p.name ={{patient_name}} ]] + [[AND TRIM(u.first_name || ' ' || COALESCE(u.last_name, '')) = {{staff_name}} ]] + */ +GROUP BY + q.title, + p.name, + p.gender, + p.phone_number, + p.address, + p.year_of_birth, + pi.value, + u.first_name, + u.last_name, + qr.id, + qr.created_date +ORDER BY + p.name, + q.title, + qr.created_date DESC +``` + + + +## Notes + +- Metabase-specific filters (`[[...]]`) allow dynamic filtering in dashboards. +- FIM scores are extracted from questionnaire responses with IDs 19 and 20. +- Ensure all referenced tables and fields exist and are mapped correctly. +- All filters are optional and applied dynamically by Metabase. + +*Last updated: 2025-12-15* diff --git a/Care/Clinical/nursingservices_pallium.md b/Care/Clinical/nursingservices_pallium.md new file mode 100644 index 0000000..364aa5c --- /dev/null +++ b/Care/Clinical/nursingservices_pallium.md @@ -0,0 +1,203 @@ +# Nursing Services + +> Returns the count of patients by nursing services provided, with optional filters for visit date, patient, staff, and link center. + +## Purpose + +To analyze the distribution of nursing services provided to patients, supporting clinical reporting and service utilization analysis. + +## Parameters + +| Parameter | Type | Description | Example | +|---------------|--------|---------------------------------------------|----------------| +| `visit_date` | DATE | Filter by visit date (optional) | '2025-12-01' | +| `patient_name` | TEXT | Filter by patient name (optional) | 'John Doe' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | + +--- + +## Query + +```sql +WITH responses AS ( + SELECT + emr_questionnaireresponse.patient_id, + emr_questionnaireresponse.created_date, + emr_questionnaireresponse.created_by_id, + emr_questionnaireresponse.questionnaire_id, + resp ->> 'question_id' AS question_id, + UPPER( + TRIM( + REPLACE( + COALESCE(val ->> 'value', val -> 'coding' ->> 'display'), + '_', + ' ' + ) + ) + ) AS response_value + FROM + emr_questionnaireresponse + LEFT JOIN emr_encounter e ON emr_questionnaireresponse.encounter_id = e.id, + jsonb_array_elements(emr_questionnaireresponse.responses) AS resp, + jsonb_array_elements(resp -> 'values') AS val + WHERE + emr_questionnaireresponse.questionnaire_id IN (17, 15) + AND resp ->> 'question_id' IN ( + 'df782138-3239-4ce9-91fc-17f7f00fb456', + 'b5b910c6-46b2-4c7c-94ef-1f83b68b0af0' + ) + + -- The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{visit_date}}]] + */ +) +SELECT + r.response_value AS PROCEDURE, + COUNT(DISTINCT (r.patient_id, r.created_date)) AS patient_count +FROM + responses r +JOIN emr_patient p ON r.patient_id = p.id +LEFT JOIN users_user u ON r.created_by_id = u.id +WHERE 1=1 + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND p.name = {{patient_name}} ]] + [[AND TRIM(u.first_name || ' ' || COALESCE(u.last_name, '')) = {{staff_name}} ]] + */ +GROUP BY + r.response_value +ORDER BY + patient_count DESC; +``` + + +## Drill-Down Query + +> Returns detailed patient information for each nursing service provided, including staff, encounter, and link center. + +### Purpose + +To provide a detailed list of patients for each nursing service, supporting patient-level review and service tracking. + +### Parameters + +| Parameter | Type | Description | Example | +|---------------|--------|---------------------------------------------|----------------| +| `visit_date` | DATE | Filter by visit date (optional) | '2025-12-01' | +| `procedure` | TEXT | Filter by nursing service (optional) | 'WOUND CARE' | +| `patient_name` | TEXT | Filter by patient name (optional) | 'John Doe' | +| `link_center` | TEXT | Filter by link center (optional) | 'CENTER A' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | + +--- + +```sql +-- CTE to get latest link_center per patient +WITH latest_link_center AS ( + SELECT DISTINCT ON (qr.patient_id) + qr.patient_id, + UPPER( + TRIM( + REPLACE( + COALESCE(val ->> 'value', val -> 'coding' ->> 'display'), + '_', + ' ' + ) + ) + ) AS link_center, + qr.created_date + FROM + emr_questionnaireresponse qr, + jsonb_array_elements(qr.responses) AS resp, + jsonb_array_elements(resp -> 'values') AS val + WHERE + qr.questionnaire_id IN (17) + AND resp ->> 'question_id' = '697792a5-6240-4486-ad47-82298af35ab7' + + -- The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{visit_date}}]] + */ + ORDER BY + qr.patient_id, qr.created_date DESC +) + +-- Main drilldown query +SELECT + r.response_value AS procedure, + p.name AS patient_name, + p.gender, + p.phone_number, + EXTRACT(YEAR FROM CURRENT_DATE) - p.year_of_birth AS age, + p.address, + pi.value AS MRnumber, + TRIM(s.first_name || ' ' || COALESCE(s.last_name, '')) AS staff_name, + r.created_date, + r.questionnaire_id, + e.encounter_class, + llc.link_center +FROM ( + SELECT + qr.patient_id, + qr.created_date, + qr.created_by_id, + qr.questionnaire_id, + qr.encounter_id, + resp ->> 'question_id' AS question_id, + UPPER( + TRIM( + REPLACE( + COALESCE(val ->> 'value', val -> 'coding' ->> 'display'), + '_', + ' ' + ) + ) + ) AS response_value + FROM + emr_questionnaireresponse qr, + jsonb_array_elements(qr.responses) AS resp, + jsonb_array_elements(resp -> 'values') AS val + WHERE + qr.questionnaire_id IN (17, 15) + AND resp ->> 'question_id' IN ( + 'df782138-3239-4ce9-91fc-17f7f00fb456', + 'b5b910c6-46b2-4c7c-94ef-1f83b68b0af0' + ) + + -- The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{visit_date}}]] + */ +) r +JOIN emr_patient p ON r.patient_id = p.id +LEFT JOIN emr_encounter e ON r.encounter_id = e.id +LEFT JOIN emr_patientidentifier pi ON pi.patient_id = p.id AND pi.config_id = 5 +LEFT JOIN users_user s ON r.created_by_id = s.id +LEFT JOIN latest_link_center llc ON llc.patient_id = p.id +WHERE + p.deleted = FALSE + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND r.response_value = {{procedure}} ]] + [[AND p.name = {{patient_name}} ]] + [[AND llc.link_center ILIKE '%' || {{link_center}} || '%']] + [[AND TRIM(s.first_name || ' ' || COALESCE(s.last_name, '')) = {{staff_name}} ]] + */ +ORDER BY r.created_date DESC, patient_name; +``` + + +## Notes + +- Metabase-specific filters (`[[...]]`) allow dynamic filtering in dashboards. +- The queries use `questionnaire_id IN (17, 15)` to identify nursing services related forms. +- Specific question_ids are used to extract services provided data. +- The drill-down query includes the latest link center per patient using a CTE. +- The `config_id = 5` condition is used to select the correct Medical Record number (MRnumber) for each patient. +- Ensure all referenced tables and fields exist and are mapped correctly. +- All filters are optional and applied dynamically by Metabase. + +*Last updated: 2025-12-15* diff --git a/Care/Clinical/painscore_pallium.md b/Care/Clinical/painscore_pallium.md new file mode 100644 index 0000000..0b19bd6 --- /dev/null +++ b/Care/Clinical/painscore_pallium.md @@ -0,0 +1,98 @@ +# Pain Score + +> Returns the count of pain score forms filled, with optional filters for visit date, patient, and staff. + +## Purpose + +To track the number of pain score forms filled for patients, supporting pain management and quality of care. + +## Parameters + +| Parameter | Type | Description | Example | +|---------------|--------|---------------------------------------------|----------------| +| `visit_date` | DATE | Filter by visit date (optional) | '2025-12-01' | +| `patient_name` | TEXT | Filter by patient name (optional) | 'John Doe' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | + +--- + +## Query + +```sql +SELECT + COUNT(*) AS pain_score_forms_filled +FROM emr_questionnaireresponse qr +JOIN emr_patient p ON qr.patient_id = p.id +LEFT JOIN users_user u ON qr.created_by_id = u.id +WHERE qr.questionnaire_id = 27 + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{visit_date}} ]] + [[AND p.name = {{patient_name}} ]] + [[AND TRIM(u.first_name || ' ' || COALESCE(u.last_name, '')) = {{staff_name}} ]] + */ +; +``` + + + +## Drill-Down Query + +> Returns detailed pain score form information for each patient, including demographics, staff, and pain score value. + +### Purpose + +To provide a detailed list of pain score form responses for each patient, supporting clinical review and patient-level analysis. + +### Parameters + +| Parameter | Type | Description | Example | +|---------------|--------|---------------------------------------------|----------------| +| `visit_date` | DATE | Filter by visit date (optional) | '2025-12-01' | +| `patient_name` | TEXT | Filter by patient name (optional) | 'John Doe' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | + +--- + +```sql +SELECT + p.name AS patient_name, + p.gender, + EXTRACT(YEAR FROM CURRENT_DATE) - p.year_of_birth AS age, + pi.value AS MRnumber, + p.phone_number, + qr.created_date AS form_created_date, + TRIM(u.first_name || ' ' || COALESCE(u.last_name, '')) AS staff_name, + ( + SELECT val ->> 'value' + FROM jsonb_array_elements(qr.responses) resp + JOIN LATERAL jsonb_array_elements(resp -> 'values') val ON TRUE + WHERE resp ->> 'question_id' = 'pain_score_question_id' + LIMIT 1 + ) AS pain_score +FROM emr_questionnaireresponse qr +JOIN emr_patient p ON qr.patient_id = p.id +LEFT JOIN emr_patientidentifier pi ON pi.patient_id = p.id AND pi.config_id = 5 +LEFT JOIN users_user u ON qr.created_by_id = u.id +WHERE qr.questionnaire_id = 27 + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{visit_date}} ]] + [[AND p.name = {{patient_name}} ]] + [[AND TRIM(u.first_name || ' ' || COALESCE(u.last_name, '')) = {{staff_name}} ]] + */ +ORDER BY qr.created_date DESC, patient_name; +``` + +## Notes + +- Metabase-specific filters (`[[...]]`) allow dynamic filtering in dashboards. +- The queries use `questionnaire_id = 27` to identify the pain score form. +- The `pain_score_question_id` should be replaced with the actual question_id for the pain score in your form. +- The `config_id = 5` condition is used to select the correct Medical Record number (MRnumber) for each patient. +- Ensure all referenced tables and fields exist and are mapped correctly. +- All filters are optional and applied dynamically by Metabase. + +*Last updated: 2025-12-15* diff --git a/Care/Clinical/patientson_opiods_pallium.md b/Care/Clinical/patientson_opiods_pallium.md new file mode 100644 index 0000000..764ba51 --- /dev/null +++ b/Care/Clinical/patientson_opiods_pallium.md @@ -0,0 +1,129 @@ +# Patients on Opioids + +> Returns the count of patients on specific opioids, with optional filters for date, patient, and staff. + +## Purpose + +To analyze the number of patients administered specific opioids, supporting medication monitoring and pain management reporting. + +## Parameters + +| Parameter | Type | Description | Example | +|------------------|--------|---------------------------------------------|----------------| +| `created_date` | DATE | Filter by medication administration date (optional) | '2025-12-01' | +| `patient_name` | TEXT | Filter by patient name (optional) | 'John Doe' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | + +--- + +## Query + +```sql +SELECT + CASE + WHEN ma.medication->>'display' ILIKE '%morphine%' THEN 'Morphine' + WHEN ma.medication->>'display' ILIKE '%fentanyl%' THEN 'Fentanyl' + WHEN ma.medication->>'display' ILIKE '%methadone%' THEN 'Methadone' + WHEN ma.medication->>'display' ILIKE '%tramadol%' THEN 'Tramadol' + ELSE 'Other' + END AS medicine_name, + COUNT(DISTINCT ma.patient_id) AS patient_count +FROM + emr_medicationadministration ma +JOIN emr_patient p ON ma.patient_id = p.id +LEFT JOIN users_user u ON ma.created_by_id = u.id +WHERE + ma.medication->>'display' ILIKE ANY (ARRAY[ + '%morphine%', + '%fentanyl%', + '%methadone%', + '%tramadol%' + ]) + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{created_date}}]] + [[AND p.name = {{patient_name}} ]] + [[AND TRIM(u.first_name || ' ' || COALESCE(u.last_name, '')) = {{staff_name}} ]] + */ +GROUP BY + medicine_name +ORDER BY + patient_count DESC; +``` + + +## Drill-Down Query + +> Returns detailed patient and medication information for each opioid, including demographics, staff, and MR number. + +### Purpose + +To provide a detailed list of patients administered each opioid, supporting patient-level review and medication tracking. + +### Parameters + +| Parameter | Type | Description | Example | +|------------------|--------|---------------------------------------------|----------------| +| `created_date` | DATE | Filter by medication administration date (optional) | '2025-12-01' | +| `medicine_name` | TEXT | Filter by opioid name (optional) | 'Morphine' | +| `patient_name` | TEXT | Filter by patient name (optional) | 'John Doe' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | + +--- + +```sql +SELECT + CASE + WHEN m.medication->>'display' ILIKE '%morphine%' THEN 'Morphine' + WHEN m.medication->>'display' ILIKE '%fentanyl%' THEN 'Fentanyl' + WHEN m.medication->>'display' ILIKE '%methadone%' THEN 'Methadone' + WHEN m.medication->>'display' ILIKE '%tramadol%' THEN 'Tramadol' + ELSE 'Other' + END AS medicine_name, + p.name AS patient_name, + p.gender, + p.phone_number, + EXTRACT(YEAR FROM CURRENT_DATE) - p.year_of_birth AS age, + p.address, + pi.value AS MRnumber, + TRIM(s.first_name || ' ' || COALESCE(s.last_name, '')) AS staff_name, + m.created_date +FROM emr_medicationadministration m +JOIN emr_patient p ON m.patient_id = p.id +LEFT JOIN emr_patientidentifier pi ON pi.patient_id = p.id AND pi.config_id = 5 +LEFT JOIN users_user s ON m.created_by_id = s.id +WHERE + m.medication->>'display' ILIKE ANY (ARRAY[ + '%morphine%', + '%fentanyl%', + '%methadone%', + '%tramadol%' + ]) + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{created_date}}]] + [[AND CASE + WHEN m.medication->>'display' ILIKE '%morphine%' THEN 'Morphine' + WHEN m.medication->>'display' ILIKE '%fentanyl%' THEN 'Fentanyl' + WHEN m.medication->>'display' ILIKE '%methadone%' THEN 'Methadone' + WHEN m.medication->>'display' ILIKE '%tramadol%' THEN 'Tramadol' + ELSE 'Other' + END = {{medicine_name}} ]] + [[AND p.name ={{patient_name}} ]] + [[AND TRIM(s.first_name || ' ' || COALESCE(s.last_name, '')) ={{staff_name}} ]] + */ +ORDER BY m.created_date DESC, patient_name; +``` + + +## Notes + +- Metabase-specific filters (`[[...]]`) allow dynamic filtering in dashboards. +- The queries use medication display names to classify opioids. +- The `config_id = 5` condition is used to select the correct Medical Record number (MRnumber) for each patient. +- Ensure all referenced tables and fields exist and are mapped correctly. +- All filters are optional and applied dynamically by Metabase. + +*Last updated: 2025-12-15* diff --git a/Care/Clinical/patientsonantibiotics_pallium.md b/Care/Clinical/patientsonantibiotics_pallium.md new file mode 100644 index 0000000..1ec2ec7 --- /dev/null +++ b/Care/Clinical/patientsonantibiotics_pallium.md @@ -0,0 +1,143 @@ +# Patients on Antibiotics + +> Returns the count of antibiotic forms filled for patients, with optional filters for visit date, patient, and staff. + +## Purpose + +To track the number of patients for whom the antibiotics form was filled and patient care monitoring. + +## Parameters + +| Parameter | Type | Description | Example | +|---------------|--------|---------------------------------------------|----------------| +| `visit_date` | DATE | Filter by visit date (optional) | '2025-12-01' | +| `patient_name` | TEXT | Filter by patient name (optional) | 'John Doe' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | + +--- + +## Query + +```sql +SELECT + COUNT(*) AS form_count +FROM + emr_questionnaireresponse qr +JOIN emr_patient p ON qr.patient_id = p.id +LEFT JOIN users_user u ON qr.created_by_id = u.id +WHERE + qr.questionnaire_id = 25 + AND qr.deleted = false + + ---The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{visit_date}}]] + [[AND p.name = {{patient_name}} ]] + [[AND TRIM(u.first_name || ' ' || COALESCE(u.last_name, '')) = {{staff_name}} ]] + */ +; +``` + + + +## Drill-Down Query + +> Returns detailed antibiotic form information for each patient, staff, and all key antibiotic-related fields. + +### Purpose + +To provide a detailed list of antibiotic form responses for each patient, supporting clinical review and patient-level analysis. + +### Parameters + +| Parameter | Type | Description | Example | +|---------------|--------|---------------------------------------------|----------------| +| `visit_date` | DATE | Filter by visit date (optional) | '2025-12-01' | +| `patient_name` | TEXT | Filter by patient name (optional) | 'John Doe' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | + +--- + +```sql +WITH latest_responses AS ( + SELECT DISTINCT ON (patient_id) + emr_questionnaireresponse.patient_id, + emr_questionnaireresponse.responses, + emr_questionnaireresponse.created_date, + emr_questionnaireresponse.created_by_id + FROM + emr_questionnaireresponse + WHERE + emr_questionnaireresponse.questionnaire_id = 25 + + ---The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{visit_date}}]] + */ + ORDER BY + patient_id, created_date DESC +) +SELECT + p.name AS patient_name, + p.gender, + p.phone_number, + EXTRACT(YEAR FROM CURRENT_DATE) - p.year_of_birth AS age, + p.address, + pi.value AS MRnumber, + TRIM(u.first_name || ' ' || COALESCE(u.last_name, '')) AS staff_name, + lr.created_date, + MAX(CASE WHEN resp->>'question_id' = '9b0bd96b-5602-45f3-bdd8-f54cd0551caa' THEN val->>'value' END) AS Indication_details, + MAX(CASE WHEN resp->>'question_id' = '15ee191c-9160-4d95-9d00-117a6a414468' THEN val->>'value' END) AS suspected_or_confirmed, + MAX(CASE WHEN resp->>'question_id' = '1bcf2671-cf72-4e4f-82c3-caf789b52b8a' THEN val->>'value' END) AS cultures_ordered, + MAX(CASE WHEN resp->>'question_id' = 'dcd38ab3-676c-4290-bba4-b12a5f9ed4c2' THEN val->>'value' END) AS infection_details, + STRING_AGG(CASE WHEN resp->>'question_id' = '8f330a5a-1d5c-4344-8d6a-2463eb8113d7' THEN val->>'value' END, ', ') AS signs_and_symptoms, + MAX(CASE WHEN resp->>'question_id' = 'aed32016-1fc2-4da6-a7d8-a167a8d1a5d7' THEN val->>'value' END) AS antibiotic_initiation, + MAX(CASE WHEN resp->>'question_id' = 'b4f030fd-4f6a-4899-90f8-e94f1c12dd89' THEN val->>'value' END) AS continuation_of_antibiotic_prescribed_outside_of_Pallium, + MAX(CASE WHEN resp->>'question_id' = '9845cea0-459e-4ec3-bca2-6458d5f9df21' THEN val->>'value' END) AS changing_to_new_antibiotic, + MAX(CASE WHEN resp->>'question_id' = '3fce4a38-62e6-47ab-8740-ad254b24a65e' THEN val->>'value' END) AS choice_of_antibiotic, + MAX(CASE WHEN resp->>'question_id' = '266978ec-7a36-4aeb-af04-b578afc7c594' THEN val->>'value' END) AS treatment_duration, + MAX(CASE WHEN resp->>'question_id' = '61967479-574c-4cf3-ab82-5432c8586469' THEN val->>'value' END) AS antibiotics_provided, + MAX(CASE WHEN resp->>'question_id' = '50c00c3e-d52f-415c-a954-4d3eb9c01d8a' THEN val->>'value' END) AS education_provided +FROM latest_responses lr +JOIN emr_patient p ON p.id = lr.patient_id +LEFT JOIN emr_patientidentifier pi ON pi.patient_id = p.id AND pi.config_id = 5 +LEFT JOIN users_user u ON lr.created_by_id = u.id +LEFT JOIN LATERAL jsonb_array_elements(lr.responses) AS resp ON TRUE +LEFT JOIN LATERAL jsonb_array_elements(resp -> 'values') AS val ON TRUE +WHERE resp ->> 'question_id' in ( + '9b0bd96b-5602-45f3-bdd8-f54cd0551caa', + '15ee191c-9160-4d95-9d00-117a6a414468', + '1bcf2671-cf72-4e4f-82c3-caf789b52b8a', + 'dcd38ab3-676c-4290-bba4-b12a5f9ed4c2', + '8f330a5a-1d5c-4344-8d6a-2463eb8113d7', + 'aed32016-1fc2-4da6-a7d8-a167a8d1a5d7', + 'b4f030fd-4f6a-4899-90f8-e94f1c12dd89', + '9845cea0-459e-4ec3-bca2-6458d5f9df21', + '3fce4a38-62e6-47ab-8740-ad254b24a65e', + '266978ec-7a36-4aeb-af04-b578afc7c594', + '61967479-574c-4cf3-ab82-5432c8586469', + '50c00c3e-d52f-415c-a954-4d3eb9c01d8a' +) +AND val ->> 'value' IS NOT NULL AND val ->> 'value' <> '' + + ---The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND p.name = {{patient_name}} ]] + [[AND TRIM(u.first_name || ' ' || COALESCE(u.last_name, '')) = {{staff_name}} ]] +*/ +GROUP BY + p.id, p.name, p.gender, p.phone_number, p.address, p.meta, pi.value, lr.created_date, u.first_name, u.last_name +ORDER BY p.name; +``` + + +## Notes + +- Metabase-specific filters (`[[...]]`) allow dynamic filtering in dashboards. +- The main and drill-down queries use `questionnaire_id = 25` to identify the antibiotics form. +- Each question_id (e.g., `50c00c3e-d52f-415c-a954-4d3eb9c01d8a`) corresponds to a specific question in the antibiotics form and is displayed as a separate column in the drill-down output. +- The drill-down query extracts the latest antibiotic form per patient and all key antibiotic-related fields. +- Ensure all referenced tables and fields exist and are mapped correctly. +- All filters are optional and applied dynamically by Metabase. + +*Last updated: 2025-12-15* diff --git a/Care/Clinical/socialservices_pallium.md b/Care/Clinical/socialservices_pallium.md new file mode 100644 index 0000000..e8f2d53 --- /dev/null +++ b/Care/Clinical/socialservices_pallium.md @@ -0,0 +1,200 @@ +# Social Services + +> Returns the count of patients by social services provided, with optional filters for visit date, patient, staff, and link center. + +## Purpose + +To analyze the distribution of social services provided to patients, supporting clinical reporting and service utilization analysis. + +## Parameters + +| Parameter | Type | Description | Example | +|---------------|--------|---------------------------------------------|----------------| +| `visit_date` | DATE | Filter by visit date (optional) | '2025-12-01' | +| `patient_name` | TEXT | Filter by patient name (optional) | 'John Doe' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | + +--- + +## Query + +```sql +WITH responses AS ( + SELECT + emr_questionnaireresponse.patient_id, + emr_questionnaireresponse.created_date, + emr_questionnaireresponse.created_by_id, + emr_questionnaireresponse.questionnaire_id, + resp ->> 'question_id' AS question_id, + UPPER( + TRIM( + REPLACE( + COALESCE(val ->> 'value', val -> 'coding' ->> 'display'), + '_', + ' ' + ) + ) + ) AS response_value + FROM + emr_questionnaireresponse + LEFT JOIN emr_encounter e ON emr_questionnaireresponse.encounter_id = e.id, + jsonb_array_elements(emr_questionnaireresponse.responses) AS resp, + jsonb_array_elements(resp -> 'values') AS val + WHERE + emr_questionnaireresponse.questionnaire_id IN (18,16) + AND resp ->> 'question_id' IN ( + '0b023c57-8853-41c3-8c5d-64b79389b5ca', + '9f6e4f5-81d5-4270-ac71-81cb42c45f31' + ) + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{visit_date}}]] + */ +) +SELECT + r.response_value AS PROCEDURE, + COUNT(DISTINCT (r.patient_id, r.created_date)) AS patient_count +FROM + responses r +JOIN emr_patient p ON r.patient_id = p.id +LEFT JOIN users_user u ON r.created_by_id = u.id +WHERE 1=1 + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND p.name = {{patient_name}} ]] + [[AND TRIM(u.first_name || ' ' || COALESCE(u.last_name, '')) = {{staff_name}} ]] + */ +GROUP BY + r.response_value +ORDER BY + patient_count DESC; +``` + +## Drill-Down Query + +> Returns detailed patient information for each social service provided, including demographics, staff, encounter, and link center. + +### Purpose + +To provide a detailed list of patients for each social service, supporting patient-level review and service tracking. + +### Parameters + +| Parameter | Type | Description | Example | +|---------------|--------|---------------------------------------------|----------------| +| `visit_date` | DATE | Filter by visit date (optional) | '2025-12-01' | +| `Service` | TEXT | Filter by social service (optional) | 'COUNSELING' | +| `patient_name` | TEXT | Filter by patient name (optional) | 'John Doe' | +| `link_center` | TEXT | Filter by link center (optional) | 'CENTER A' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | + +--- + +```sql +-- CTE for latest link center per patient +WITH latest_link_center AS ( + SELECT DISTINCT ON (qr.patient_id) + qr.patient_id, + UPPER( + TRIM( + REPLACE( + COALESCE(val ->> 'value', val -> 'coding' ->> 'display'), + '_', + ' ' + ) + ) + ) AS link_center, + qr.created_date + FROM + emr_questionnaireresponse qr, + jsonb_array_elements(qr.responses) AS resp, + jsonb_array_elements(resp -> 'values') AS val + WHERE + qr.questionnaire_id IN (18,16) + AND resp ->> 'question_id' IN ( 'ad37942c-da4e-43d3-bc5a-c5bcb716b6f7', '21aa0e28-477b-4a5e-88cb-59905046ab24') + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{visit_date}}]] + */ + ORDER BY + qr.patient_id, qr.created_date DESC +) + +SELECT + r.response_value AS procedure, + p.name AS patient_name, + p.gender, + p.phone_number, + EXTRACT(YEAR FROM CURRENT_DATE) - p.year_of_birth AS age, + p.address, + pi.value AS MRnumber, + TRIM(s.first_name || ' ' || COALESCE(s.last_name, '')) AS staff_name, + r.created_date, + r.questionnaire_id, + e.encounter_class, + llc.link_center AS latest_link_center +FROM ( + SELECT + qr.patient_id, + qr.created_date, + qr.created_by_id, + qr.questionnaire_id, + qr.encounter_id, + resp ->> 'question_id' AS question_id, + UPPER( + TRIM( + REPLACE( + COALESCE(val ->> 'value', val -> 'coding' ->> 'display'), + '_', + ' ' + ) + ) + ) AS response_value + FROM + emr_questionnaireresponse qr, + jsonb_array_elements(qr.responses) AS resp, + jsonb_array_elements(resp -> 'values') AS val + WHERE + qr.questionnaire_id IN (18,16) + AND resp ->> 'question_id' IN ( + '79f6e4f5-81d5-4270-ac71-81cb42c45f31', + '0b023c57-8853-41c3-8c5d-64b79389b5ca' + ) + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{visit_date}}]] + */ +) r +JOIN emr_patient p ON r.patient_id = p.id +LEFT JOIN emr_encounter e ON r.encounter_id = e.id +LEFT JOIN emr_patientidentifier pi ON pi.patient_id = p.id AND pi.config_id = 5 +LEFT JOIN users_user s ON r.created_by_id = s.id +LEFT JOIN latest_link_center llc ON p.id = llc.patient_id +WHERE + p.deleted = FALSE + /* + The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + [[AND r.response_value = {{Service}} ]] + [[AND p.name ILIKE {{patient_name}} ]] + [[AND llc.link_center ILIKE '%' || {{link_center}} || '%']] + [[AND TRIM(s.first_name || ' ' || COALESCE(s.last_name, '')) = {{staff_name}} ]] + */ +ORDER BY r.created_date DESC, patient_name; +``` + + +## Notes + +- Metabase-specific filters (`[[...]]`) allow dynamic filtering in dashboards. +- The queries use `questionnaire_id IN (18, 16)` to identify social services forms. +- Specific question_ids are used to extract services provided data and link center information. +- The drill-down query includes the latest link center per patient using a CTE. +- The `config_id = 5` condition is used to select the correct Medical Record number (MRnumber) for each patient. +- Ensure all referenced tables and fields exist and are mapped correctly. +- All filters are optional and applied dynamically by Metabase. + +*Last updated: 2025-12-15* diff --git a/Care/Clinical/woundtypes_pallium.md b/Care/Clinical/woundtypes_pallium.md new file mode 100644 index 0000000..b139f9d --- /dev/null +++ b/Care/Clinical/woundtypes_pallium.md @@ -0,0 +1,160 @@ +# Wound Types + +> Returns the count of patients by wound type (procedure), with optional filters for visit date, patient, and staff. + +## Purpose + +To analyze the distribution of wound types among patients, supporting clinical reporting and wound care management. + +## Parameters + +| Parameter | Type | Description | Example | +|---------------|--------|---------------------------------------------|----------------| +| `visit_date` | DATE | Filter by visit date (optional) | '2025-12-01' | +| `patient_name` | TEXT | Filter by patient name (optional) | 'John Doe' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | + +--- + +## Query + +```sql +WITH responses AS ( + SELECT + emr_questionnaireresponse.patient_id, + emr_questionnaireresponse.created_date, + emr_questionnaireresponse.created_by_id, + emr_questionnaireresponse.questionnaire_id, + resp ->> 'question_id' AS question_id, + UPPER( + TRIM( + REPLACE( + COALESCE(val ->> 'value', val -> 'coding' ->> 'display'), + '_', + ' ' + ) + ) + ) AS response_value + FROM + emr_questionnaireresponse + LEFT JOIN emr_encounter e ON emr_questionnaireresponse.encounter_id = e.id, + jsonb_array_elements(emr_questionnaireresponse.responses) AS resp, + jsonb_array_elements(resp -> 'values') AS val + WHERE + emr_questionnaireresponse.questionnaire_id IN (17) + AND resp ->> 'question_id' IN ( + 'b50632b5-c6ab-4822-87a7-7f2c495ee985' + ) + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{visit_date}}]] + */ +) +SELECT + r.response_value AS PROCEDURE, + COUNT(DISTINCT (r.patient_id, r.created_date)) AS patient_count +FROM + responses r +JOIN emr_patient p ON r.patient_id = p.id +LEFT JOIN users_user u ON r.created_by_id = u.id +WHERE 1=1 + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND p.name = {{patient_name}} ]] + [[AND TRIM(u.first_name || ' ' || COALESCE(u.last_name, '')) = {{staff_name}} ]] + */ +GROUP BY + r.response_value +ORDER BY + patient_count DESC; +``` + +## Drill-Down Query + +> Returns detailed patient information for each wound type, staff and MR number. + +### Purpose + +To provide a detailed list of patients for each wound type, supporting patient-level review and wound care management. + +### Parameters + +| Parameter | Type | Description | Example | +|---------------|--------|---------------------------------------------|----------------| +| `visit_date` | DATE | Filter by visit date (optional) | '2025-12-01' | +| `wound_type` | TEXT | Filter by wound type (optional) | 'PRESSURE ULCER'| +| `patient_name` | TEXT | Filter by patient name (optional) | 'John Doe' | +| `staff_name` | TEXT | Filter by staff full name (optional) | 'Jane Smith' | + +--- + +```sql +SELECT + r.response_value AS procedure, + p.name AS patient_name, + p.gender, + p.phone_number, + EXTRACT(YEAR FROM CURRENT_DATE) - p.year_of_birth AS age, + p.address, + pi.value AS MRnumber, + TRIM(s.first_name || ' ' || COALESCE(s.last_name, '')) AS staff_name, + r.created_date, + r.questionnaire_id +FROM ( + SELECT + qr.patient_id, + qr.created_date, + qr.created_by_id, + qr.questionnaire_id, + resp ->> 'question_id' AS question_id, + UPPER( + TRIM( + REPLACE( + COALESCE(val ->> 'value', val -> 'coding' ->> 'display'), + '_', + ' ' + ) + ) + ) AS response_value + FROM + emr_questionnaireresponse qr, + jsonb_array_elements(qr.responses) AS resp, + jsonb_array_elements(resp -> 'values') AS val + WHERE + qr.questionnaire_id IN (17) + AND resp ->> 'question_id' IN ( + 'b50632b5-c6ab-4822-87a7-7f2c495ee985' + ) + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND {{visit_date}}]] + */ +) r +JOIN emr_patient p ON r.patient_id = p.id +LEFT JOIN emr_patientidentifier pi ON pi.patient_id = p.id AND pi.config_id = 5 +LEFT JOIN users_user s ON r.created_by_id = s.id +WHERE + p.deleted = FALSE + + --The following filters are Metabase-specific and will be replaced by Metabase if parameters are provided: + /* + [[AND r.response_value ILIKE '%' || {{wound_type}} || '%' ]] + [[AND p.name = {{patient_name}} ]] + [[AND TRIM(s.first_name || ' ' || COALESCE(s.last_name, '')) = {{staff_name}} ]] + */ +ORDER BY r.created_date DESC, patient_name; +``` + + +## Notes + +- Metabase-specific filters (`[[...]]`) allow dynamic filtering in dashboards. +- The queries use `questionnaire_id = 17` and question_id `b50632b5-c6ab-4822-87a7-7f2c495ee985` to identify wound type responses. +- The `config_id = 5` condition is used to select the correct Medical Record number (MRnumber) for each patient. +- Ensure all referenced tables and fields exist and are mapped correctly. +- All filters are optional and applied dynamically by Metabase. + +*Last updated: 2025-12-15*