From e37a82c0c93da8561a8374f9ce225f288e91a799 Mon Sep 17 00:00:00 2001 From: Michael Terry Date: Tue, 18 Jun 2024 15:41:28 -0400 Subject: [PATCH] feat(c_resources_per_pt): add std_dev column and rename special values "* All" -> cumulus__all "* No recognized category" -> cumulus__none --- .../c_resources_per_pt.jinja | 49 ++++++++++--------- .../general/expected_summary.csv | 36 +++++++------- 2 files changed, 43 insertions(+), 42 deletions(-) diff --git a/cumulus_library_data_metrics/c_resources_per_pt/c_resources_per_pt.jinja b/cumulus_library_data_metrics/c_resources_per_pt/c_resources_per_pt.jinja index 22bc2a5..88b84f0 100644 --- a/cumulus_library_data_metrics/c_resources_per_pt/c_resources_per_pt.jinja +++ b/cumulus_library_data_metrics/c_resources_per_pt/c_resources_per_pt.jinja @@ -23,7 +23,7 @@ counts_by_pt AS ( SELECT patient_refs.ref AS patient, '{{ resource }}' AS resource, - cat_codes.category, + {{ utils.coalesce_missing('cat_codes.category') }} AS category, COUNT(DISTINCT src.id) AS num_resources FROM patient_refs LEFT JOIN {{ resource }} AS src @@ -37,6 +37,14 @@ counts_by_pt AS ( {% endfor %} ), +{% for resource in categories %} +cat_{{ resource|lower }}_values AS ( + SELECT DISTINCT category + FROM counts_by_pt + WHERE resource = '{{ resource }}' +), +{% endfor %} + {% for resource, field_name in patient_fields.items() %} summed_{{ resource|lower }}_counts AS ( SELECT @@ -48,12 +56,6 @@ summed_{{ resource|lower }}_counts AS ( ), {% endfor %} -total_patients AS ( - SELECT - COUNT(ref) AS total - FROM patient_refs -), - summed_counts AS ( SELECT patient, @@ -63,38 +65,37 @@ summed_counts AS ( ) SELECT - '* All' AS id, - '* All' AS category, + 'cumulus__all' AS id, + 'cumulus__all' AS category, CAST(AVG(num_resources) AS DECIMAL(18, 2)) AS average, + CAST(STDDEV_POP(num_resources) AS DECIMAL(18, 2)) AS std_dev, MAX(num_resources) AS "max" FROM summed_counts UNION {%- for resource in patient_fields %} SELECT '{{ resource }}' AS id, - '* All' AS category, + 'cumulus__all' AS category, CAST(AVG(num_resources) AS DECIMAL(18, 2)) AS average, + CAST(STDDEV_POP(num_resources) AS DECIMAL(18, 2)) AS std_dev, MAX(num_resources) AS "max" FROM summed_{{ resource|lower }}_counts WHERE resource = '{{ resource }}' {% if resource in categories %} UNION SELECT - resource AS id, - COALESCE(category, '* No recognized category') AS category, - CASE - WHEN ARBITRARY(tp.total) = 0 - THEN 0 - ELSE CAST( - CAST(SUM(num_resources) AS DECIMAL(18, 2)) / ARBITRARY(tp.total) - AS DECIMAL(18, 2) - ) - END AS average, + '{{ resource }}' AS id, + cat_values.category, + CAST(AVG(COALESCE(num_resources, 0)) AS DECIMAL(18, 2)) AS average, + CAST(STDDEV_POP(COALESCE(num_resources, 0)) AS DECIMAL(18, 2)) as std_dev, MAX(num_resources) AS "max" -FROM counts_by_pt AS counts -CROSS JOIN total_patients AS tp -WHERE resource = '{{ resource }}' -GROUP BY resource, category +FROM patient_refs +CROSS JOIN cat_{{ resource|lower }}_values AS cat_values +LEFT JOIN counts_by_pt AS counts +ON counts.resource = '{{ resource }}' +AND counts.patient = patient_refs.ref +AND counts.category = cat_values.category +GROUP BY cat_values.category HAVING MAX(num_resources) > 0 {% endif %} {%- if not loop.last %} diff --git a/tests/data/c_resources_per_pt/general/expected_summary.csv b/tests/data/c_resources_per_pt/general/expected_summary.csv index 7332888..69a24af 100644 --- a/tests/data/c_resources_per_pt/general/expected_summary.csv +++ b/tests/data/c_resources_per_pt/general/expected_summary.csv @@ -1,18 +1,18 @@ -id,category,average,max -ServiceRequest,* All,0.00,0 -Procedure,* All,0.00,0 -Observation,* All,0.00,0 -MedicationRequest,outpatient,1.33,2 -MedicationRequest,inpatient,0.33,1 -MedicationRequest,discharge; inpatient,0.33,1 -MedicationRequest,* No recognized category,0.33,1 -MedicationRequest,* All,2.33,4 -Immunization,* All,0.00,0 -Encounter,* All,0.00,0 -DocumentReference,* All,0.00,0 -DiagnosticReport,* All,0.00,0 -Device,* All,0.00,0 -Condition,* No recognized category,0.67,1 -Condition,* All,0.67,1 -AllergyIntolerance,* All,0.00,0 -* All,* All,3.00,4 +id,category,average,std_dev,max +cumulus__all,cumulus__all,3.00,0.82,4 +ServiceRequest,cumulus__all,0.00,0.00,0 +Procedure,cumulus__all,0.00,0.00,0 +Observation,cumulus__all,0.00,0.00,0 +MedicationRequest,outpatient,1.33,0.47,2 +MedicationRequest,inpatient,0.33,0.47,1 +MedicationRequest,discharge; inpatient,0.33,0.47,1 +MedicationRequest,cumulus__none,0.33,0.47,1 +MedicationRequest,cumulus__all,2.33,1.25,4 +Immunization,cumulus__all,0.00,0.00,0 +Encounter,cumulus__all,0.00,0.00,0 +DocumentReference,cumulus__all,0.00,0.00,0 +DiagnosticReport,cumulus__all,0.00,0.00,0 +Device,cumulus__all,0.00,0.00,0 +Condition,cumulus__none,0.67,0.47,1 +Condition,cumulus__all,0.67,0.47,1 +AllergyIntolerance,cumulus__all,0.00,0.00,0