Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date diff hot fix #1151

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 53 additions & 26 deletions inst/sql/sql_server/CohortRelationship.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 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)
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 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)
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 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 @@ -155,41 +162,61 @@ SELECT t.cohort_definition_id cohort_id,
THEN DATEADD(day, @end_day_offset, t.cohort_end_date)
ELSE c.cohort_end_date
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 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,
) + 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 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
)
ELSE 0
END
) + 1) c_days_after_te,
) + 1 as 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 as 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 as c_days
-- comparator cohort days (offset)
INTO #cohort_rel_output
FROM #target_cohort_table t
Expand Down
14 changes: 10 additions & 4 deletions inst/sql/sql_server/ComputeTimeSeries1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@ 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 +40,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,
-- person days within period - incident
Expand Down
12 changes: 9 additions & 3 deletions inst/sql/sql_server/ComputeTimeSeries2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@
{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(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
9 changes: 7 additions & 2 deletions inst/sql/sql_server/ComputeTimeSeries3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ 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
Loading