Skip to content
Open
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
29 changes: 29 additions & 0 deletions models/reports/sql/sensitive-medication-dispensed-summary.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
select
ep.medication as "{{ translate_label('prescriptionMedication')}}",
ep.medication_code as "{{ translate_label('prescriptionMedicationCode')}}",
sum(ep.quantity) as "{{ translate_label('prescriptionQuantity')}}"
from {{ ref('ds__sensitive_encounter_prescriptions') }} ep
where is_selected_for_discharge = true
and
case
when {{ parameter('facilityId') }} is null then true
else ep.facility_id = {{ parameter('facilityId') }}
end
and
case
when {{ parameter('medicationId') }} is null then true
else ep.medication_id = {{ parameter('medicationId') }}
end
and
case
when {{ parameter('fromDate', default_value='2024-01-01', data_type='timestamp') }} is null then true
else ep.datetime
>= {{ parameter('fromDate', default_value='2024-01-01', data_type='timestamp') }}
end
and
case
when {{ parameter('toDate', default_value='2024-01-31', data_type='timestamp') }} is null then true
else ep.datetime
<= {{ parameter('toDate', default_value='2024-01-31', data_type='timestamp') }}
end
group by ep.medication_id, ep.medication, ep.medication_code
Comment on lines +1 to +29

Choose a reason for hiding this comment

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

high

The current implementation of the WHERE clause is verbose and inefficient. It calls the parameter macro multiple times for each parameter and uses CASE statements for optional filters. This can be improved by:

  1. Storing parameter values in variables: This avoids repeated calls to the parameter macro, which is more efficient during dbt compilation.
  2. Simplifying filter logic: Using (parameter is null or column = parameter) is more concise and readable than a CASE statement, and often helps the database query planner.

I've suggested a refactoring of the entire file to apply these improvements.

{%- set facility_id = parameter('facilityId') -%}
{%- set medication_id = parameter('medicationId') -%}
{%- set from_date = parameter('fromDate', default_value='2024-01-01', data_type='timestamp') -%}
{%- set to_date = parameter('toDate', default_value='2024-01-31', data_type='timestamp') -%}

select 
    ep.medication as "{{ translate_label('prescriptionMedication')}}",
    ep.medication_code as "{{ translate_label('prescriptionMedicationCode')}}",
    sum(ep.quantity) as "{{ translate_label('prescriptionQuantity')}}"
from {{ ref('ds__sensitive_encounter_prescriptions') }} ep
where is_selected_for_discharge = true
    and ({{ facility_id }} is null or ep.facility_id = {{ facility_id }})
    and ({{ medication_id }} is null or ep.medication_id = {{ medication_id }})
    and ({{ from_date }} is null or ep.datetime >= {{ from_date }})
    and ({{ to_date }} is null or ep.datetime <= {{ to_date }})
group by ep.medication_id, ep.medication, ep.medication_code