diff --git a/examples/diff-showcase-report.html b/examples/diff-showcase-report.html index df34e613..56cca0c0 100644 --- a/examples/diff-showcase-report.html +++ b/examples/diff-showcase-report.html @@ -2382,8 +2382,374 @@
fct_clinical_events
fct_encountersfct_clinical_events models/marts/core/fct_clinical_events.sql
2 call sites.
+{{ quarantine_filter() }}{{ quarantine_filter() }}{{ + config( + materialized='table', + unique_key='clinical_event_key' + ) +}} + +with valid_encounters as ( + select encounter_id + from {{ ref('stg_synthea__encounters') }} + {{ quarantine_filter() }} +), + +conditions as ( + select + condition_id as event_id + , 'CONDITION' as event_type + , patient_id + , cond.encounter_id + , condition_start_date as event_start_date + , condition_end_date as event_end_date + , condition_code as code + , 'SNOMED-CT' as code_system + , condition_description as description + , cast(null as varchar) as reason_code + , cast(null as varchar) as reason_description + , cast(null as decimal(18, 2)) as event_cost + from {{ ref('stg_synthea__conditions') }} as cond + where cond.encounter_id in (select encounter_id from valid_encounters) +), + +medications as ( + select + medication_id as event_id + , 'MEDICATION' as event_type + , patient_id + , med.encounter_id + , cast(medication_start_at as date) as event_start_date + , cast(medication_end_at as date) as event_end_date + , medication_code as code + , 'RXNORM' as code_system + , medication_description as description + , reason_code + , reason_description + , total_cost as event_cost + from {{ ref('stg_synthea__medications') }} as med + {{ quarantine_filter() }} + and med.encounter_id in (select encounter_id from valid_encounters) +), + +procedures as ( + select + procedure_id as event_id + , 'PROCEDURE' as event_type + , patient_id + , proc.encounter_id + , cast(procedure_at as date) as event_start_date + , cast(null as date) as event_end_date + , procedure_code as code + , 'SNOMED-CT' as code_system + , procedure_description as description + , reason_code + , reason_description + , base_cost as event_cost + from {{ ref('stg_synthea__procedures') }} as proc + where proc.encounter_id in (select encounter_id from valid_encounters) +), + +all_events as ( + select * from conditions + union all + select * from medications + union all + select * from procedures +), + +patients as ( + select + patient_key + , patient_id + from {{ ref('dim_patients') }} + where is_current = true +), + +dim_date as ( + select + date_key + , date_actual + from {{ ref('dim_date') }} +), + +encounters as ( + select + encounter_key + , encounter_id + from {{ ref('fct_encounters') }} +), + +final as ( + select + -- surrogate key + row_number() over (order by e.event_id) as clinical_event_key + + -- event identifiers + , e.event_type + , e.event_id + + -- dimension keys + , coalesce(p.patient_key, -1) as patient_key + , e.patient_id + , coalesce(enc.encounter_key, -1) as encounter_key + , e.encounter_id + , coalesce(d.date_key, -1) as event_date_key + + -- dates + , e.event_start_date + , e.event_end_date + + -- code information + , e.code + , e.code_system + , e.description + + -- reason + , e.reason_code + , e.reason_description + + -- cost + , e.event_cost + + -- metadata + , current_timestamp as _loaded_at + + from all_events as e + left join patients as p on e.patient_id = p.patient_id + left join encounters as enc on e.encounter_id = enc.encounter_id + left join dim_date as d on e.event_start_date = d.date_actual +) + +select * from final + +-- cute-dbt fixture: synthetic body mod ++
fct_encounters models/marts/core/fct_encounters.sql
1 call site.
+{{ quarantine_filter() }}{{ + config( + materialized='table', + unique_key='encounter_key' + ) +}} + +with encounters as ( + select * from {{ ref('stg_synthea__encounters') }} + {{ quarantine_filter() }} +), + +patients as ( + select + patient_key + , patient_id + , birth_date + from {{ ref('dim_patients') }} + where is_current = true +), + +providers as ( + select + provider_key + , provider_id + from {{ ref('dim_providers') }} +), + +organizations as ( + select + organization_key + , organization_id + from {{ ref('dim_organizations') }} +), + +payers as ( + select + payer_key + , payer_id + from {{ ref('dim_payers') }} +), + +dim_date as ( + select + date_key + , date_actual + from {{ ref('dim_date') }} +), + +final as ( + select + -- surrogate key + row_number() over (order by e.encounter_id) as encounter_key + + -- natural key + , e.encounter_id + + -- dimension keys + , coalesce(p.patient_key, -1) as patient_key + , e.patient_id + , coalesce(pr.provider_key, -1) as provider_key + , e.provider_id + , coalesce(o.organization_key, -1) as organization_key + , e.organization_id + , coalesce(py.payer_key, -1) as payer_key + , e.payer_id + + -- date keys + , coalesce(ds.date_key, -1) as encounter_start_date_key + , coalesce(de.date_key, -1) as encounter_end_date_key + + -- attributes + , e.encounter_class + , e.encounter_code + , e.encounter_description + , e.reason_code + , e.reason_description + + -- timestamps + , e.encounter_start_at + , e.encounter_end_at + + -- derived duration + , date_diff('minute', e.encounter_start_at, e.encounter_end_at) as duration_minutes + + -- costs + , e.base_encounter_cost + , e.total_claim_cost + , e.payer_coverage + , e.total_claim_cost - coalesce(e.payer_coverage, 0) as patient_responsibility + + -- derived patient context + , case + when p.birth_date is not null + then date_diff('year', p.birth_date, cast(e.encounter_start_at as date)) + end as patient_age_at_encounter + + -- metadata + , current_timestamp as _loaded_at + + from encounters as e + left join patients as p on e.patient_id = p.patient_id + left join providers as pr on e.provider_id = pr.provider_id + left join organizations as o on e.organization_id = o.organization_id + left join payers as py on e.payer_id = py.payer_id + left join dim_date as ds on cast(e.encounter_start_at as date) = ds.date_actual + left join dim_date as de on cast(e.encounter_end_at as date) = de.date_actual +) + +select * from final ++
int_encounters__enriched models/intermediate/healthcare/int_encounters__enriched.sql
1 call site.
+{{ quarantine_filter() }}{{ + config( + materialized='view' + ) +}} + +with encounters as ( + select + encounter_id + from {{ ref('fct_encounters') }} +), + +conditions as ( + select + encounter_id + , count(*) as condition_count + , cast(0 as decimal(18, 2)) as total_condition_cost + from {{ ref('stg_synthea__conditions') }} + group by encounter_id +), + +medications as ( + select + encounter_id + , count(*) as medication_count + , coalesce(sum(total_cost), 0) as total_medication_cost + from {{ ref('stg_synthea__medications') }} + {{ quarantine_filter() }} + group by encounter_id +), + +procedures as ( + select + encounter_id + , count(*) as procedure_count + , coalesce(sum(base_cost), 0) as total_procedure_cost + from {{ ref('stg_synthea__procedures') }} + group by encounter_id +), + +final as ( + select + e.encounter_id + , coalesce(c.condition_count, 0) as condition_count + , coalesce(m.medication_count, 0) as medication_count + , coalesce(p.procedure_count, 0) as procedure_count + , coalesce(c.condition_count, 0) + + coalesce(m.medication_count, 0) + + coalesce(p.procedure_count, 0) as total_event_count + , coalesce(c.total_condition_cost, 0) as total_condition_cost + , coalesce(m.total_medication_cost, 0) as total_medication_cost + , coalesce(p.total_procedure_cost, 0) as total_procedure_cost + , coalesce(c.total_condition_cost, 0) + + coalesce(m.total_medication_cost, 0) + + coalesce(p.total_procedure_cost, 0) as total_event_cost + from encounters as e + left join conditions as c on e.encounter_id = c.encounter_id + left join medications as m on e.encounter_id = m.encounter_id + left join procedures as p on e.encounter_id = p.encounter_id +) + +select * from final ++