Skip to content

Commit

Permalink
DATEDIFF fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
azimov committed Jan 9, 2025
1 parent 6e45def commit 0a10618
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 28 deletions.
24 changes: 12 additions & 12 deletions inst/sql/sql_server/CohortRelationship.sql
Original file line number Diff line number Diff line change
Expand Up @@ -115,27 +115,27 @@ 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,
-- comparator cohort days before target start date (offset)
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,
Expand All @@ -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)
Expand All @@ -154,42 +154,42 @@ 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,
-- comparator cohort days within target cohort days (offset)
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,
-- comparator cohort days after target start date (offset)
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
Expand Down
8 changes: 4 additions & 4 deletions inst/sql/sql_server/ComputeTimeSeries1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ 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
END, CASE
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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions inst/sql/sql_server/ComputeTimeSeries2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ 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
END, CASE
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
Expand Down
4 changes: 2 additions & 2 deletions inst/sql/sql_server/ComputeTimeSeries3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ 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
END, CASE
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
Expand Down
8 changes: 4 additions & 4 deletions inst/sql/sql_server/ComputeTimeSeries4.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ 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
END, CASE
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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions inst/sql/sql_server/ComputeTimeSeries5.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ 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
END, CASE
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
Expand Down
4 changes: 2 additions & 2 deletions inst/sql/sql_server/ComputeTimeSeries6.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ 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
END, CASE
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
Expand Down

0 comments on commit 0a10618

Please sign in to comment.