Skip to content

Commit

Permalink
Fixes for bracket locations
Browse files Browse the repository at this point in the history
  • Loading branch information
azimov committed Jan 10, 2025
1 parent 0a10618 commit 1179b10
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 37 deletions.
83 changes: 55 additions & 28 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 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)
Expand All @@ -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
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(CAST(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) 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
Expand All @@ -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
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(CAST(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)) 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
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(CAST(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) 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
Expand Down

0 comments on commit 1179b10

Please sign in to comment.