From 0a1061873629c3b4af621173f4419064069bbb01 Mon Sep 17 00:00:00 2001 From: jgilber2 Date: Thu, 9 Jan 2025 08:26:16 -0800 Subject: [PATCH 1/2] DATEDIFF fixes --- inst/sql/sql_server/CohortRelationship.sql | 24 +++++++++++----------- inst/sql/sql_server/ComputeTimeSeries1.sql | 8 ++++---- inst/sql/sql_server/ComputeTimeSeries2.sql | 4 ++-- inst/sql/sql_server/ComputeTimeSeries3.sql | 4 ++-- inst/sql/sql_server/ComputeTimeSeries4.sql | 8 ++++---- inst/sql/sql_server/ComputeTimeSeries5.sql | 4 ++-- inst/sql/sql_server/ComputeTimeSeries6.sql | 4 ++-- 7 files changed, 28 insertions(+), 28 deletions(-) diff --git a/inst/sql/sql_server/CohortRelationship.sql b/inst/sql/sql_server/CohortRelationship.sql index d47369f06..06ed92152 100644 --- a/inst/sql/sql_server/CohortRelationship.sql +++ b/inst/sql/sql_server/CohortRelationship.sql @@ -115,13 +115,13 @@ SELECT t.cohort_definition_id cohort_id, SUM(( CASE -- comparator cohort start date before target start date (offset) WHEN c.cohort_start_date < DATEADD(day, @start_day_offset, t.cohort_start_date) - THEN datediff(dd, + THEN CAST(DATEDIFF(dd, c.cohort_start_date, CASE --min of comparator end date/target start dates (offset) WHEN c.cohort_end_date < DATEADD(day, @start_day_offset, t.cohort_start_date) THEN c.cohort_end_date ELSE DATEADD(day, @start_day_offset, t.cohort_start_date) - END) + END) AS BIGINT) ELSE 0 END ) + 1) c_days_before_ts, @@ -129,13 +129,13 @@ SELECT t.cohort_definition_id cohort_id, SUM(( CASE -- comparator cohort start date before target end date (offset) WHEN c.cohort_start_date < DATEADD(day, @start_day_offset, t.cohort_end_date) - THEN datediff(dd, + THEN CAST(DATEDIFF(dd, c.cohort_start_date, CASE --min of comparator end date/target end dates (offset) WHEN c.cohort_end_date < DATEADD(day, @start_day_offset, t.cohort_end_date) THEN c.cohort_end_date ELSE DATEADD(day, @start_day_offset, t.cohort_end_date) - END) + END) AS bigint) ELSE 0 END ) + 1) c_days_before_te, @@ -144,7 +144,7 @@ SELECT t.cohort_definition_id cohort_id, CASE -- comparator cohort days within target days (offset) WHEN c.cohort_end_date >= DATEADD(day, @start_day_offset, t.cohort_start_date) AND c.cohort_start_date <= DATEADD(day, @end_day_offset, t.cohort_end_date) - THEN datediff(dd, + THEN CAST(DATEDIFF(dd, CASE --min of comparator start date/target start dates (offset) WHEN c.cohort_start_date < DATEADD(day, @start_day_offset, t.cohort_start_date) THEN DATEADD(day, @start_day_offset, t.cohort_start_date) @@ -154,7 +154,7 @@ SELECT t.cohort_definition_id cohort_id, WHEN c.cohort_end_date > DATEADD(day, @end_day_offset, t.cohort_end_date) THEN DATEADD(day, @end_day_offset, t.cohort_end_date) ELSE c.cohort_end_date - END) + END)) AS BIGINT ELSE 0 END ) + 1) c_days_within_t_days, @@ -162,13 +162,13 @@ SELECT t.cohort_definition_id cohort_id, SUM(( CASE -- comparator cohort end date after target start date (offset) WHEN c.cohort_end_date > DATEADD(day, @start_day_offset, t.cohort_start_date) - THEN datediff(dd, + THEN CAST(DATEDIFF(dd, CASE --max of comparator start date/target start dates (offset) WHEN c.cohort_start_date < DATEADD(day, @start_day_offset, t.cohort_start_date) THEN DATEADD(day, @start_day_offset, t.cohort_start_date) ELSE c.cohort_start_date END, - c.cohort_end_date) + c.cohort_end_date) AS BIGINT) ELSE 0 END ) + 1) c_days_after_ts, @@ -176,20 +176,20 @@ SELECT t.cohort_definition_id cohort_id, SUM(( CASE -- comparator cohort end date after target end date (offset) WHEN c.cohort_end_date > DATEADD(day, @start_day_offset, t.cohort_end_date) - THEN datediff(dd, + THEN CAST(DATEDIFF(dd, CASE --max of comparator start date/target start dates (offset) WHEN c.cohort_start_date < DATEADD(day, @start_day_offset, t.cohort_end_date) THEN DATEADD(day, @start_day_offset, t.cohort_end_date) ELSE c.cohort_start_date END, - c.cohort_end_date) + c.cohort_end_date) AS BIGINT) ELSE 0 END ) + 1) c_days_after_te, -- comparator cohort days after target end date (offset) - SUM(datediff(dd, DATEADD(day, @start_day_offset, t.cohort_start_date), DATEADD(day, @end_day_offset, t.cohort_end_date)) + 1) t_days, + SUM(CAST(DATEDIFF(dd, DATEADD(day, @start_day_offset, t.cohort_start_date), DATEADD(day, @end_day_offset, t.cohort_end_date)) AS BIGINT) + 1) t_days, -- target cohort days (no offset) - SUM(datediff(dd, c.cohort_start_date, c.cohort_end_date) + 1) c_days + SUM(CAST(DATEDIFF(dd, c.cohort_start_date, c.cohort_end_date)) AS BIGINT + 1) c_days -- comparator cohort days (offset) INTO #cohort_rel_output FROM #target_cohort_table t diff --git a/inst/sql/sql_server/ComputeTimeSeries1.sql b/inst/sql/sql_server/ComputeTimeSeries1.sql index f3a0c68f1..7df5939fd 100644 --- a/inst/sql/sql_server/ComputeTimeSeries1.sql +++ b/inst/sql/sql_server/ComputeTimeSeries1.sql @@ -13,7 +13,7 @@ SELECT cohort_definition_id cohort_id, -- records in calendar period COUNT_BIG(DISTINCT subject_id) subjects, -- unique subjects in calendar period - SUM(datediff(dd, CASE + SUM(CAST(DATEDIFF(dd, CASE WHEN cohort_start_date >= period_begin THEN cohort_start_date ELSE period_begin @@ -21,10 +21,10 @@ SELECT cohort_definition_id cohort_id, WHEN cohort_end_date >= period_end THEN period_end ELSE cohort_end_date - END) + 1) person_days, + END) AS BIGINT) + 1) person_days, -- person days within period SUM(CASE WHEN first_occurrence = 'Y' -- incident - THEN datediff(dd, CASE + THEN CAST(DATEDIFF(dd, CASE WHEN cohort_start_date >= period_begin THEN cohort_start_date ELSE period_begin @@ -34,7 +34,7 @@ SELECT cohort_definition_id cohort_id, THEN period_end ELSE cohort_end_date END - ) + 1 + ) AS BIGIINT) + 1 ELSE 0 END) person_days_in, -- person days within period - incident diff --git a/inst/sql/sql_server/ComputeTimeSeries2.sql b/inst/sql/sql_server/ComputeTimeSeries2.sql index 470d05d79..e57ea0c75 100644 --- a/inst/sql/sql_server/ComputeTimeSeries2.sql +++ b/inst/sql/sql_server/ComputeTimeSeries2.sql @@ -12,7 +12,7 @@ SELECT cohort_definition_id cohort_id, {@stratify_by_age_group} ? {FLOOR((YEAR(period_begin) - year_of_birth) / 10) AS age_group,} : {CAST(NULL AS INT) age_group, } COUNT_BIG(DISTINCT CONCAT(cast(subject_id AS VARCHAR(30)), '_', cast(observation_period_start_date AS VARCHAR(30)))) records, -- records in calendar month COUNT_BIG(DISTINCT subject_id) subjects, -- unique subjects - SUM(datediff(dd, CASE + SUM(CAST(DATEDIFF(dd, CASE WHEN observation_period_start_date >= period_begin THEN observation_period_start_date ELSE period_begin @@ -20,7 +20,7 @@ SELECT cohort_definition_id cohort_id, WHEN observation_period_end_date >= period_end THEN period_end ELSE observation_period_end_date - END) + 1) person_days, -- person days within period + END)) AS BIGINT + 1) person_days, -- person days within period 0 person_days_in, -- person days within period - incident COUNT_BIG(CASE WHEN observation_period_start_date >= period_begin diff --git a/inst/sql/sql_server/ComputeTimeSeries3.sql b/inst/sql/sql_server/ComputeTimeSeries3.sql index d054e0ba9..fc53193e2 100644 --- a/inst/sql/sql_server/ComputeTimeSeries3.sql +++ b/inst/sql/sql_server/ComputeTimeSeries3.sql @@ -11,7 +11,7 @@ SELECT -44819062 cohort_id, {@stratify_by_age_group} ? {FLOOR((YEAR(period_begin) - year_of_birth) / 10) AS age_group,} : {CAST(NULL AS INT) age_group, } COUNT_BIG(DISTINCT CONCAT(cast(o.person_id AS VARCHAR(30)), '_', cast(observation_period_start_date AS VARCHAR(30)))) records, -- records in calendar month COUNT_BIG(DISTINCT o.person_id) subjects, -- unique subjects - SUM(datediff(dd, CASE + SUM(CAST(DATEDIFF(dd, CASE WHEN observation_period_start_date >= period_begin THEN observation_period_start_date ELSE period_begin @@ -19,7 +19,7 @@ SELECT -44819062 cohort_id, WHEN observation_period_end_date >= period_end THEN period_end ELSE observation_period_end_date - END) + 1) person_days, + END) AS BIGINT) + 1) person_days, 0 person_days_in, COUNT_BIG(CASE WHEN observation_period_start_date >= period_begin diff --git a/inst/sql/sql_server/ComputeTimeSeries4.sql b/inst/sql/sql_server/ComputeTimeSeries4.sql index 4d56efae6..cc1c0a480 100644 --- a/inst/sql/sql_server/ComputeTimeSeries4.sql +++ b/inst/sql/sql_server/ComputeTimeSeries4.sql @@ -5,7 +5,7 @@ SELECT cohort_definition_id cohort_id, time_id, COUNT_BIG(DISTINCT CONCAT(cast(subject_id AS VARCHAR(30)), '_', cast(cohort_start_date AS VARCHAR(30)))) records, -- records in calendar month COUNT_BIG(DISTINCT subject_id) subjects, -- unique subjects - SUM(datediff(dd, CASE + SUM(CAST(DATEDIFF(dd, CASE WHEN cohort_start_date >= period_begin THEN cohort_start_date ELSE period_begin @@ -13,9 +13,9 @@ SELECT cohort_definition_id cohort_id, WHEN cohort_end_date >= period_end THEN period_end ELSE cohort_end_date - END) + 1) person_days, + END) AS BIGINT) + 1) person_days, SUM(CASE WHEN first_occurrence = 'Y' -- incident - THEN datediff(dd, CASE + THEN CAST(DATEDIFF(dd, CASE WHEN cohort_start_date >= period_begin THEN cohort_start_date ELSE period_begin @@ -25,7 +25,7 @@ SELECT cohort_definition_id cohort_id, THEN period_end ELSE cohort_end_date END - ) + 1 + ) AS BIGINT) + 1 ELSE 0 END) person_days_in, COUNT_BIG(CASE diff --git a/inst/sql/sql_server/ComputeTimeSeries5.sql b/inst/sql/sql_server/ComputeTimeSeries5.sql index e22cc267d..5d177f3a1 100644 --- a/inst/sql/sql_server/ComputeTimeSeries5.sql +++ b/inst/sql/sql_server/ComputeTimeSeries5.sql @@ -5,7 +5,7 @@ SELECT cohort_definition_id cohort_id, time_id, COUNT_BIG(DISTINCT CONCAT(cast(subject_id AS VARCHAR(30)), '_', cast(observation_period_start_date AS VARCHAR(30)))) records, -- records in calendar month COUNT_BIG(DISTINCT subject_id) subjects, -- unique subjects - SUM(datediff(dd, CASE + SUM(CAST(DATEDIFF(dd, CASE WHEN observation_period_start_date >= period_begin THEN observation_period_start_date ELSE period_begin @@ -13,7 +13,7 @@ SELECT cohort_definition_id cohort_id, WHEN observation_period_end_date >= period_end THEN period_end ELSE observation_period_end_date - END) + 1) person_days, + END) AS BIGINT) + 1) person_days, 0 person_days_in, -- person days within period - incident COUNT_BIG(CASE WHEN observation_period_start_date >= period_begin diff --git a/inst/sql/sql_server/ComputeTimeSeries6.sql b/inst/sql/sql_server/ComputeTimeSeries6.sql index 625c398ef..257121287 100644 --- a/inst/sql/sql_server/ComputeTimeSeries6.sql +++ b/inst/sql/sql_server/ComputeTimeSeries6.sql @@ -5,7 +5,7 @@ SELECT -44819062 cohort_id, time_id, COUNT_BIG(DISTINCT CONCAT(cast(person_id AS VARCHAR(30)), '_', cast(observation_period_start_date AS VARCHAR(30)))) records, -- records in calendar month COUNT_BIG(DISTINCT person_id) subjects, -- unique subjects - SUM(datediff(dd, CASE + SUM(CAST(DATEDIFF(dd, CASE WHEN observation_period_start_date >= period_begin THEN observation_period_start_date ELSE period_begin @@ -13,7 +13,7 @@ SELECT -44819062 cohort_id, WHEN observation_period_end_date >= period_end THEN period_end ELSE observation_period_end_date - END) + 1) person_days, + END) AS BIGINT) + 1) person_days, 0 person_days_in, COUNT_BIG(CASE WHEN observation_period_start_date >= period_begin From 1179b1074d04894ec1b194f5f989e24ca6ae0144 Mon Sep 17 00:00:00 2001 From: jgilber2 Date: Fri, 10 Jan 2025 10:00:36 -0800 Subject: [PATCH 2/2] Fixes for bracket locations --- inst/sql/sql_server/CohortRelationship.sql | 83 ++++++++++++++-------- inst/sql/sql_server/ComputeTimeSeries1.sql | 14 ++-- inst/sql/sql_server/ComputeTimeSeries2.sql | 12 +++- inst/sql/sql_server/ComputeTimeSeries3.sql | 9 ++- 4 files changed, 81 insertions(+), 37 deletions(-) diff --git a/inst/sql/sql_server/CohortRelationship.sql b/inst/sql/sql_server/CohortRelationship.sql index 06ed92152..7e4928771 100644 --- a/inst/sql/sql_server/CohortRelationship.sql +++ b/inst/sql/sql_server/CohortRelationship.sql @@ -112,39 +112,46 @@ SELECT t.cohort_definition_id cohort_id, ELSE NULL END) sub_c_within_t, -- comparator cohort days within target (offset) days [How many subjects in comparator cohort have their entire cohort period within first target cohort period] - SUM(( + SUM( CASE -- comparator cohort start date before target start date (offset) WHEN c.cohort_start_date < DATEADD(day, @start_day_offset, t.cohort_start_date) - THEN CAST(DATEDIFF(dd, + THEN CAST( + DATEDIFF(dd, c.cohort_start_date, CASE --min of comparator end date/target start dates (offset) - WHEN c.cohort_end_date < DATEADD(day, @start_day_offset, t.cohort_start_date) - THEN c.cohort_end_date - ELSE DATEADD(day, @start_day_offset, t.cohort_start_date) - END) AS BIGINT) + WHEN c.cohort_end_date < DATEADD(day, @start_day_offset, t.cohort_start_date) + THEN c.cohort_end_date + ELSE DATEADD(day, @start_day_offset, t.cohort_start_date) + END + ) + AS BIGINT + ) ELSE 0 END - ) + 1) c_days_before_ts, + ) + 1 c_days_before_ts, -- comparator cohort days before target start date (offset) - SUM(( + SUM( CASE -- comparator cohort start date before target end date (offset) WHEN c.cohort_start_date < DATEADD(day, @start_day_offset, t.cohort_end_date) - THEN CAST(DATEDIFF(dd, + THEN CAST( + DATEDIFF(dd, c.cohort_start_date, CASE --min of comparator end date/target end dates (offset) - WHEN c.cohort_end_date < DATEADD(day, @start_day_offset, t.cohort_end_date) - THEN c.cohort_end_date - ELSE DATEADD(day, @start_day_offset, t.cohort_end_date) - END) AS bigint) + WHEN c.cohort_end_date < DATEADD(day, @start_day_offset, t.cohort_end_date) + THEN c.cohort_end_date + ELSE DATEADD(day, @start_day_offset, t.cohort_end_date) + END) AS BIGINT + ) ELSE 0 END - ) + 1) c_days_before_te, + ) + 1 as c_days_before_te, -- comparator cohort days before target end date (offset) - SUM(( + SUM( CASE -- comparator cohort days within target days (offset) WHEN c.cohort_end_date >= DATEADD(day, @start_day_offset, t.cohort_start_date) AND c.cohort_start_date <= DATEADD(day, @end_day_offset, t.cohort_end_date) - THEN CAST(DATEDIFF(dd, + THEN CAST( + DATEDIFF(dd, CASE --min of comparator start date/target start dates (offset) WHEN c.cohort_start_date < DATEADD(day, @start_day_offset, t.cohort_start_date) THEN DATEADD(day, @start_day_offset, t.cohort_start_date) @@ -154,42 +161,62 @@ SELECT t.cohort_definition_id cohort_id, WHEN c.cohort_end_date > DATEADD(day, @end_day_offset, t.cohort_end_date) THEN DATEADD(day, @end_day_offset, t.cohort_end_date) ELSE c.cohort_end_date - END)) AS BIGINT + END) + AS BIGINT + ) ELSE 0 END - ) + 1) c_days_within_t_days, + ) + 1 as c_days_within_t_days, -- comparator cohort days within target cohort days (offset) - SUM(( + SUM( CASE -- comparator cohort end date after target start date (offset) WHEN c.cohort_end_date > DATEADD(day, @start_day_offset, t.cohort_start_date) - THEN CAST(DATEDIFF(dd, + THEN CAST( + DATEDIFF(dd, CASE --max of comparator start date/target start dates (offset) WHEN c.cohort_start_date < DATEADD(day, @start_day_offset, t.cohort_start_date) THEN DATEADD(day, @start_day_offset, t.cohort_start_date) ELSE c.cohort_start_date END, - c.cohort_end_date) AS BIGINT) + c.cohort_end_date + ) AS BIGINT + ) ELSE 0 END - ) + 1) c_days_after_ts, + ) + 1 as c_days_after_ts, -- comparator cohort days after target start date (offset) - SUM(( + SUM( CASE -- comparator cohort end date after target end date (offset) WHEN c.cohort_end_date > DATEADD(day, @start_day_offset, t.cohort_end_date) - THEN CAST(DATEDIFF(dd, + THEN CAST( + DATEDIFF(dd, CASE --max of comparator start date/target start dates (offset) WHEN c.cohort_start_date < DATEADD(day, @start_day_offset, t.cohort_end_date) THEN DATEADD(day, @start_day_offset, t.cohort_end_date) ELSE c.cohort_start_date END, - c.cohort_end_date) AS BIGINT) + c.cohort_end_date) + AS BIGINT + ) ELSE 0 END - ) + 1) c_days_after_te, + ) + 1 as c_days_after_te, -- comparator cohort days after target end date (offset) - SUM(CAST(DATEDIFF(dd, DATEADD(day, @start_day_offset, t.cohort_start_date), DATEADD(day, @end_day_offset, t.cohort_end_date)) AS BIGINT) + 1) t_days, + SUM( + CAST( + DATEDIFF( + dd, DATEADD(day, @start_day_offset, t.cohort_start_date), DATEADD(day, @end_day_offset, t.cohort_end_date) + ) + AS BIGINT + ) + ) + 1 as t_days, -- target cohort days (no offset) - SUM(CAST(DATEDIFF(dd, c.cohort_start_date, c.cohort_end_date)) AS BIGINT + 1) c_days + SUM( + CAST( + DATEDIFF(dd, c.cohort_start_date, c.cohort_end_date) + AS BIGINT + ) + ) + 1 as c_days -- comparator cohort days (offset) INTO #cohort_rel_output FROM #target_cohort_table t diff --git a/inst/sql/sql_server/ComputeTimeSeries1.sql b/inst/sql/sql_server/ComputeTimeSeries1.sql index 7df5939fd..041d1f5cb 100644 --- a/inst/sql/sql_server/ComputeTimeSeries1.sql +++ b/inst/sql/sql_server/ComputeTimeSeries1.sql @@ -13,7 +13,9 @@ SELECT cohort_definition_id cohort_id, -- records in calendar period COUNT_BIG(DISTINCT subject_id) subjects, -- unique subjects in calendar period - SUM(CAST(DATEDIFF(dd, CASE + SUM( + CAST( + DATEDIFF(dd, CASE WHEN cohort_start_date >= period_begin THEN cohort_start_date ELSE period_begin @@ -21,10 +23,14 @@ SELECT cohort_definition_id cohort_id, WHEN cohort_end_date >= period_end THEN period_end ELSE cohort_end_date - END) AS BIGINT) + 1) person_days, + END) + AS BIGINT + ) + ) + 1 person_days, -- person days within period SUM(CASE WHEN first_occurrence = 'Y' -- incident - THEN CAST(DATEDIFF(dd, CASE + THEN CAST( + DATEDIFF(dd, CASE WHEN cohort_start_date >= period_begin THEN cohort_start_date ELSE period_begin @@ -34,7 +40,7 @@ SELECT cohort_definition_id cohort_id, THEN period_end ELSE cohort_end_date END - ) AS BIGIINT) + 1 + ) AS BIGINT) + 1 ELSE 0 END) person_days_in, -- person days within period - incident diff --git a/inst/sql/sql_server/ComputeTimeSeries2.sql b/inst/sql/sql_server/ComputeTimeSeries2.sql index e57ea0c75..8d3030b56 100644 --- a/inst/sql/sql_server/ComputeTimeSeries2.sql +++ b/inst/sql/sql_server/ComputeTimeSeries2.sql @@ -6,13 +6,16 @@ {DEFAULT @stratify_by_gender = FALSE} {DEFAULT @stratify_by_age_group = FALSE} -SELECT cohort_definition_id cohort_id, +SELECT cohort_definition_id as cohort_id, time_id, {@stratify_by_gender} ? {CASE WHEN gender IS NULL THEN 'NULL' ELSE gender END} : {'NULL'} gender, {@stratify_by_age_group} ? {FLOOR((YEAR(period_begin) - year_of_birth) / 10) AS age_group,} : {CAST(NULL AS INT) age_group, } COUNT_BIG(DISTINCT CONCAT(cast(subject_id AS VARCHAR(30)), '_', cast(observation_period_start_date AS VARCHAR(30)))) records, -- records in calendar month COUNT_BIG(DISTINCT subject_id) subjects, -- unique subjects - SUM(CAST(DATEDIFF(dd, CASE + SUM( + CAST( + DATEDIFF( + dd, CASE WHEN observation_period_start_date >= period_begin THEN observation_period_start_date ELSE period_begin @@ -20,7 +23,10 @@ SELECT cohort_definition_id cohort_id, WHEN observation_period_end_date >= period_end THEN period_end ELSE observation_period_end_date - END)) AS BIGINT + 1) person_days, -- person days within period + END + ) + AS BIGINT) + ) + 1 person_days, -- person days within period 0 person_days_in, -- person days within period - incident COUNT_BIG(CASE WHEN observation_period_start_date >= period_begin diff --git a/inst/sql/sql_server/ComputeTimeSeries3.sql b/inst/sql/sql_server/ComputeTimeSeries3.sql index fc53193e2..d21bc1d22 100644 --- a/inst/sql/sql_server/ComputeTimeSeries3.sql +++ b/inst/sql/sql_server/ComputeTimeSeries3.sql @@ -11,7 +11,9 @@ SELECT -44819062 cohort_id, {@stratify_by_age_group} ? {FLOOR((YEAR(period_begin) - year_of_birth) / 10) AS age_group,} : {CAST(NULL AS INT) age_group, } COUNT_BIG(DISTINCT CONCAT(cast(o.person_id AS VARCHAR(30)), '_', cast(observation_period_start_date AS VARCHAR(30)))) records, -- records in calendar month COUNT_BIG(DISTINCT o.person_id) subjects, -- unique subjects - SUM(CAST(DATEDIFF(dd, CASE + SUM( + CAST( + DATEDIFF(dd, CASE WHEN observation_period_start_date >= period_begin THEN observation_period_start_date ELSE period_begin @@ -19,7 +21,10 @@ SELECT -44819062 cohort_id, WHEN observation_period_end_date >= period_end THEN period_end ELSE observation_period_end_date - END) AS BIGINT) + 1) person_days, + END) + AS BIGINT + ) + ) + 1 person_days, 0 person_days_in, COUNT_BIG(CASE WHEN observation_period_start_date >= period_begin