text: CREATE TABLE #Codesets (
    codeset_id int NOT NULL,
    concept_id bigint NOT NULL
  )
  ;
  
  INSERT INTO #Codesets (codeset_id, concept_id)
  SELECT 3 as codeset_id, c.concept_id FROM (select distinct I.concept_id FROM
  ( 
    select concept_id from @cdmDatabaseSchema.CONCEPT where concept_id in (319844,312327,434376,438170,444406,316427,438168,321318,438172,435561,315286,317576,4127089,4108215,4110961,4108678,314666,4119953,319038,315296,4108679,4108219,4108220,4124683,4108217,4108677,4108218,4108680)and invalid_reason is null
  
  ) I
  ) C;
  INSERT INTO #Codesets (codeset_id, concept_id)
  SELECT 4 as codeset_id, c.concept_id FROM (select distinct I.concept_id FROM
  ( 
    select concept_id from @cdmDatabaseSchema.CONCEPT where concept_id in (312327,434376,438170,444406)and invalid_reason is null
  
  ) I
  ) C;
  INSERT INTO #Codesets (codeset_id, concept_id)
  SELECT 6 as codeset_id, c.concept_id FROM (select distinct I.concept_id FROM
  ( 
    select concept_id from @cdmDatabaseSchema.CONCEPT where concept_id in (4319328,4326561,4218781,376713,4110189,443454,4111714,4108356,4110190,4110192,381316,4176892,4110185,436430,4111709,4226021,432923,4108952,4111708,4049659)and invalid_reason is null
  
  ) I
  ) C;
  INSERT INTO #Codesets (codeset_id, concept_id)
  SELECT 7 as codeset_id, c.concept_id FROM (select distinct I.concept_id FROM
  ( 
    select concept_id from @cdmDatabaseSchema.CONCEPT where concept_id in (4110189,443454,4111714,4108356,4110190,4110192)and invalid_reason is null
  
  ) I
  ) C;
  INSERT INTO #Codesets (codeset_id, concept_id)
  SELECT 8 as codeset_id, c.concept_id FROM (select distinct I.concept_id FROM
  ( 
    select concept_id from @cdmDatabaseSchema.CONCEPT where concept_id in (4319328,4326561,4218781,376713,4176892,4110185,436430,4111709,4226021,432923,4108952,4111708,4049659)and invalid_reason is null
  
  ) I
  ) C;
  INSERT INTO #Codesets (codeset_id, concept_id)
  SELECT 9 as codeset_id, c.concept_id FROM (select distinct I.concept_id FROM
  ( 
    select concept_id from @cdmDatabaseSchema.CONCEPT where concept_id in (319835,316139,314378,439846)and invalid_reason is null
  
  ) I
  ) C;
  INSERT INTO #Codesets (codeset_id, concept_id)
  SELECT 10 as codeset_id, c.concept_id FROM (select distinct I.concept_id FROM
  ( 
    select concept_id from @cdmDatabaseSchema.CONCEPT where concept_id in (320128)and invalid_reason is null
  
  ) I
  ) C;
  INSERT INTO #Codesets (codeset_id, concept_id)
  SELECT 11 as codeset_id, c.concept_id FROM (select distinct I.concept_id FROM
  ( 
    select concept_id from @cdmDatabaseSchema.CONCEPT where concept_id in (4108805,319844,312327,312653,4332246,321318,314054,312337,4306703,4108832,321042,321319,376713,443454,381316,381591,315286,316999,4307356,321887,134057,318772,433208,320739,24966,320128,320425,321588,316139,195556,442604,201313,434056,317002,319843,4108234,313792,317309,313219,440417,315282,4175807,4226021,432923,4108217,4327889,4108817,4232337)and invalid_reason is null
  
  ) I
  ) C;
  INSERT INTO #Codesets (codeset_id, concept_id)
  SELECT 12 as codeset_id, c.concept_id FROM (select distinct I.concept_id FROM
  ( 
    select concept_id from @cdmDatabaseSchema.CONCEPT where concept_id in (1335471,1351557,1340128,19050216,1341927,1346686,1363749,19122327,1347384,1308216,1367500,1310756,40226742,1373225,1331235,1334456,1317640,1308842,19102107)and invalid_reason is null
  
  ) I
  ) C;
  INSERT INTO #Codesets (codeset_id, concept_id)
  SELECT 13 as codeset_id, c.concept_id FROM (select distinct I.concept_id FROM
  ( 
    select concept_id from @cdmDatabaseSchema.CONCEPT where concept_id in (1319998,1314002,1322081,1338005,950370,1346823,19049145,19063575,1386957,1307046,1314577,19024904,1327978,1345858,1353766)and invalid_reason is null
  
  ) I
  ) C;
  INSERT INTO #Codesets (codeset_id, concept_id)
  SELECT 14 as codeset_id, c.concept_id FROM (select distinct I.concept_id FROM
  ( 
    select concept_id from @cdmDatabaseSchema.CONCEPT where concept_id in (1328165,1353776,1326012,19004539,19015802,19071995,19102106,1318853,19113063,1319133,1319880,19020061,1307863)and invalid_reason is null
  
  ) I
  ) C;
  INSERT INTO #Codesets (codeset_id, concept_id)
  SELECT 15 as codeset_id, c.concept_id FROM (select distinct I.concept_id FROM
  ( 
    select concept_id from @cdmDatabaseSchema.CONCEPT where concept_id in (1395058,974166,978555,907013,19010493)and invalid_reason is null
  
  ) I
  ) C;
  
  
  with primary_events (event_id, person_id, start_date, end_date, op_start_date, op_end_date) as
  (
  -- Begin Primary Events
  select row_number() over (PARTITION BY P.person_id order by P.start_date) as event_id, P.person_id, P.start_date, P.end_date, OP.observation_period_start_date as op_start_date, OP.observation_period_end_date as op_end_date
  FROM
  (
    select P.person_id, P.start_date, P.end_date, ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY start_date ASC) ordinal
    FROM 
    (
    -- Begin Drug Era Criteria
  select C.person_id, C.drug_era_id as event_id, C.drug_era_start_date as start_date, C.drug_era_end_date as end_date, C.drug_concept_id as TARGET_CONCEPT_ID
  from 
  (
    select de.*, ROW_NUMBER() over (PARTITION BY de.person_id ORDER BY de.drug_era_start_date, de.drug_era_id) as ordinal
    FROM @cdmDatabaseSchema.DRUG_ERA de
  where de.drug_concept_id in (SELECT concept_id from  #Codesets where codeset_id = 12)
  ) C
  JOIN @cdmDatabaseSchema.PERSON P on C.person_id = P.person_id
  WHERE C.ordinal = 1
  AND DATEDIFF(d,C.drug_era_start_date, C.drug_era_end_date) >= 180
  AND YEAR(C.drug_era_start_date) - P.year_of_birth >= 20
  -- End Drug Era Criteria
  
  UNION ALL
  -- Begin Drug Era Criteria
  select C.person_id, C.drug_era_id as event_id, C.drug_era_start_date as start_date, C.drug_era_end_date as end_date, C.drug_concept_id as TARGET_CONCEPT_ID
  from 
  (
    select de.*, ROW_NUMBER() over (PARTITION BY de.person_id ORDER BY de.drug_era_start_date, de.drug_era_id) as ordinal
    FROM @cdmDatabaseSchema.DRUG_ERA de
  where de.drug_concept_id in (SELECT concept_id from  #Codesets where codeset_id = 15)
  ) C
  JOIN @cdmDatabaseSchema.PERSON P on C.person_id = P.person_id
  WHERE C.ordinal = 1
  AND DATEDIFF(d,C.drug_era_start_date, C.drug_era_end_date) >= 180
  AND YEAR(C.drug_era_start_date) - P.year_of_birth >= 20
  -- End Drug Era Criteria
  
    ) P
  ) P
  JOIN @cdmDatabaseSchema.observation_period OP on P.person_id = OP.person_id and P.start_date >=  OP.observation_period_start_date and P.start_date <= op.observation_period_end_date
  WHERE DATEADD(day,365,OP.OBSERVATION_PERIOD_START_DATE) <= P.START_DATE AND DATEADD(day,0,P.START_DATE) <= OP.OBSERVATION_PERIOD_END_DATE AND P.ordinal = 1
  -- End Primary Events
  
  )
  SELECT event_id, person_id, start_date, end_date, op_start_date, op_end_date
  INTO #qualified_events
  FROM 
  (
    select pe.event_id, pe.person_id, pe.start_date, pe.end_date, pe.op_start_date, pe.op_end_date, row_number() over (partition by pe.person_id order by pe.start_date ASC) as ordinal
    FROM primary_events pe
    
  JOIN (
  -- Begin Criteria Group
  select 0 as index_id, person_id, event_id
  FROM
  (
    select E.person_id, E.event_id 
    FROM primary_events E
    LEFT JOIN
    (
      -- Begin Correlated Criteria
  SELECT 0 as index_id, p.person_id, p.event_id
  FROM primary_events P
  LEFT JOIN
  (
    -- Begin Condition Occurrence Criteria
  SELECT C.person_id, C.condition_occurrence_id as event_id, C.condition_start_date as start_date, COALESCE(C.condition_end_date, DATEADD(day,1,C.condition_start_date)) as end_date, C.CONDITION_CONCEPT_ID as TARGET_CONCEPT_ID
  FROM 
  (
    SELECT co.*, ROW_NUMBER() over (PARTITION BY co.person_id ORDER BY co.condition_start_date, co.condition_occurrence_id) as ordinal
    FROM @cdmDatabaseSchema.CONDITION_OCCURRENCE co
    where co.condition_concept_id in (SELECT concept_id from  #Codesets where codeset_id = 10)
  ) C
  
  
  -- End Condition Occurrence Criteria
  
  ) A on A.person_id = P.person_id and A.START_DATE >= P.OP_START_DATE AND A.START_DATE <= P.OP_END_DATE AND A.START_DATE >= DATEADD(day,-365,P.START_DATE) and A.START_DATE <= DATEADD(day,0,P.START_DATE)
  GROUP BY p.person_id, p.event_id
  HAVING COUNT(A.TARGET_CONCEPT_ID) >= 1
  -- End Correlated Criteria
  
    ) CQ on E.person_id = CQ.person_id and E.event_id = CQ.event_id
    GROUP BY E.person_id, E.event_id
    HAVING COUNT(index_id) = 1
  ) G
  -- End Criteria Group
  ) AC on AC.person_id = pe.person_id and AC.event_id = pe.event_id
  
  ) QE
  WHERE QE.ordinal = 1
  ;
  
  
  create table #inclusionRuleCohorts 
  (
    inclusion_rule_id bigint,
    person_id bigint,
    event_id bigint
  )
  ;
  INSERT INTO #inclusionRuleCohorts (inclusion_rule_id, person_id, event_id)
  select 0 as inclusion_rule_id, person_id, event_id
  FROM 
  (
    select pe.person_id, pe.event_id
    FROM #qualified_events pe
    
  JOIN (
  -- Begin Criteria Group
  select 0 as index_id, person_id, event_id
  FROM
  (
    select E.person_id, E.event_id 
    FROM #qualified_events E
    LEFT JOIN
    (
      -- Begin Correlated Criteria
  SELECT 0 as index_id, p.person_id, p.event_id
  FROM #qualified_events P
  LEFT JOIN
  (
    -- Begin Drug Era Criteria
  select C.person_id, C.drug_era_id as event_id, C.drug_era_start_date as start_date, C.drug_era_end_date as end_date, C.drug_concept_id as TARGET_CONCEPT_ID
  from 
  (
    select de.*, ROW_NUMBER() over (PARTITION BY de.person_id ORDER BY de.drug_era_start_date, de.drug_era_id) as ordinal
    FROM @cdmDatabaseSchema.DRUG_ERA de
  where de.drug_concept_id in (SELECT concept_id from  #Codesets where codeset_id = 13)
  ) C
  
  WHERE DATEDIFF(d,C.drug_era_start_date, C.drug_era_end_date) >= 7
  -- End Drug Era Criteria
  
  ) A on A.person_id = P.person_id and A.START_DATE >= P.OP_START_DATE AND A.START_DATE <= P.OP_END_DATE AND A.START_DATE >= P.OP_START_DATE and A.START_DATE <= DATEADD(day,180,P.START_DATE)
  GROUP BY p.person_id, p.event_id
  HAVING COUNT(A.TARGET_CONCEPT_ID) <= 0
  -- End Correlated Criteria
  
    ) CQ on E.person_id = CQ.person_id and E.event_id = CQ.event_id
    GROUP BY E.person_id, E.event_id
    HAVING COUNT(index_id) = 1
  ) G
  -- End Criteria Group
  ) AC on AC.person_id = pe.person_id AND AC.event_id = pe.event_id
  ) Results
  ;
  
  INSERT INTO #inclusionRuleCohorts (inclusion_rule_id, person_id, event_id)
  select 1 as inclusion_rule_id, person_id, event_id
  FROM 
  (
    select pe.person_id, pe.event_id
    FROM #qualified_events pe
    
  JOIN (
  -- Begin Criteria Group
  select 0 as index_id, person_id, event_id
  FROM
  (
    select E.person_id, E.event_id 
    FROM #qualified_events E
    LEFT JOIN
    (
      -- Begin Correlated Criteria
  SELECT 0 as index_id, p.person_id, p.event_id
  FROM #qualified_events P
  LEFT JOIN
  (
    -- Begin Drug Era Criteria
  select C.person_id, C.drug_era_id as event_id, C.drug_era_start_date as start_date, C.drug_era_end_date as end_date, C.drug_concept_id as TARGET_CONCEPT_ID
  from 
  (
    select de.*, ROW_NUMBER() over (PARTITION BY de.person_id ORDER BY de.drug_era_start_date, de.drug_era_id) as ordinal
    FROM @cdmDatabaseSchema.DRUG_ERA de
  where de.drug_concept_id in (SELECT concept_id from  #Codesets where codeset_id = 14)
  ) C
  
  WHERE DATEDIFF(d,C.drug_era_start_date, C.drug_era_end_date) >= 7
  -- End Drug Era Criteria
  
  ) A on A.person_id = P.person_id and A.START_DATE >= P.OP_START_DATE AND A.START_DATE <= P.OP_END_DATE AND A.START_DATE >= P.OP_START_DATE and A.START_DATE <= DATEADD(day,180,P.START_DATE)
  GROUP BY p.person_id, p.event_id
  HAVING COUNT(A.TARGET_CONCEPT_ID) <= 0
  -- End Correlated Criteria
  
    ) CQ on E.person_id = CQ.person_id and E.event_id = CQ.event_id
    GROUP BY E.person_id, E.event_id
    HAVING COUNT(index_id) = 1
  ) G
  -- End Criteria Group
  ) AC on AC.person_id = pe.person_id AND AC.event_id = pe.event_id
  ) Results
  ;
  
  INSERT INTO #inclusionRuleCohorts (inclusion_rule_id, person_id, event_id)
  select 2 as inclusion_rule_id, person_id, event_id
  FROM 
  (
    select pe.person_id, pe.event_id
    FROM #qualified_events pe
    
  JOIN (
  -- Begin Criteria Group
  select 0 as index_id, person_id, event_id
  FROM
  (
    select E.person_id, E.event_id 
    FROM #qualified_events E
    LEFT JOIN
    (
      -- Begin Correlated Criteria
  SELECT 0 as index_id, p.person_id, p.event_id
  FROM #qualified_events P
  LEFT JOIN
  (
    -- Begin Death Criteria
  select C.person_id, C.person_id as event_id, C.death_date as start_date, DATEADD(d,1,C.death_date) as end_date, coalesce(C.cause_concept_id,0) as TARGET_CONCEPT_ID
  from 
  (
    select d.*
    FROM @cdmDatabaseSchema.DEATH d
  
  ) C
  
  
  -- End Death Criteria
  
  
  ) A on A.person_id = P.person_id and A.START_DATE >= P.OP_START_DATE AND A.START_DATE <= P.OP_END_DATE AND A.START_DATE >= P.OP_START_DATE and A.START_DATE <= DATEADD(day,7,P.START_DATE)
  GROUP BY p.person_id, p.event_id
  HAVING COUNT(A.TARGET_CONCEPT_ID) <= 0
  -- End Correlated Criteria
  
    ) CQ on E.person_id = CQ.person_id and E.event_id = CQ.event_id
    GROUP BY E.person_id, E.event_id
    HAVING COUNT(index_id) = 1
  ) G
  -- End Criteria Group
  ) AC on AC.person_id = pe.person_id AND AC.event_id = pe.event_id
  ) Results
  ;
  
  INSERT INTO #inclusionRuleCohorts (inclusion_rule_id, person_id, event_id)
  select 3 as inclusion_rule_id, person_id, event_id
  FROM 
  (
    select pe.person_id, pe.event_id
    FROM #qualified_events pe
    
  JOIN (
  -- Begin Criteria Group
  select 0 as index_id, person_id, event_id
  FROM
  (
    select E.person_id, E.event_id 
    FROM #qualified_events E
    LEFT JOIN
    (
      -- Begin Correlated Criteria
  SELECT 0 as index_id, p.person_id, p.event_id
  FROM #qualified_events P
  LEFT JOIN
  (
    -- Begin Condition Occurrence Criteria
  SELECT C.person_id, C.condition_occurrence_id as event_id, C.condition_start_date as start_date, COALESCE(C.condition_end_date, DATEADD(day,1,C.condition_start_date)) as end_date, C.CONDITION_CONCEPT_ID as TARGET_CONCEPT_ID
  FROM 
  (
    SELECT co.*, ROW_NUMBER() over (PARTITION BY co.person_id ORDER BY co.condition_start_date, co.condition_occurrence_id) as ordinal
    FROM @cdmDatabaseSchema.CONDITION_OCCURRENCE co
    where co.condition_concept_id in (SELECT concept_id from  #Codesets where codeset_id = 3)
  ) C
  
  
  -- End Condition Occurrence Criteria
  
  ) A on A.person_id = P.person_id and A.START_DATE >= P.OP_START_DATE AND A.START_DATE <= P.OP_END_DATE AND A.START_DATE >= P.OP_START_DATE and A.START_DATE <= DATEADD(day,7,P.START_DATE)
  GROUP BY p.person_id, p.event_id
  HAVING COUNT(A.TARGET_CONCEPT_ID) <= 0
  -- End Correlated Criteria
  
    ) CQ on E.person_id = CQ.person_id and E.event_id = CQ.event_id
    GROUP BY E.person_id, E.event_id
    HAVING COUNT(index_id) = 1
  ) G
  -- End Criteria Group
  ) AC on AC.person_id = pe.person_id AND AC.event_id = pe.event_id
  ) Results
  ;
  
  INSERT INTO #inclusionRuleCohorts (inclusion_rule_id, person_id, event_id)
  select 4 as inclusion_rule_id, person_id, event_id
  FROM 
  (
    select pe.person_id, pe.event_id
    FROM #qualified_events pe
    
  JOIN (
  -- Begin Criteria Group
  select 0 as index_id, person_id, event_id
  FROM
  (
    select E.person_id, E.event_id 
    FROM #qualified_events E
    LEFT JOIN
    (
      -- Begin Correlated Criteria
  SELECT 0 as index_id, p.person_id, p.event_id
  FROM #qualified_events P
  LEFT JOIN
  (
    -- Begin Condition Occurrence Criteria
  SELECT C.person_id, C.condition_occurrence_id as event_id, C.condition_start_date as start_date, COALESCE(C.condition_end_date, DATEADD(day,1,C.condition_start_date)) as end_date, C.CONDITION_CONCEPT_ID as TARGET_CONCEPT_ID
  FROM 
  (
    SELECT co.*, ROW_NUMBER() over (PARTITION BY co.person_id ORDER BY co.condition_start_date, co.condition_occurrence_id) as ordinal
    FROM @cdmDatabaseSchema.CONDITION_OCCURRENCE co
    where co.condition_concept_id in (SELECT concept_id from  #Codesets where codeset_id = 9)
  ) C
  
  
  -- End Condition Occurrence Criteria
  
  ) A on A.person_id = P.person_id and A.START_DATE >= P.OP_START_DATE AND A.START_DATE <= P.OP_END_DATE AND A.START_DATE >= P.OP_START_DATE and A.START_DATE <= DATEADD(day,7,P.START_DATE)
  GROUP BY p.person_id, p.event_id
  HAVING COUNT(A.TARGET_CONCEPT_ID) <= 0
  -- End Correlated Criteria
  
    ) CQ on E.person_id = CQ.person_id and E.event_id = CQ.event_id
    GROUP BY E.person_id, E.event_id
    HAVING COUNT(index_id) = 1
  ) G
  -- End Criteria Group
  ) AC on AC.person_id = pe.person_id AND AC.event_id = pe.event_id
  ) Results
  ;
  
  INSERT INTO #inclusionRuleCohorts (inclusion_rule_id, person_id, event_id)
  select 5 as inclusion_rule_id, person_id, event_id
  FROM 
  (
    select pe.person_id, pe.event_id
    FROM #qualified_events pe
    
  JOIN (
  -- Begin Criteria Group
  select 0 as index_id, person_id, event_id
  FROM
  (
    select E.person_id, E.event_id 
    FROM #qualified_events E
    LEFT JOIN
    (
      -- Begin Correlated Criteria
  SELECT 0 as index_id, p.person_id, p.event_id
  FROM #qualified_events P
  LEFT JOIN
  (
    -- Begin Condition Occurrence Criteria
  SELECT C.person_id, C.condition_occurrence_id as event_id, C.condition_start_date as start_date, COALESCE(C.condition_end_date, DATEADD(day,1,C.condition_start_date)) as end_date, C.CONDITION_CONCEPT_ID as TARGET_CONCEPT_ID
  FROM 
  (
    SELECT co.*, ROW_NUMBER() over (PARTITION BY co.person_id ORDER BY co.condition_start_date, co.condition_occurrence_id) as ordinal
    FROM @cdmDatabaseSchema.CONDITION_OCCURRENCE co
    where co.condition_concept_id in (SELECT concept_id from  #Codesets where codeset_id = 6)
  ) C
  
  
  -- End Condition Occurrence Criteria
  
  ) A on A.person_id = P.person_id and A.START_DATE >= P.OP_START_DATE AND A.START_DATE <= P.OP_END_DATE AND A.START_DATE >= P.OP_START_DATE and A.START_DATE <= DATEADD(day,7,P.START_DATE)
  GROUP BY p.person_id, p.event_id
  HAVING COUNT(A.TARGET_CONCEPT_ID) <= 0
  -- End Correlated Criteria
  
    ) CQ on E.person_id = CQ.person_id and E.event_id = CQ.event_id
    GROUP BY E.person_id, E.event_id
    HAVING COUNT(index_id) = 1
  ) G
  -- End Criteria Group
  ) AC on AC.person_id = pe.person_id AND AC.event_id = pe.event_id
  ) Results
  ;
  
  
  with cteIncludedEvents(event_id, person_id, start_date, end_date, op_start_date, op_end_date, ordinal) as
  (
    SELECT event_id, person_id, start_date, end_date, op_start_date, op_end_date, row_number() over (partition by person_id order by start_date ASC) as ordinal
    from
    (
      select Q.event_id, Q.person_id, Q.start_date, Q.end_date, Q.op_start_date, Q.op_end_date, SUM(coalesce(POWER(cast(2 as bigint), I.inclusion_rule_id), 0)) as inclusion_rule_mask
      from #qualified_events Q
      LEFT JOIN #inclusionRuleCohorts I on I.person_id = Q.person_id and I.event_id = Q.event_id
      GROUP BY Q.event_id, Q.person_id, Q.start_date, Q.end_date, Q.op_start_date, Q.op_end_date
    ) MG -- matching groups
  
    -- the matching group with all bits set ( POWER(2,# of inclusion rules) - 1 = inclusion_rule_mask
    WHERE (MG.inclusion_rule_mask = POWER(cast(2 as bigint),6)-1)
  
  )
  select event_id, person_id, start_date, end_date, op_start_date, op_end_date
  into #included_events
  FROM cteIncludedEvents Results
  WHERE Results.ordinal = 1
  ;
  
  -- Apply end date stratagies
  -- by default, all events extend to the op_end_date.
  select event_id, person_id, op_end_date as end_date
  into #cohort_ends
  from #included_events;
  
  
  
  INSERT INTO #cohort_ends (event_id, person_id, end_date)
  select i.event_id, i.person_id, MIN(c.start_date) as end_date
  FROM #included_events i
  JOIN
  (
  -- Begin Drug Era Criteria
  select C.person_id, C.drug_era_id as event_id, C.drug_era_start_date as start_date, C.drug_era_end_date as end_date, C.drug_concept_id as TARGET_CONCEPT_ID
  from 
  (
    select de.*, ROW_NUMBER() over (PARTITION BY de.person_id ORDER BY de.drug_era_start_date, de.drug_era_id) as ordinal
    FROM @cdmDatabaseSchema.DRUG_ERA de
  where de.drug_concept_id in (SELECT concept_id from  #Codesets where codeset_id = 13)
  ) C
  
  WHERE DATEDIFF(d,C.drug_era_start_date, C.drug_era_end_date) >= 7
  -- End Drug Era Criteria
  
  ) C on C.person_id = I.person_id and C.start_date >= I.start_date and C.START_DATE <= I.op_end_date
  GROUP BY i.event_id, i.person_id
  ;
  
  INSERT INTO #cohort_ends (event_id, person_id, end_date)
  select i.event_id, i.person_id, MIN(c.start_date) as end_date
  FROM #included_events i
  JOIN
  (
  -- Begin Drug Era Criteria
  select C.person_id, C.drug_era_id as event_id, C.drug_era_start_date as start_date, C.drug_era_end_date as end_date, C.drug_concept_id as TARGET_CONCEPT_ID
  from 
  (
    select de.*, ROW_NUMBER() over (PARTITION BY de.person_id ORDER BY de.drug_era_start_date, de.drug_era_id) as ordinal
    FROM @cdmDatabaseSchema.DRUG_ERA de
  where de.drug_concept_id in (SELECT concept_id from  #Codesets where codeset_id = 14)
  ) C
  
  WHERE DATEDIFF(d,C.drug_era_start_date, C.drug_era_end_date) >= 7
  -- End Drug Era Criteria
  
  ) C on C.person_id = I.person_id and C.start_date >= I.start_date and C.START_DATE <= I.op_end_date
  GROUP BY i.event_id, i.person_id
  ;
  
  
  DELETE FROM @resultsDatabaseSchema.@exposureTable where cohort_definition_id = @target_cohort_id;
  INSERT INTO @resultsDatabaseSchema.@exposureTable (cohort_definition_id, subject_id, cohort_start_date, cohort_end_date)
  select @target_cohort_id as cohort_definition_id, F.person_id, F.start_date, F.end_date
  FROM (
    select I.person_id, I.start_date, E.end_date, row_number() over (partition by I.person_id, I.event_id order by E.end_date) as ordinal 
    from #included_events I
    join #cohort_ends E on I.event_id = E.event_id and I.person_id = E.person_id and E.end_date >= I.start_date
  ) F
  WHERE F.ordinal = 1
  ;
  
  
  
  
  TRUNCATE TABLE #cohort_ends;
  DROP TABLE #cohort_ends;
  
  TRUNCATE TABLE #inclusionRuleCohorts;
  DROP TABLE #inclusionRuleCohorts;
  
  TRUNCATE TABLE #qualified_events;
  DROP TABLE #qualified_events;
  
  TRUNCATE TABLE #included_events;
  DROP TABLE #included_events;
  
  TRUNCATE TABLE #Codesets;
  DROP TABLE #Codesets;