From 5c2cf102b895248d11ccdbcc042933584e3c42df Mon Sep 17 00:00:00 2001 From: Lawrence Adams Date: Sun, 6 Oct 2024 16:52:32 +0100 Subject: [PATCH 1/3] refactor: use datediff macro over extracting `day` from timedelta - various sql engines do not allow interval extraction from delta (as deltas are passed as numeric types); concretely: snowflake, likely others - use the dbt standard datediff macro to open up possibility to other sql engines - arguably more idiomatic - bring in line with other recent changes by @katy-sadowski --- models/omop/drug_era.sql | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/models/omop/drug_era.sql b/models/omop/drug_era.sql index a6dc0e8..d60f3df 100644 --- a/models/omop/drug_era.sql +++ b/models/omop/drug_era.sql @@ -122,9 +122,7 @@ WITH ctePreDrugTarget AS ( , drug_sub_exposure_start_date , drug_sub_exposure_end_date , drug_exposure_count - , EXTRACT( - DAY FROM drug_sub_exposure_end_date - drug_sub_exposure_start_date - ) AS days_exposed + , {{ dbt.datediff("drug_sub_exposure_start_date", "drug_sub_exposure_end_date", "day") }} AS days_exposed FROM cteSubExposures ) From 1cc37f0e9f51b788050849aba79ae1557bdd6d6c Mon Sep 17 00:00:00 2001 From: Lawrence Adams Date: Sun, 6 Oct 2024 19:43:31 +0100 Subject: [PATCH 2/3] refactor: use dbt native dateadd macro --- models/omop/drug_era.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/omop/drug_era.sql b/models/omop/drug_era.sql index d60f3df..ac8270a 100644 --- a/models/omop/drug_era.sql +++ b/models/omop/drug_era.sql @@ -12,10 +12,10 @@ WITH ctePreDrugTarget AS ( , COALESCE( NULLIF(drug_exposure_end_date, NULL) , NULLIF( - drug_exposure_start_date + days_supply * INTERVAL '1 day' + {{ dateadd("day", "days_supply", "drug_exposure_start_date") }} , drug_exposure_start_date ) - , drug_exposure_start_date + INTERVAL '1 day' + , {{ dateadd("day", 1, "drug_exposure_start_date") }} ) AS drug_exposure_end_date FROM {{ ref ('drug_exposure') }} AS d INNER JOIN {{ ref ('stg_vocabulary__concept_ancestor') }} AS ca From 2536f2829f182c875c8fd6de07cb567b45765509 Mon Sep 17 00:00:00 2001 From: Lawrence Adams Date: Sun, 6 Oct 2024 19:46:21 +0100 Subject: [PATCH 3/3] chore: use dbt class/dict for consistency - probably not necessary but keep uniform --- models/omop/drug_era.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/omop/drug_era.sql b/models/omop/drug_era.sql index ac8270a..b8c9399 100644 --- a/models/omop/drug_era.sql +++ b/models/omop/drug_era.sql @@ -12,10 +12,10 @@ WITH ctePreDrugTarget AS ( , COALESCE( NULLIF(drug_exposure_end_date, NULL) , NULLIF( - {{ dateadd("day", "days_supply", "drug_exposure_start_date") }} + {{ dbt.dateadd("day", "days_supply", "drug_exposure_start_date") }} , drug_exposure_start_date ) - , {{ dateadd("day", 1, "drug_exposure_start_date") }} + , {{ dbt.dateadd("day", 1, "drug_exposure_start_date") }} ) AS drug_exposure_end_date FROM {{ ref ('drug_exposure') }} AS d INNER JOIN {{ ref ('stg_vocabulary__concept_ancestor') }} AS ca