Skip to content

Commit

Permalink
move casts to stg models where applicable, use dbt.cast
Browse files Browse the repository at this point in the history
  • Loading branch information
Katy Sadowski committed Oct 5, 2024
1 parent b675383 commit ae79114
Show file tree
Hide file tree
Showing 45 changed files with 282 additions and 284 deletions.
18 changes: 9 additions & 9 deletions models/intermediate/int__assign_all_visit_ids.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
SELECT
e.encounter_id
, e.patient_id AS person_source_value
, e.encounter_start_datetime AS date_service
, e.encounter_stop_datetime AS date_service_end
, e.encounter_start_datetime AS datetime_service
, e.encounter_stop_datetime AS datetime_service_end
, e.encounter_class
, av.encounter_class AS visit_type
, av.visit_start_date
, av.visit_end_date
, av.visit_start_datetime
, av.visit_end_datetime
, av.visit_occurrence_id
, CASE
WHEN
Expand All @@ -19,11 +19,11 @@ SELECT
CASE
WHEN
av.encounter_class = 'inpatient'
AND e.encounter_start_datetime > av.visit_start_date
AND e.encounter_start_datetime > av.visit_start_datetime
THEN visit_occurrence_id
WHEN
av.encounter_class IN ('emergency', 'urgent')
AND e.encounter_start_datetime = av.visit_start_date
AND e.encounter_start_datetime = av.visit_start_datetime
THEN visit_occurrence_id
END
)
Expand All @@ -32,7 +32,7 @@ SELECT
CASE
WHEN
av.encounter_class = 'inpatient'
AND e.encounter_start_datetime >= av.visit_start_date
AND e.encounter_start_datetime >= av.visit_start_datetime
THEN visit_occurrence_id
WHEN
av.encounter_class IN (
Expand All @@ -46,5 +46,5 @@ FROM {{ ref('stg_synthea__encounters') }} AS e
INNER JOIN {{ ref('int__all_visits') }} AS av
ON
e.patient_id = av.patient_id
AND e.encounter_start_datetime >= av.visit_start_date
AND e.encounter_start_datetime <= av.visit_end_date
AND e.encounter_start_datetime >= av.visit_start_datetime
AND e.encounter_start_datetime <= av.visit_end_datetime
18 changes: 9 additions & 9 deletions models/intermediate/int__cost_condition.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ WITH cte AS (
co.condition_occurrence_id
, ppp.payer_plan_period_id
, coalesce(
sum(CASE WHEN ct.transfer_type = '1' THEN {{ dbt.cast("ct.transaction_amount", api.Column.translate_type("decimal")) }} END)
sum(CASE WHEN ct.transfer_type = '1' THEN ct.transaction_amount END)
, 0
)
AS payer_paid
, coalesce(
sum(CASE WHEN ct.transfer_type = 'p' THEN {{ dbt.cast("ct.transaction_amount", api.Column.translate_type("decimal")) }} END)
sum(CASE WHEN ct.transfer_type = 'p' THEN ct.transaction_amount END)
, 0
)
AS patient_paid
Expand Down Expand Up @@ -41,7 +41,7 @@ WITH cte AS (
AND e.encounter_id = ca.encounter_id
AND e.provider_id = ca.provider_id
AND e.payer_id = ca.primary_patient_insurance_id
AND e.encounter_start_datetime = ca.service_date
AND e.encounter_start_datetime = ca.service_datetime
INNER JOIN {{ ref ('stg_synthea__claims_transactions') }} AS ct
ON
ca.claim_id = ct.claim_id
Expand All @@ -62,14 +62,14 @@ SELECT
, {{ dbt.cast("payer_paid ", api.Column.translate_type("decimal")) }} + {{ dbt.cast("patient_paid", api.Column.translate_type("decimal")) }} AS total_paid
, payer_paid AS paid_by_payer
, patient_paid AS paid_by_patient
, cast(null AS numeric) AS paid_patient_copay
, cast(null AS numeric) AS paid_patient_coinsurance
, cast(null AS numeric) AS paid_patient_deductible
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_patient_copay
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_patient_coinsurance
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_patient_deductible
, payer_paid AS paid_by_primary
, cast(null AS numeric) AS paid_ingredient_cost
, cast(null AS numeric) AS paid_dispensing_fee
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_ingredient_cost
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_dispensing_fee
, payer_plan_period_id
, cast(null AS numeric) AS amount_allowed
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS amount_allowed
, 0 AS revenue_code_concept_id
, 'unknown / unknown' AS revenue_code_source_value
, 0 AS drg_concept_id
Expand Down
24 changes: 11 additions & 13 deletions models/intermediate/int__cost_drug_exposure_1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ SELECT DISTINCT
, 'Drug' AS cost_domain_id
, 32814 AS cost_type_concept_id
, 44818668 AS currency_concept_id
, {{ dbt.cast("e.total_encounter_cost", api.Column.translate_type("decimal")) }} + {{ dbt.cast("i.immunization_base_cost", api.Column.translate_type("decimal")) }} AS total_charge
, {{ dbt.cast("e.total_encounter_cost", api.Column.translate_type("decimal")) }} + {{ dbt.cast("i.immunization_base_cost", api.Column.translate_type("decimal")) }} AS total_cost
, {{ dbt.cast("e.encounter_payer_coverage", api.Column.translate_type("decimal")) }} + {{ dbt.cast("i.immunization_base_cost", api.Column.translate_type("decimal")) }} AS total_paid
, e.total_encounter_cost + i.immunization_base_cost AS total_charge
, e.total_encounter_cost + i.immunization_base_cost AS total_cost
, e.encounter_payer_coverage + i.immunization_base_cost AS total_paid
, e.encounter_payer_coverage AS paid_by_payer
, {{ dbt.cast("e.total_encounter_cost", api.Column.translate_type("decimal")) }}
+ {{ dbt.cast("i.immunization_base_cost", api.Column.translate_type("decimal")) }}
- {{ dbt.cast("e.encounter_payer_coverage", api.Column.translate_type("decimal")) }} AS paid_by_patient
, cast(null AS numeric) AS paid_patient_copay
, cast(null AS numeric) AS paid_patient_coinsurance
, cast(null AS numeric) AS paid_patient_deductible
, cast(null AS numeric) AS paid_by_primary
, cast(null AS numeric) AS paid_ingredient_cost
, cast(null AS numeric) AS paid_dispensing_fee
, e.total_encounter_cost + i.immunization_base_cost - e.encounter_payer_coverage AS paid_by_patient
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_patient_copay
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_patient_coinsurance
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_patient_deductible
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_by_primary
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_ingredient_cost
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_dispensing_fee
, ppp.payer_plan_period_id
, cast(null AS numeric) AS amount_allowed
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS amount_allowed
, 0 AS revenue_code_concept_id
, 'UNKNOWN / UNKNOWN' AS revenue_code_source_value
, 0 AS drg_concept_id
Expand Down
24 changes: 11 additions & 13 deletions models/intermediate/int__cost_drug_exposure_2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ SELECT DISTINCT
, 'Drug' AS cost_domain_id
, 32814 AS cost_type_concept_id
, 44818668 AS currency_concept_id
, {{ dbt.cast("e.total_encounter_cost", api.Column.translate_type("decimal")) }} + {{ dbt.cast("m.medication_base_cost", api.Column.translate_type("decimal")) }} AS total_charge
, {{ dbt.cast("e.total_encounter_cost", api.Column.translate_type("decimal")) }} + {{ dbt.cast("m.medication_base_cost", api.Column.translate_type("decimal")) }} AS total_cost
, {{ dbt.cast("e.encounter_payer_coverage", api.Column.translate_type("decimal")) }} + {{ dbt.cast("m.medication_base_cost", api.Column.translate_type("decimal")) }} AS total_paid
, e.total_encounter_cost + m.medication_base_cost AS total_charge
, e.total_encounter_cost + m.medication_base_cost AS total_cost
, e.encounter_payer_coverage + m.medication_base_cost AS total_paid
, e.encounter_payer_coverage AS paid_by_payer
, {{ dbt.cast("e.total_encounter_cost", api.Column.translate_type("decimal")) }}
+ {{ dbt.cast("m.medication_base_cost", api.Column.translate_type("decimal")) }}
- {{ dbt.cast("e.encounter_payer_coverage", api.Column.translate_type("decimal")) }} AS paid_by_patient
, cast(null AS numeric) AS paid_patient_copay
, cast(null AS numeric) AS paid_patient_coinsurance
, cast(null AS numeric) AS paid_patient_deductible
, cast(null AS numeric) AS paid_by_primary
, cast(null AS numeric) AS paid_ingredient_cost
, cast(null AS numeric) AS paid_dispensing_fee
, e.total_encounter_cost + m.medication_base_cost - e.encounter_payer_coverage AS paid_by_patient
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_patient_copay
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_patient_coinsurance
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_patient_deductible
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_by_primary
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_ingredient_cost
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_dispensing_fee
, ppp.payer_plan_period_id
, cast(null AS numeric) AS amount_allowed
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS amount_allowed
, 0 AS revenue_code_concept_id
, 'UNKNOWN / UNKNOWN' AS revenue_code_source_value
, 0 AS drg_concept_id
Expand Down
24 changes: 11 additions & 13 deletions models/intermediate/int__cost_procedure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ SELECT DISTINCT
, 'Procedure' AS cost_domain_id
, 32814 AS cost_type_concept_id
, 44818668 AS currency_concept_id
, {{ dbt.cast("e.total_encounter_cost", api.Column.translate_type("decimal")) }} + {{ dbt.cast("pr.procedure_base_cost", api.Column.translate_type("decimal")) }} AS total_charge
, {{ dbt.cast("e.total_encounter_cost", api.Column.translate_type("decimal")) }} + {{ dbt.cast("pr.procedure_base_cost", api.Column.translate_type("decimal")) }} AS total_cost
, {{ dbt.cast("e.encounter_payer_coverage", api.Column.translate_type("decimal")) }} + {{ dbt.cast("pr.procedure_base_cost", api.Column.translate_type("decimal")) }} AS total_paid
, e.total_encounter_cost + pr.procedure_base_cost AS total_charge
, e.total_encounter_cost + pr.procedure_base_cost AS total_cost
, e.total_encounter_cost + pr.procedure_base_cost AS total_paid
, e.encounter_payer_coverage AS paid_by_payer
, {{ dbt.cast("e.total_encounter_cost", api.Column.translate_type("decimal")) }}
+ {{ dbt.cast("pr.procedure_base_cost", api.Column.translate_type("decimal")) }}
- {{ dbt.cast("e.encounter_payer_coverage", api.Column.translate_type("decimal")) }} AS paid_by_patient
, cast(null AS numeric) AS paid_patient_copay
, cast(null AS numeric) AS paid_patient_coinsurance
, cast(null AS numeric) AS paid_patient_deductible
, cast(null AS numeric) AS paid_by_primary
, cast(null AS numeric) AS paid_ingredient_cost
, cast(null AS numeric) AS paid_dispensing_fee
, e.total_encounter_cost + pr.procedure_base_cost - e.encounter_payer_coverage AS paid_by_patient
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_patient_copay
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_patient_coinsurance
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_patient_deductible
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_by_primary
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_ingredient_cost
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS paid_dispensing_fee
, ppp.payer_plan_period_id
, cast(null AS numeric) AS amount_allowed
, {{ dbt.cast("null", api.Column.translate_type("numeric")) }} AS amount_allowed
, 0 AS revenue_code_concept_id
, 'UNKNOWN / UNKNOWN' AS revenue_code_source_value
, 0 AS drg_concept_id
Expand Down
20 changes: 10 additions & 10 deletions models/intermediate/int__drug_immunisations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ SELECT
i.patient_id
, i.encounter_id
, srctostdvm.target_concept_id AS drug_concept_id
, i.immunization_date AS drug_exposure_start_date
, {{ dbt.cast("i.immunization_date", api.Column.translate_type("date")) }} AS drug_exposure_start_date
, i.immunization_date AS drug_exposure_start_datetime
, i.immunization_date AS drug_exposure_end_date
, {{ dbt.cast("i.immunization_date", api.Column.translate_type("date")) }} AS drug_exposure_end_date
, i.immunization_date AS drug_exposure_end_datetime
, i.immunization_date AS verbatim_end_date
, {{ dbt.cast("i.immunization_date", api.Column.translate_type("date")) }} AS verbatim_end_date
, 32827 AS drug_type_concept_id
, cast(null AS varchar) AS stop_reason
, cast(null AS integer) AS refills
, cast(null AS integer) AS quantity
, cast(null AS integer) AS days_supply
, cast(null AS varchar) AS sig
, {{ dbt.cast("null", api.Column.translate_type("varchar")) }} AS stop_reason
, {{ dbt.cast("null", api.Column.translate_type("integer")) }} AS refills
, {{ dbt.cast("null", api.Column.translate_type("integer")) }} AS quantity
, {{ dbt.cast("null", api.Column.translate_type("integer")) }} AS days_supply
, {{ dbt.cast("null", api.Column.translate_type("varchar")) }} AS sig
, 0 AS route_concept_id
, '0' AS lot_number
, i.immunization_code AS drug_source_value
, srctosrcvm.source_concept_id AS drug_source_concept_id
, cast(null AS varchar) AS route_source_value
, cast(null AS varchar) AS dose_unit_source_value
, {{ dbt.cast("null", api.Column.translate_type("varchar")) }} AS route_source_value
, {{ dbt.cast("null", api.Column.translate_type("varchar")) }} AS dose_unit_source_value
FROM {{ ref ('stg_synthea__immunizations') }} AS i
INNER JOIN {{ ref ('int__source_to_standard_vocab_map') }} AS srctostdvm
ON
Expand Down
30 changes: 13 additions & 17 deletions models/intermediate/int__drug_medications.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,27 @@ SELECT
m.patient_id
, m.encounter_id
, srctostdvm.target_concept_id AS drug_concept_id
, m.medication_start_datetime AS drug_exposure_start_date
, m.medication_start_date AS drug_exposure_start_date
, m.medication_start_datetime AS drug_exposure_start_datetime
, coalesce(
m.medication_stop_datetime, m.medication_start_datetime
) AS drug_exposure_end_date
, coalesce(
m.medication_stop_datetime, m.medication_start_datetime
) AS drug_exposure_end_datetime
, m.medication_stop_datetime AS verbatim_end_date
, coalesce(m.medication_stop_date, m.medication_start_date) AS drug_exposure_end_date
, coalesce(m.medication_stop_datetime, m.medication_start_datetime) AS drug_exposure_end_datetime
, m.medication_stop_date AS verbatim_end_date
, 32838 AS drug_type_concept_id
, cast(null AS varchar) AS stop_reason
, cast(null AS integer) AS refills
, cast(null AS integer) AS quantity
, {{ dbt.cast("null", api.Column.translate_type("varchar")) }} AS stop_reason
, {{ dbt.cast("null", api.Column.translate_type("integer")) }} AS refills
, {{ dbt.cast("null", api.Column.translate_type("integer")) }} AS quantity
, {{ dbt.datediff(
dbt.cast("m.medication_start_datetime", api.Column.translate_type("date")),
dbt.cast("m.medication_stop_datetime", api.Column.translate_type("date")),
"day")
"m.medication_start_date",
"m.medication_stop_date",
"day")
}} AS days_supply
, cast(null AS varchar) AS sig
, {{ dbt.cast("null", api.Column.translate_type("varchar")) }} AS sig
, 0 AS route_concept_id
, '0' AS lot_number
, m.medication_code AS drug_source_value
, srctosrcvm.source_concept_id AS drug_source_concept_id
, cast(null AS varchar) AS route_source_value
, cast(null AS varchar) AS dose_unit_source_value
, {{ dbt.cast("null", api.Column.translate_type("varchar")) }} AS route_source_value
, {{ dbt.cast("null", api.Column.translate_type("varchar")) }} AS dose_unit_source_value
FROM {{ ref ('stg_synthea__medications') }} AS m
INNER JOIN {{ ref ('int__source_to_standard_vocab_map') }} AS srctostdvm
ON
Expand Down
14 changes: 7 additions & 7 deletions models/intermediate/int__er_visits.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ SELECT
t2.encounter_id
, t2.patient_id
, t2.encounter_class
, t2.visit_start_date
, t2.visit_end_date
, t2.visit_start_datetime
, t2.visit_end_datetime
FROM (
SELECT
min(encounter_id) AS encounter_id
, patient_id
, encounter_class
, visit_start_date
, max(visit_end_date) AS visit_end_date
, visit_start_datetime
, max(visit_end_datetime) AS visit_end_datetime
FROM (
SELECT
cl1.encounter_id
, cl1.patient_id
, cl1.encounter_class
, cl1.encounter_start_datetime AS visit_start_date
, cl2.encounter_stop_datetime AS visit_end_date
, cl1.encounter_start_datetime AS visit_start_datetime
, cl2.encounter_stop_datetime AS visit_end_datetime
FROM {{ ref( 'stg_synthea__encounters') }} AS cl1
INNER JOIN {{ ref( 'stg_synthea__encounters') }} AS cl2
ON
Expand All @@ -29,5 +29,5 @@ FROM (
AND cl1.encounter_class = cl2.encounter_class
WHERE cl1.encounter_class IN ('emergency', 'urgent')
) AS t1
GROUP BY patient_id, encounter_class, visit_start_date
GROUP BY patient_id, encounter_class, visit_start_datetime
) AS t2
Loading

0 comments on commit ae79114

Please sign in to comment.