text: CREATE TABLE #Codesets (
    codeset_id int NOT NULL,
    concept_id bigint NOT NULL
  )
  ;
  
  INSERT INTO #Codesets (codeset_id, concept_id)
  SELECT 0 as codeset_id, c.concept_id FROM (select distinct I.concept_id FROM
  ( 
    select concept_id from @vocabulary_database_schema.CONCEPT where concept_id in (4006788,4264285,4265293,4225903,4283892,4337738,4139198,44511532,45770795,44789455,44784573,44512256,44511273,44511272,44511271,44511269,44511268,44511133,44511131,44511130,43533353,43533352,43533248,43533247,43531440,43531439,43531438,4329263,4328103,4264286,4238755,4216356,4214516,4181025,4178148,4175997,4171077,4020653,2001506,2001505,2000064)
  UNION  select c.concept_id
    from @vocabulary_database_schema.CONCEPT c
    join @vocabulary_database_schema.CONCEPT_ANCESTOR ca on c.concept_id = ca.descendant_concept_id
    and ca.ancestor_concept_id in (4006788,4264285,4265293,4225903,4283892,4337738,4139198,44511532,45770795,44784573,44512256,44511273,44511271,44511269,44511268,44511131,44511130,43533353,43533248,43531440,4329263,4181025,4178148,4175997,4020653,2000064)
    and c.invalid_reason is null
  
  ) I
  ) C;
  INSERT INTO #Codesets (codeset_id, concept_id)
  SELECT 1 as codeset_id, c.concept_id FROM (select distinct I.concept_id FROM
  ( 
    select concept_id from @vocabulary_database_schema.CONCEPT where concept_id in (312327,438170,434376,444406,315296)
  UNION  select c.concept_id
    from @vocabulary_database_schema.CONCEPT c
    join @vocabulary_database_schema.CONCEPT_ANCESTOR ca on c.concept_id = ca.descendant_concept_id
    and ca.ancestor_concept_id in (312327,438170,434376,444406,315296)
    and c.invalid_reason is null
  
  ) I
  ) C;
  INSERT INTO #Codesets (codeset_id, concept_id)
  SELECT 2 as codeset_id, c.concept_id FROM (select distinct I.concept_id FROM
  ( 
    select concept_id from @vocabulary_database_schema.CONCEPT where concept_id in (312327,438170,434376,444406)
  UNION  select c.concept_id
    from @vocabulary_database_schema.CONCEPT c
    join @vocabulary_database_schema.CONCEPT_ANCESTOR ca on c.concept_id = ca.descendant_concept_id
    and ca.ancestor_concept_id in (312327,438170,434376,444406)
    and c.invalid_reason is null
  
  ) I
  ) C;
  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 @vocabulary_database_schema.CONCEPT where concept_id in (1112807)
  UNION  select c.concept_id
    from @vocabulary_database_schema.CONCEPT c
    join @vocabulary_database_schema.CONCEPT_ANCESTOR ca on c.concept_id = ca.descendant_concept_id
    and ca.ancestor_concept_id in (1112807)
    and c.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 @vocabulary_database_schema.CONCEPT where concept_id in (40241186)
  UNION  select c.concept_id
    from @vocabulary_database_schema.CONCEPT c
    join @vocabulary_database_schema.CONCEPT_ANCESTOR ca on c.concept_id = ca.descendant_concept_id
    and ca.ancestor_concept_id in (40241186)
    and c.invalid_reason is null
  
  ) I
  ) C;
  INSERT INTO #Codesets (codeset_id, concept_id)
  SELECT 5 as codeset_id, c.concept_id FROM (select distinct I.concept_id FROM
  ( 
    select concept_id from @vocabulary_database_schema.CONCEPT where concept_id in (1322184)
  UNION  select c.concept_id
    from @vocabulary_database_schema.CONCEPT c
    join @vocabulary_database_schema.CONCEPT_ANCESTOR ca on c.concept_id = ca.descendant_concept_id
    and ca.ancestor_concept_id in (1322184)
    and c.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 @vocabulary_database_schema.CONCEPT where concept_id in (4283892)
  UNION  select c.concept_id
    from @vocabulary_database_schema.CONCEPT c
    join @vocabulary_database_schema.CONCEPT_ANCESTOR ca on c.concept_id = ca.descendant_concept_id
    and ca.ancestor_concept_id in (4283892)
    and c.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 @vocabulary_database_schema.CONCEPT where concept_id in (40163718)
  UNION  select c.concept_id
    from @vocabulary_database_schema.CONCEPT c
    join @vocabulary_database_schema.CONCEPT_ANCESTOR ca on c.concept_id = ca.descendant_concept_id
    and ca.ancestor_concept_id in (40163718)
    and c.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 @vocabulary_database_schema.CONCEPT where concept_id in (443454,4043731)
  UNION  select c.concept_id
    from @vocabulary_database_schema.CONCEPT c
    join @vocabulary_database_schema.CONCEPT_ANCESTOR ca on c.concept_id = ca.descendant_concept_id
    and ca.ancestor_concept_id in (443454,4043731)
    and c.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 @vocabulary_database_schema.CONCEPT where concept_id in (4319328,4326561,4218781,376713,4176892,4110185,439847,436430,4111709,4226021,432923,4108952,4111708,4049659,439040)
  
  ) 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 @vocabulary_database_schema.CONCEPT where concept_id in (192671,443530,4103703)
  UNION  select c.concept_id
    from @vocabulary_database_schema.CONCEPT c
    join @vocabulary_database_schema.CONCEPT_ANCESTOR ca on c.concept_id = ca.descendant_concept_id
    and ca.ancestor_concept_id in (192671,443530,4103703)
    and c.invalid_reason is null
  
  ) I
  LEFT JOIN
  (
    select concept_id from @vocabulary_database_schema.CONCEPT where concept_id in (4189259,37109016,4071070,4048608,4048286,194158,4048601,4048282,4048602)
  
  ) E ON I.concept_id = E.concept_id
  WHERE E.concept_id is null
  ) 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 @vocabulary_database_schema.CONCEPT where concept_id in (9203,9201)
  UNION  select c.concept_id
    from @vocabulary_database_schema.CONCEPT c
    join @vocabulary_database_schema.CONCEPT_ANCESTOR ca on c.concept_id = ca.descendant_concept_id
    and ca.ancestor_concept_id in (9203,9201)
    and c.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 @vocabulary_database_schema.CONCEPT where concept_id in (2001516,4284104,4229433,4146972,4228305,4228304,4063237,4148030,4008625,4106548,4031996,4234990,45889469,4240486,4336464,4337056,4000733,4336467,4336465,4339629,4337737,4336466,4233421,4305509,4309432,4011931,4253805,45887879,2107242,2107243,2107244,2107231,45889898,2107223,2107224,2107226,2107227,2107228,2107222,45887862,2107217,2107218,2107219,2107220,2107221,2107216,2001515,4000732,4305852,4020216,4233420,2001514,45889467)
  UNION  select c.concept_id
    from @vocabulary_database_schema.CONCEPT c
    join @vocabulary_database_schema.CONCEPT_ANCESTOR ca on c.concept_id = ca.descendant_concept_id
    and ca.ancestor_concept_id in (2001516,4284104,4229433,4146972,4228305,4228304,4063237,4148030,4008625,4106548,4031996,4234990,45889469,4240486,4336464,4337056,4000733,4336467,4336465,4339629,4337737,4336466,4233421,4305509,4309432,4011931,4253805,45887879,2107242,2107243,2107244,2107231,45889898,2107223,2107224,2107226,2107227,2107228,2107222,45887862,2107217,2107218,2107219,2107220,2107221,2107216,2001515,4000732,4233420,2001514,45889467)
    and c.invalid_reason is null
  
  ) I
  ) C;
  
  
  with primary_events (event_id, person_id, start_date, end_date, op_start_date, op_end_date, visit_occurrence_id) as
  (
  -- Begin Primary Events
  select P.ordinal as event_id, P.person_id, P.start_date, P.end_date, op_start_date, op_end_date, cast(P.visit_occurrence_id as bigint) as visit_occurrence_id
  FROM
  (
    select E.person_id, E.start_date, E.end_date,
           row_number() OVER (PARTITION BY E.person_id ORDER BY E.sort_date ASC) ordinal,
           OP.observation_period_start_date as op_start_date, OP.observation_period_end_date as op_end_date, cast(E.visit_occurrence_id as bigint) as visit_occurrence_id
    FROM 
    (
    -- Begin Procedure Occurrence Criteria
  select C.person_id, C.procedure_occurrence_id as event_id, C.procedure_date as start_date, DATEADD(d,1,C.procedure_date) as END_DATE,
         C.procedure_concept_id as TARGET_CONCEPT_ID, C.visit_occurrence_id,
         C.procedure_date as sort_date
  from 
  (
    select po.* , row_number() over (PARTITION BY po.person_id ORDER BY po.procedure_date, po.procedure_occurrence_id) as ordinal
    FROM @cdm_database_schema.PROCEDURE_OCCURRENCE po
  JOIN #Codesets codesets on ((po.procedure_concept_id = codesets.concept_id and codesets.codeset_id = 0))
  ) C
  JOIN @cdm_database_schema.PERSON P on C.person_id = P.person_id
  WHERE YEAR(C.procedure_date) - P.year_of_birth >= 20
  AND C.ordinal = 1
  -- End Procedure Occurrence Criteria
  
    ) E
  	JOIN @cdm_database_schema.observation_period OP on E.person_id = OP.person_id and E.start_date >=  OP.observation_period_start_date and E.start_date <= op.observation_period_end_date
    WHERE DATEADD(day,365,OP.OBSERVATION_PERIOD_START_DATE) <= E.START_DATE AND DATEADD(day,0,E.START_DATE) <= OP.OBSERVATION_PERIOD_END_DATE
  ) P
  WHERE P.ordinal = 1
  -- End Primary Events
  
  )
  SELECT event_id, person_id, start_date, end_date, op_start_date, op_end_date, visit_occurrence_id
  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, cast(pe.visit_occurrence_id as bigint) as visit_occurrence_id
    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
    INNER JOIN
    (
      -- Begin Correlated Criteria
  SELECT 0 as index_id, p.person_id, p.event_id
  FROM primary_events P
  INNER 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, C.visit_occurrence_id,
         C.condition_start_date as sort_date
  FROM 
  (
    SELECT co.* 
    FROM @cdm_database_schema.CONDITION_OCCURRENCE co
    JOIN #Codesets codesets on ((co.condition_concept_id = codesets.concept_id and codesets.codeset_id = 1))
  ) 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,-7,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
  
  UNION ALL
  -- Begin Correlated Criteria
  SELECT 1 as index_id, p.person_id, p.event_id
  FROM primary_events P
  INNER JOIN
  (
    -- Begin Drug Exposure Criteria
  select C.person_id, C.drug_exposure_id as event_id, C.drug_exposure_start_date as start_date,
         COALESCE(C.drug_exposure_end_date, DATEADD(day, 1, C.drug_exposure_start_date)) as end_date, C.drug_concept_id as TARGET_CONCEPT_ID, C.visit_occurrence_id,
         C.drug_exposure_start_date as sort_date
  from 
  (
    select de.* 
    FROM @cdm_database_schema.DRUG_EXPOSURE de
  JOIN #Codesets codesets on ((de.drug_concept_id = codesets.concept_id and codesets.codeset_id = 5))
  ) C
  
  
  -- End Drug Exposure 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,-7,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) = 2
  ) G
  -- End Criteria Group
  ) AC on AC.person_id = pe.person_id and AC.event_id = pe.event_id
  
  ) QE
  WHERE QE.ordinal = 1
  ;
  
  --- Inclusion Rule Inserts
  
  select 0 as inclusion_rule_id, person_id, event_id
  INTO #Inclusion_0
  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
    INNER JOIN
    (
      -- Begin Correlated Criteria
  SELECT 0 as index_id, p.person_id, p.event_id
  FROM #qualified_events P
  LEFT JOIN
  (
    -- Begin Drug Exposure Criteria
  select C.person_id, C.drug_exposure_id as event_id, C.drug_exposure_start_date as start_date,
         COALESCE(C.drug_exposure_end_date, DATEADD(day, 1, C.drug_exposure_start_date)) as end_date, C.drug_concept_id as TARGET_CONCEPT_ID, C.visit_occurrence_id,
         C.drug_exposure_start_date as sort_date
  from 
  (
    select de.* 
    FROM @cdm_database_schema.DRUG_EXPOSURE de
  JOIN #Codesets codesets on ((de.drug_concept_id = codesets.concept_id and codesets.codeset_id = 4))
  ) C
  
  
  -- End Drug Exposure 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,-30,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) <= 0
  -- End Correlated Criteria
  
  UNION ALL
  -- Begin Correlated Criteria
  SELECT 1 as index_id, p.person_id, p.event_id
  FROM #qualified_events P
  LEFT JOIN
  (
    -- Begin Drug Exposure Criteria
  select C.person_id, C.drug_exposure_id as event_id, C.drug_exposure_start_date as start_date,
         COALESCE(C.drug_exposure_end_date, DATEADD(day, 1, C.drug_exposure_start_date)) as end_date, C.drug_concept_id as TARGET_CONCEPT_ID, C.visit_occurrence_id,
         C.drug_exposure_start_date as sort_date
  from 
  (
    select de.* 
    FROM @cdm_database_schema.DRUG_EXPOSURE de
  JOIN #Codesets codesets on ((de.drug_concept_id = codesets.concept_id and codesets.codeset_id = 7))
  ) C
  
  
  -- End Drug Exposure 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,-30,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) <= 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) = 2
  ) G
  -- End Criteria Group
  ) AC on AC.person_id = pe.person_id AND AC.event_id = pe.event_id
  ) Results
  ;
  
  select 1 as inclusion_rule_id, person_id, event_id
  INTO #Inclusion_1
  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
    INNER 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, C.visit_occurrence_id,
         C.condition_start_date as sort_date
  FROM 
  (
    SELECT co.* 
    FROM @cdm_database_schema.CONDITION_OCCURRENCE co
    JOIN #Codesets codesets on ((co.condition_concept_id = codesets.concept_id and codesets.codeset_id = 8))
  ) 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,0,P.START_DATE)
  GROUP BY p.person_id, p.event_id
  HAVING COUNT(A.TARGET_CONCEPT_ID) <= 0
  -- End Correlated Criteria
  
  UNION ALL
  -- Begin Correlated Criteria
  SELECT 1 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, C.visit_occurrence_id,
         C.condition_start_date as sort_date
  FROM 
  (
    SELECT co.* 
    FROM @cdm_database_schema.CONDITION_OCCURRENCE co
    JOIN #Codesets codesets on ((co.condition_concept_id = codesets.concept_id and codesets.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,0,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) = 2
  ) G
  -- End Criteria Group
  ) AC on AC.person_id = pe.person_id AND AC.event_id = pe.event_id
  ) Results
  ;
  
  select 2 as inclusion_rule_id, person_id, event_id
  INTO #Inclusion_2
  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
    INNER 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, C.visit_occurrence_id,
         C.condition_start_date as sort_date
  FROM 
  (
    SELECT co.* 
    FROM @cdm_database_schema.CONDITION_OCCURRENCE co
    JOIN #Codesets codesets on ((co.condition_concept_id = codesets.concept_id and codesets.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 >= P.OP_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) <= 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
  ;
  
  select 3 as inclusion_rule_id, person_id, event_id
  INTO #Inclusion_3
  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
    INNER JOIN
    (
      -- Begin Correlated Criteria
  SELECT 0 as index_id, p.person_id, p.event_id
  FROM #qualified_events P
  LEFT JOIN
  (
    select PE.person_id, PE.event_id, PE.start_date, PE.end_date, PE.target_concept_id, PE.visit_occurrence_id, PE.sort_date FROM (
  -- Begin Procedure Occurrence Criteria
  select C.person_id, C.procedure_occurrence_id as event_id, C.procedure_date as start_date, DATEADD(d,1,C.procedure_date) as END_DATE,
         C.procedure_concept_id as TARGET_CONCEPT_ID, C.visit_occurrence_id,
         C.procedure_date as sort_date
  from 
  (
    select po.* 
    FROM @cdm_database_schema.PROCEDURE_OCCURRENCE po
  JOIN #Codesets codesets on ((po.procedure_concept_id = codesets.concept_id and codesets.codeset_id = 0))
  ) C
  
  
  -- End Procedure Occurrence Criteria
  
  ) PE
  JOIN (
  -- Begin Criteria Group
  select 0 as index_id, person_id, event_id
  FROM
  (
    select E.person_id, E.event_id 
    FROM (SELECT Q.person_id, Q.event_id, Q.start_date, Q.end_date, Q.visit_occurrence_id, OP.observation_period_start_date as op_start_date, OP.observation_period_end_date as op_end_date
  FROM (-- Begin Procedure Occurrence Criteria
  select C.person_id, C.procedure_occurrence_id as event_id, C.procedure_date as start_date, DATEADD(d,1,C.procedure_date) as END_DATE,
         C.procedure_concept_id as TARGET_CONCEPT_ID, C.visit_occurrence_id,
         C.procedure_date as sort_date
  from 
  (
    select po.* 
    FROM @cdm_database_schema.PROCEDURE_OCCURRENCE po
  JOIN #Codesets codesets on ((po.procedure_concept_id = codesets.concept_id and codesets.codeset_id = 0))
  ) C
  
  
  -- End Procedure Occurrence Criteria
  ) Q
  JOIN @cdm_database_schema.OBSERVATION_PERIOD OP on Q.person_id = OP.person_id 
    and OP.observation_period_start_date <= Q.start_date and OP.observation_period_end_date >= Q.start_date
  ) E
    INNER JOIN
    (
      -- Begin Correlated Criteria
  SELECT 0 as index_id, p.person_id, p.event_id
  FROM (SELECT Q.person_id, Q.event_id, Q.start_date, Q.end_date, Q.visit_occurrence_id, OP.observation_period_start_date as op_start_date, OP.observation_period_end_date as op_end_date
  FROM (-- Begin Procedure Occurrence Criteria
  select C.person_id, C.procedure_occurrence_id as event_id, C.procedure_date as start_date, DATEADD(d,1,C.procedure_date) as END_DATE,
         C.procedure_concept_id as TARGET_CONCEPT_ID, C.visit_occurrence_id,
         C.procedure_date as sort_date
  from 
  (
    select po.* 
    FROM @cdm_database_schema.PROCEDURE_OCCURRENCE po
  JOIN #Codesets codesets on ((po.procedure_concept_id = codesets.concept_id and codesets.codeset_id = 0))
  ) C
  
  
  -- End Procedure Occurrence Criteria
  ) Q
  JOIN @cdm_database_schema.OBSERVATION_PERIOD OP on Q.person_id = OP.person_id 
    and OP.observation_period_start_date <= Q.start_date and OP.observation_period_end_date >= Q.start_date
  ) P
  INNER JOIN
  (
    -- Begin Visit Occurrence Criteria
  select C.person_id, C.visit_occurrence_id as event_id, C.visit_start_date as start_date, C.visit_end_date as end_date,
         C.visit_concept_id as TARGET_CONCEPT_ID, C.visit_occurrence_id,
         C.visit_start_date as sort_date
  from 
  (
    select vo.* 
    FROM @cdm_database_schema.VISIT_OCCURRENCE vo
  JOIN #Codesets codesets on ((vo.visit_concept_id = codesets.concept_id and codesets.codeset_id = 11))
  ) C
  
  
  -- End Visit 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,0,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
  
  ) 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,1,P.START_DATE) AND A.START_DATE <= DATEADD(day,365,P.START_DATE)
  GROUP BY p.person_id, p.event_id
  HAVING COUNT(A.TARGET_CONCEPT_ID) = 0
  -- End Correlated Criteria
  
  UNION ALL
  -- Begin Correlated Criteria
  SELECT 1 as index_id, p.person_id, p.event_id
  FROM #qualified_events P
  LEFT JOIN
  (
    select PE.person_id, PE.event_id, PE.start_date, PE.end_date, PE.target_concept_id, PE.visit_occurrence_id, PE.sort_date FROM (
  -- Begin Procedure Occurrence Criteria
  select C.person_id, C.procedure_occurrence_id as event_id, C.procedure_date as start_date, DATEADD(d,1,C.procedure_date) as END_DATE,
         C.procedure_concept_id as TARGET_CONCEPT_ID, C.visit_occurrence_id,
         C.procedure_date as sort_date
  from 
  (
    select po.* 
    FROM @cdm_database_schema.PROCEDURE_OCCURRENCE po
  JOIN #Codesets codesets on ((po.procedure_concept_id = codesets.concept_id and codesets.codeset_id = 12))
  ) C
  
  
  -- End Procedure Occurrence Criteria
  
  ) PE
  JOIN (
  -- Begin Criteria Group
  select 0 as index_id, person_id, event_id
  FROM
  (
    select E.person_id, E.event_id 
    FROM (SELECT Q.person_id, Q.event_id, Q.start_date, Q.end_date, Q.visit_occurrence_id, OP.observation_period_start_date as op_start_date, OP.observation_period_end_date as op_end_date
  FROM (-- Begin Procedure Occurrence Criteria
  select C.person_id, C.procedure_occurrence_id as event_id, C.procedure_date as start_date, DATEADD(d,1,C.procedure_date) as END_DATE,
         C.procedure_concept_id as TARGET_CONCEPT_ID, C.visit_occurrence_id,
         C.procedure_date as sort_date
  from 
  (
    select po.* 
    FROM @cdm_database_schema.PROCEDURE_OCCURRENCE po
  JOIN #Codesets codesets on ((po.procedure_concept_id = codesets.concept_id and codesets.codeset_id = 12))
  ) C
  
  
  -- End Procedure Occurrence Criteria
  ) Q
  JOIN @cdm_database_schema.OBSERVATION_PERIOD OP on Q.person_id = OP.person_id 
    and OP.observation_period_start_date <= Q.start_date and OP.observation_period_end_date >= Q.start_date
  ) E
    INNER JOIN
    (
      -- Begin Correlated Criteria
  SELECT 0 as index_id, p.person_id, p.event_id
  FROM (SELECT Q.person_id, Q.event_id, Q.start_date, Q.end_date, Q.visit_occurrence_id, OP.observation_period_start_date as op_start_date, OP.observation_period_end_date as op_end_date
  FROM (-- Begin Procedure Occurrence Criteria
  select C.person_id, C.procedure_occurrence_id as event_id, C.procedure_date as start_date, DATEADD(d,1,C.procedure_date) as END_DATE,
         C.procedure_concept_id as TARGET_CONCEPT_ID, C.visit_occurrence_id,
         C.procedure_date as sort_date
  from 
  (
    select po.* 
    FROM @cdm_database_schema.PROCEDURE_OCCURRENCE po
  JOIN #Codesets codesets on ((po.procedure_concept_id = codesets.concept_id and codesets.codeset_id = 12))
  ) C
  
  
  -- End Procedure Occurrence Criteria
  ) Q
  JOIN @cdm_database_schema.OBSERVATION_PERIOD OP on Q.person_id = OP.person_id 
    and OP.observation_period_start_date <= Q.start_date and OP.observation_period_end_date >= Q.start_date
  ) P
  INNER JOIN
  (
    -- Begin Visit Occurrence Criteria
  select C.person_id, C.visit_occurrence_id as event_id, C.visit_start_date as start_date, C.visit_end_date as end_date,
         C.visit_concept_id as TARGET_CONCEPT_ID, C.visit_occurrence_id,
         C.visit_start_date as sort_date
  from 
  (
    select vo.* 
    FROM @cdm_database_schema.VISIT_OCCURRENCE vo
  JOIN #Codesets codesets on ((vo.visit_concept_id = codesets.concept_id and codesets.codeset_id = 11))
  ) C
  
  
  -- End Visit 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,0,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
  
  ) 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,1,P.START_DATE) AND A.START_DATE <= DATEADD(day,365,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) = 2
  ) G
  -- End Criteria Group
  ) AC on AC.person_id = pe.person_id AND AC.event_id = pe.event_id
  ) Results
  ;
  
  SELECT inclusion_rule_id, person_id, event_id
  INTO #inclusion_events
  FROM (select inclusion_rule_id, person_id, event_id from #Inclusion_0
  UNION ALL
  select inclusion_rule_id, person_id, event_id from #Inclusion_1
  UNION ALL
  select inclusion_rule_id, person_id, event_id from #Inclusion_2
  UNION ALL
  select inclusion_rule_id, person_id, event_id from #Inclusion_3) I;
  TRUNCATE TABLE #Inclusion_0;
  DROP TABLE #Inclusion_0;
  
  TRUNCATE TABLE #Inclusion_1;
  DROP TABLE #Inclusion_1;
  
  TRUNCATE TABLE #Inclusion_2;
  DROP TABLE #Inclusion_2;
  
  TRUNCATE TABLE #Inclusion_3;
  DROP TABLE #Inclusion_3;
  
  
  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 #inclusion_events 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),4)-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
  ;
  
  -- custom era strategy
  
  with ctePersons(person_id) as (
  	select distinct person_id from #included_events
  )
  
  select person_id, drug_exposure_start_date, drug_exposure_end_date
  INTO #drugTarget
  FROM (
  	select de.PERSON_ID, DRUG_EXPOSURE_START_DATE,  COALESCE(DRUG_EXPOSURE_END_DATE, DATEADD(day,DAYS_SUPPLY,DRUG_EXPOSURE_START_DATE), DATEADD(day,1,DRUG_EXPOSURE_START_DATE)) as DRUG_EXPOSURE_END_DATE 
  	FROM @cdm_database_schema.DRUG_EXPOSURE de
  	JOIN ctePersons p on de.person_id = p.person_id
  	JOIN #Codesets cs on cs.codeset_id = 5 AND de.drug_concept_id = cs.concept_id
  
  	UNION ALL
  
  	select de.PERSON_ID, DRUG_EXPOSURE_START_DATE,  COALESCE(DRUG_EXPOSURE_END_DATE, DATEADD(day,DAYS_SUPPLY,DRUG_EXPOSURE_START_DATE), DATEADD(day,1,DRUG_EXPOSURE_START_DATE)) as DRUG_EXPOSURE_END_DATE 
  	FROM @cdm_database_schema.DRUG_EXPOSURE de
  	JOIN ctePersons p on de.person_id = p.person_id
  	JOIN #Codesets cs on cs.codeset_id = 5 AND de.drug_source_concept_id = cs.concept_id
  ) E
  ;
  
  select et.event_id, et.person_id, ERAS.era_end_date as end_date
  INTO #strategy_ends
  from #included_events et
  JOIN 
  (
    select ENDS.person_id, min(drug_exposure_start_date) as era_start_date, DATEADD(day,0, ENDS.era_end_date) as era_end_date
    from
    (
      select de.person_id, de.drug_exposure_start_date, MIN(e.END_DATE) as era_end_date
      FROM #drugTarget DE
      JOIN 
      (
        --cteEndDates
        select PERSON_ID, DATEADD(day,-1 * 7,EVENT_DATE) as END_DATE -- unpad the end date by 7
        FROM
        (
  				select PERSON_ID, EVENT_DATE, EVENT_TYPE, 
  				MAX(START_ORDINAL) OVER (PARTITION BY PERSON_ID ORDER BY event_date, event_type ROWS UNBOUNDED PRECEDING) AS start_ordinal,
  				ROW_NUMBER() OVER (PARTITION BY PERSON_ID ORDER BY EVENT_DATE, EVENT_TYPE) AS OVERALL_ORD -- this re-numbers the inner UNION so all rows are numbered ordered by the event date
  				from
  				(
  					-- select the start dates, assigning a row number to each
  					Select PERSON_ID, DRUG_EXPOSURE_START_DATE AS EVENT_DATE, 0 as EVENT_TYPE, ROW_NUMBER() OVER (PARTITION BY PERSON_ID ORDER BY DRUG_EXPOSURE_START_DATE) as START_ORDINAL
  					from #drugTarget D
  
  					UNION ALL
  
  					-- add the end dates with NULL as the row number, padding the end dates by 7 to allow a grace period for overlapping ranges.
  					select PERSON_ID, DATEADD(day,7,DRUG_EXPOSURE_END_DATE), 1 as EVENT_TYPE, NULL
  					FROM #drugTarget D
  				) RAWDATA
        ) E
        WHERE 2 * E.START_ORDINAL - E.OVERALL_ORD = 0
      ) E on DE.PERSON_ID = E.PERSON_ID and E.END_DATE >= DE.DRUG_EXPOSURE_START_DATE
      GROUP BY de.person_id, de.drug_exposure_start_date
    ) ENDS
    GROUP BY ENDS.person_id, ENDS.era_end_date
  ) ERAS on ERAS.person_id = et.person_id 
  WHERE et.start_date between ERAS.era_start_date and ERAS.era_end_date;
  
  TRUNCATE TABLE #drugTarget;
  DROP TABLE #drugTarget;
  
  
  -- generate cohort periods into #final_cohort
  with cohort_ends (event_id, person_id, end_date) as
  (
  	-- cohort exit dates
    -- By default, cohort exit at the event's op end date
  select event_id, person_id, op_end_date as end_date from #included_events
  UNION ALL
  -- End Date Strategy
  SELECT event_id, person_id, end_date from #strategy_ends
  
  ),
  first_ends (person_id, start_date, end_date) as
  (
  	select F.person_id, F.start_date, F.end_date
  	FROM (
  	  select I.event_id, 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
  )
  select person_id, start_date, end_date
  INTO #cohort_rows
  from first_ends;
  
  with cteEndDates (person_id, end_date) AS -- the magic
  (	
  	SELECT
  		person_id
  		, DATEADD(day,-1 * 0, event_date)  as end_date
  	FROM
  	(
  		SELECT
  			person_id
  			, event_date
  			, event_type
  			, MAX(start_ordinal) OVER (PARTITION BY person_id ORDER BY event_date, event_type ROWS UNBOUNDED PRECEDING) AS start_ordinal 
  			, ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY event_date, event_type) AS overall_ord
  		FROM
  		(
  			SELECT
  				person_id
  				, start_date AS event_date
  				, -1 AS event_type
  				, ROW_NUMBER() OVER (PARTITION BY person_id ORDER BY start_date) AS start_ordinal
  			FROM #cohort_rows
  		
  			UNION ALL
  		
  
  			SELECT
  				person_id
  				, DATEADD(day,0,end_date) as end_date
  				, 1 AS event_type
  				, NULL
  			FROM #cohort_rows
  		) RAWDATA
  	) e
  	WHERE (2 * e.start_ordinal) - e.overall_ord = 0
  ),
  cteEnds (person_id, start_date, end_date) AS
  (
  	SELECT
  		 c.person_id
  		, c.start_date
  		, MIN(e.end_date) AS end_date
  	FROM #cohort_rows c
  	JOIN cteEndDates e ON c.person_id = e.person_id AND e.end_date >= c.start_date
  	GROUP BY c.person_id, c.start_date
  )
  select person_id, min(start_date) as start_date, end_date
  into #final_cohort
  from cteEnds
  group by person_id, end_date
  ;
  
  DELETE FROM @target_database_schema.@target_cohort_table where cohort_definition_id = @target_cohort_id;
  INSERT INTO @target_database_schema.@target_cohort_table (cohort_definition_id, subject_id, cohort_start_date, cohort_end_date)
  select @target_cohort_id as cohort_definition_id, person_id, start_date, end_date 
  FROM #final_cohort CO
  ;
  
  
  
  TRUNCATE TABLE #strategy_ends;
  DROP TABLE #strategy_ends;
  
  
  TRUNCATE TABLE #cohort_rows;
  DROP TABLE #cohort_rows;
  
  TRUNCATE TABLE #final_cohort;
  DROP TABLE #final_cohort;
  
  TRUNCATE TABLE #inclusion_events;
  DROP TABLE #inclusion_events;
  
  TRUNCATE TABLE #qualified_events;
  DROP TABLE #qualified_events;
  
  TRUNCATE TABLE #included_events;
  DROP TABLE #included_events;
  
  TRUNCATE TABLE #Codesets;
  DROP TABLE #Codesets;