Skip to content
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

feat(c_resources_per_pt): add std_dev column and rename special values #46

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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 %}
Expand Down
36 changes: 18 additions & 18 deletions tests/data/c_resources_per_pt/general/expected_summary.csv
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested all these test values in an online standard deviation calculator and they all came out the same 🤷

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