-- Generated by dplyr
SELECT
  subject_id,
  MIN(date_part(year, cohort_start_date) - year_of_birth) AS age
FROM (
  SELECT
    LHS.*,
    gender_concept_id,
    year_of_birth,
    month_of_birth,
    day_of_birth,
    birth_datetime,
    race_concept_id,
    ethnicity_concept_id,
    location_id,
    provider_id,
    care_site_id,
    person_source_value,
    gender_source_value,
    gender_source_concept_id,
    race_source_value,
    race_source_concept_id,
    ethnicity_source_value,
    ethnicity_source_concept_id
  FROM (
    SELECT cohort_table.*
    FROM @resultSchema.cohort_table
    WHERE (cohort_definition_id != @targetCohortId)
  ) LHS
  INNER JOIN @cdmSchema.person
  ON (LHS.subject_id = person.person_id)
) q01
GROUP BY subject_id;
