diff --git a/inst/sql/sql_server/TimeToEvent.sql b/inst/sql/sql_server/TimeToEvent.sql index 1eeac3d..3d437ec 100644 --- a/inst/sql/sql_server/TimeToEvent.sql +++ b/inst/sql/sql_server/TimeToEvent.sql @@ -1,25 +1,25 @@ --need to know indication/target/outcome tuples drop table if exists #targets; -create table #targets as select *, row_number() over (partition by cohort_definition_id, subject_id order by cohort_start_date asc) as era_number +into #targets from @target_database_schema.@target_table where cohort_definition_id in (select distinct target_cohort_definition_id from #cohort_settings) ; drop table if exists #outcomes; -create table #outcomes as select *, row_number() over (partition by cohort_definition_id, subject_id order by cohort_start_date asc) as era_number +into #outcomes from @outcome_database_schema.@outcome_table where cohort_definition_id in (select distinct outcome_cohort_definition_id from #cohort_settings) ; drop table if exists #target_w_outcome; -create table #target_w_outcome as select t1.cohort_definition_id as target_cohort_definition_id, o1.cohort_definition_id as outcome_cohort_definition_id, t1.subject_id, t1.first_start_date as target_first_start_date, t1.last_end_date as target_last_end_date, t1.num_eras as target_num_eras, t1.total_duration as target_total_duration, o1.first_start_date as outcome_first_start_date, o1.last_end_date as outcome_last_end_date, o1.num_eras as outcome_num_eras, o1.total_duration as outcome_total_duration +into #target_w_outcome from ( select cohort_definition_id, subject_id, min(cohort_start_date) as first_start_date, @@ -47,11 +47,11 @@ and o1.cohort_definition_id = ito1.outcome_cohort_definition_id /*follow-up time*/ drop table if exists #two_fu_bounds; -create table #two_fu_bounds as select t1.cohort_definition_id, min(datediff(day,t1.first_date, op1.observation_period_start_date)) as min_time, max(datediff(day,t1.first_date, op1.observation_period_end_date)) as max_time +into #two_fu_bounds from ( select cohort_definition_id, subject_id, min(cohort_start_date) as first_date from #targets @@ -69,12 +69,12 @@ group by drop table if exists #t_prior_obs; -create table #t_prior_obs as select t1.cohort_definition_id, 'Before target start' as observation_time_type, datediff(day, t1.first_date, op1.observation_period_start_date) as time_to_event, count(t1.subject_id) as num_persons +into #t_prior_obs from ( select cohort_definition_id, subject_id, min(cohort_start_date) as first_date from #targets @@ -91,12 +91,12 @@ group by drop table if exists #t_post_obs; -create table #t_post_obs as select t1.cohort_definition_id, 'After target start' as observation_time_type, datediff(day, t1.first_date, op1.observation_period_end_date) as time_to_event, count(t1.subject_id) as num_persons +into #t_post_obs from ( select cohort_definition_id, subject_id, min(cohort_start_date) as first_date from #targets @@ -114,7 +114,9 @@ group by /*time-to-event distribution*/ drop table if exists #two_tte; -create table #two_tte as + +select * into #two_tte from +( select two1.target_cohort_definition_id, two1.outcome_cohort_definition_id, case when o1.era_number = 1 then 'first' else 'subsequent' end as outcome_type, 'Before first target start' as target_outcome_type, @@ -239,12 +241,15 @@ and o1.cohort_start_date <= op1.observation_period_end_date group by two1.target_cohort_definition_id, two1.outcome_cohort_definition_id, case when o1.era_number = 1 then 'first' else 'subsequent' end, datediff(day, two1.target_first_start_date, o1.cohort_start_date) + + ) temp ; drop table if exists #two_tte_summary; -create table #two_tte_summary as -select '@database_id' as database_id, temp.* from +select '@database_id' as database_id, temp.* +into #two_tte_summary +from ( --daily counting for +/- 100 days select target_cohort_definition_id, outcome_cohort_definition_id, outcome_type, target_outcome_type, time_to_event, num_events, 'per 1-day' as time_scale