                                                                                                                                                                                                                                                                                                      ?column?                                                                                                                                                                                                                                                                                                       
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 CREATE OR REPLACE VIEW ctgov.all_browse_conditions AS  SELECT browse_conditions.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
     array_to_string(array_agg(DISTINCT browse_conditions.mesh_term), '|'::text) AS names                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
    FROM ctgov.browse_conditions                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
   GROUP BY browse_conditions.nct_id;
 CREATE OR REPLACE VIEW ctgov.all_browse_interventions AS  SELECT browse_interventions.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
     array_to_string(array_agg(browse_interventions.mesh_term), '|'::text) AS names                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
    FROM ctgov.browse_interventions                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
   GROUP BY browse_interventions.nct_id;
 CREATE OR REPLACE VIEW ctgov.all_cities AS  SELECT facilities.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
     array_to_string(array_agg(DISTINCT facilities.city), '|'::text) AS names                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
    FROM ctgov.facilities                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
   GROUP BY facilities.nct_id;
 CREATE OR REPLACE VIEW ctgov.all_conditions AS  SELECT conditions.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
     array_to_string(array_agg(DISTINCT conditions.name), '|'::text) AS names                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
    FROM ctgov.conditions                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
   GROUP BY conditions.nct_id;
 CREATE OR REPLACE VIEW ctgov.all_countries AS  SELECT countries.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
     array_to_string(array_agg(DISTINCT countries.name), '|'::text) AS names                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
    FROM ctgov.countries                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
   WHERE (countries.removed IS NOT TRUE)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
   GROUP BY countries.nct_id;
 CREATE OR REPLACE VIEW ctgov.all_design_outcomes AS  SELECT design_outcomes.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
     array_to_string(array_agg(DISTINCT design_outcomes.measure), '|'::text) AS names                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               +
    FROM ctgov.design_outcomes                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
   GROUP BY design_outcomes.nct_id;
 CREATE OR REPLACE VIEW ctgov.all_facilities AS  SELECT facilities.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
     array_to_string(array_agg(facilities.name), '|'::text) AS names                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
    FROM ctgov.facilities                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
   GROUP BY facilities.nct_id;
 CREATE OR REPLACE VIEW ctgov.all_group_types AS  SELECT design_groups.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
     array_to_string(array_agg(DISTINCT design_groups.group_type), '|'::text) AS names                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
    FROM ctgov.design_groups                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
   GROUP BY design_groups.nct_id;
 CREATE OR REPLACE VIEW ctgov.all_id_information AS  SELECT id_information.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     array_to_string(array_agg(DISTINCT id_information.id_value), '|'::text) AS names                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               +
    FROM ctgov.id_information                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
   GROUP BY id_information.nct_id;
 CREATE OR REPLACE VIEW ctgov.all_intervention_types AS  SELECT interventions.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               +
     array_to_string(array_agg(interventions.intervention_type), '|'::text) AS names                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
    FROM ctgov.interventions                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
   GROUP BY interventions.nct_id;
 CREATE OR REPLACE VIEW ctgov.all_interventions AS  SELECT interventions.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
     array_to_string(array_agg(interventions.name), '|'::text) AS names                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
    FROM ctgov.interventions                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
   GROUP BY interventions.nct_id;
 CREATE OR REPLACE VIEW ctgov.all_keywords AS  SELECT keywords.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
     array_to_string(array_agg(DISTINCT keywords.name), '|'::text) AS names                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
    FROM ctgov.keywords                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
   GROUP BY keywords.nct_id;
 CREATE OR REPLACE VIEW ctgov.all_overall_official_affiliations AS  SELECT overall_officials.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
     array_to_string(array_agg(overall_officials.affiliation), '|'::text) AS names                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
    FROM ctgov.overall_officials                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
   GROUP BY overall_officials.nct_id;
 CREATE OR REPLACE VIEW ctgov.all_overall_officials AS  SELECT overall_officials.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
     array_to_string(array_agg(overall_officials.name), '|'::text) AS names                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
    FROM ctgov.overall_officials                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
   GROUP BY overall_officials.nct_id;
 CREATE OR REPLACE VIEW ctgov.all_primary_outcome_measures AS  SELECT design_outcomes.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
     array_to_string(array_agg(DISTINCT design_outcomes.measure), '|'::text) AS names                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               +
    FROM ctgov.design_outcomes                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
   WHERE ((design_outcomes.outcome_type)::text = 'primary'::text)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
   GROUP BY design_outcomes.nct_id;
 CREATE OR REPLACE VIEW ctgov.all_secondary_outcome_measures AS  SELECT design_outcomes.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
     array_to_string(array_agg(DISTINCT design_outcomes.measure), '|'::text) AS names                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               +
    FROM ctgov.design_outcomes                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
   WHERE ((design_outcomes.outcome_type)::text = 'secondary'::text)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
   GROUP BY design_outcomes.nct_id;
 CREATE OR REPLACE VIEW ctgov.all_sponsors AS  SELECT sponsors.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
     array_to_string(array_agg(DISTINCT sponsors.name), '|'::text) AS names                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
    FROM ctgov.sponsors                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
   GROUP BY sponsors.nct_id;
 CREATE OR REPLACE VIEW ctgov.all_states AS  SELECT facilities.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
     array_to_string(array_agg(DISTINCT facilities.state), '|'::text) AS names                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
    FROM ctgov.facilities                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
   GROUP BY facilities.nct_id;
 CREATE OR REPLACE VIEW ctgov.categories AS  SELECT search_results.id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
     search_results.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
     search_results.name,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
     search_results.created_at,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
     search_results.updated_at,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
     search_results."grouping",                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
     search_results.study_search_id                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
    FROM ctgov.search_results;
 CREATE OR REPLACE VIEW ctgov.covid_19_studies AS  SELECT s.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
     s.overall_status,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
     s.study_type,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     s.official_title,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
     s.acronym,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
     s.phase,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
     s.why_stopped,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
     s.has_dmc,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
     s.enrollment,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     s.is_fda_regulated_device,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
     s.is_fda_regulated_drug,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
     s.is_unapproved_device,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
     s.has_expanded_access,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
     s.study_first_submitted_date,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     s.last_update_posted_date,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
     s.results_first_posted_date,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     s.start_date,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     s.primary_completion_date,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
     s.completion_date,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
     s.study_first_posted_date,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
     cv.number_of_facilities,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
     cv.has_single_facility,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
     cv.nlm_download_date,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
     s.number_of_arms,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
     s.number_of_groups,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
     sp.name AS lead_sponsor,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
     aid.names AS other_ids,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
     e.gender,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
     e.gender_based,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
     e.gender_description,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
     e.population,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     e.minimum_age,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
     e.maximum_age,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
     e.criteria,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
     e.healthy_volunteers,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
     ak.names AS keywords,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
     ai.names AS interventions,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
     ac.names AS conditions,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
     d.primary_purpose,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
     d.allocation,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     d.observational_model,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
     d.intervention_model,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
     d.masking,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
     d.subject_masked,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
     d.caregiver_masked,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
     d.investigator_masked,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
     d.outcomes_assessor_masked,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
     ado.names AS design_outcomes,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     bs.description AS brief_summary,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               +
     dd.description AS detailed_description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
    FROM (((((((((((ctgov.studies s                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
      FULL JOIN ctgov.all_conditions ac ON (((s.nct_id)::text = (ac.nct_id)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
      FULL JOIN ctgov.all_id_information aid ON (((s.nct_id)::text = (aid.nct_id)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
      FULL JOIN ctgov.all_design_outcomes ado ON (((s.nct_id)::text = (ado.nct_id)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
      FULL JOIN ctgov.all_keywords ak ON (((s.nct_id)::text = (ak.nct_id)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
      FULL JOIN ctgov.all_interventions ai ON (((s.nct_id)::text = (ai.nct_id)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
      FULL JOIN ctgov.sponsors sp ON (((s.nct_id)::text = (sp.nct_id)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
      FULL JOIN ctgov.calculated_values cv ON (((s.nct_id)::text = (cv.nct_id)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
      FULL JOIN ctgov.designs d ON (((s.nct_id)::text = (d.nct_id)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
      FULL JOIN ctgov.eligibilities e ON (((s.nct_id)::text = (e.nct_id)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
      FULL JOIN ctgov.brief_summaries bs ON (((s.nct_id)::text = (bs.nct_id)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               +
      FULL JOIN ctgov.detailed_descriptions dd ON (((s.nct_id)::text = (dd.nct_id)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
   WHERE (((sp.lead_or_collaborator)::text = 'lead'::text) AND ((s.nct_id)::text IN ( SELECT search_results.nct_id                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
            FROM ctgov.search_results                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               +
           WHERE ((search_results.name)::text = 'covid-19'::text))));
 CREATE OR REPLACE VIEW history.match_drugs_to_trials AS  SELECT bi.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
     rp.rxcui,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
     rp.propvalue1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
    FROM (ctgov.browse_interventions bi                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
      JOIN rxnorm_migrated.rxnorm_props rp ON (((bi.downcase_mesh_term)::text = (rp.propvalue1)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
   WHERE (((rp.propname)::text = 'RxNorm Name'::text) AND ((bi.nct_id)::text IN ( SELECT trial_snapshots.nct_id                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
            FROM history.trial_snapshots)));
 CREATE OR REPLACE VIEW http.most_recent_download_status AS  SELECT t.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
     t.status,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
     t.update_timestamp                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
    FROM ( SELECT download_status.id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               +
             download_status.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
             download_status.status,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
             download_status.update_timestamp,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
             row_number() OVER (PARTITION BY download_status.nct_id ORDER BY download_status.update_timestamp DESC) AS rn                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
            FROM http.download_status) t                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
   WHERE (t.rn = 1)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
   ORDER BY t.nct_id;
 CREATE OR REPLACE VIEW public.time_between_submission_and_start_view AS  SELECT s.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
     s.start_date,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     ts.version,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
     ts.submission_date,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
     abs(((EXTRACT(epoch FROM (ts.submission_date - (s.start_date)::timestamp without time zone)))::double precision / (((24 * 60) * 60))::double precision)) AS start_deviance                                                                                                                                                                                                                                                                                                                                                                                                                                     +
    FROM (ctgov.studies s                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
      JOIN history.trial_snapshots ts ON (((s.nct_id)::text = (ts.nct_id)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
   WHERE ((s.nct_id)::text IN ( SELECT DISTINCT tti.nct_id                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
            FROM "DiseaseBurden".trial_to_icd10 tti));
 CREATE OR REPLACE VIEW public.rank_proximity_to_start_time_view AS  SELECT cte.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
     cte.version,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     row_number() OVER (PARTITION BY cte.nct_id ORDER BY cte.start_deviance) AS rownum,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
     cte.submission_date,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
     cte.start_deviance,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
     cte.start_date,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
     ts.primary_completion_date,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
     ts.primary_completion_date_category,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
     ts.overall_status,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
     ts.enrollment,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
     ts.enrollment_category                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
    FROM (time_between_submission_and_start_view cte                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
      JOIN history.trial_snapshots ts ON ((((cte.nct_id)::text = (ts.nct_id)::text) AND (cte.version = ts.version))));
 CREATE OR REPLACE VIEW public.enrollment_closest_to_start_view AS  SELECT cte2.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
     min(cte2.rownum) AS enrollment_source                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
    FROM rank_proximity_to_start_time_view cte2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
   WHERE (cte2.enrollment IS NOT NULL)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
   GROUP BY cte2.nct_id;
 CREATE OR REPLACE VIEW public.match_trials_to_bn_in AS  WITH trialncts AS (                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
          SELECT DISTINCT ts.nct_id                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
            FROM history.trial_snapshots ts                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
         )                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
  SELECT bi.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
     bi.downcase_mesh_term,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
     rr.tty2,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
     rr.rxcui2 AS bn_or_in_cui,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
     count(*) AS count                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
    FROM ((ctgov.browse_interventions bi                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
      LEFT JOIN rxnorm_migrated.rxnorm_props rp ON (((bi.downcase_mesh_term)::text = (rp.propvalue1)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
      LEFT JOIN rxnorm_migrated.rxnorm_relations rr ON ((rr.rxcui1 = rp.rxcui)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
   WHERE (((bi.nct_id)::text IN ( SELECT trialncts.nct_id                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
            FROM trialncts)) AND ((bi.mesh_type)::text = 'mesh-list'::text) AND ((rp.propname)::text = 'Active_ingredient_name'::text) AND (rr.tty2 = ANY (ARRAY['BN'::bpchar, 'IN'::bpchar, 'MIN'::bpchar])))                                                                                                                                                                                                                                                                                                                                                                                                      +
   GROUP BY bi.nct_id, bi.downcase_mesh_term, rr.tty2, rr.rxcui2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
   ORDER BY bi.nct_id;
 CREATE OR REPLACE VIEW public.match_trial_to_ndc11 AS  SELECT mttbi.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
     ah.ndc,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
     count(*) AS count                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
    FROM ((match_trials_to_bn_in mttbi                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
      LEFT JOIN rxnorm_migrated.rxnorm_relations rr ON ((mttbi.bn_or_in_cui = rr.rxcui1)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
      LEFT JOIN rxnorm_migrated."ALLNDC_HISTORY" ah ON ((rr.rxcui2 = ah.rxcui)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
   WHERE ((rr.tty1 = 'BN'::bpchar) AND (rr.tty2 = ANY (ARRAY['SBD'::bpchar, 'BPCK'::bpchar])) AND ((ah.sab)::text = 'RXNORM'::text))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
   GROUP BY mttbi.nct_id, ah.ndc                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
   ORDER BY mttbi.nct_id, ah.ndc;
 CREATE OR REPLACE VIEW public.match_trial_to_marketing_start_date AS  SELECT mttn.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
     n.application_number_or_citation,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
     min(n.marketing_start_date) AS min                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
    FROM (match_trial_to_ndc11 mttn                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
      JOIN spl.nsde n ON ((mttn.ndc = (n.package_ndc11)::bpchar)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
   WHERE (((n.product_type)::text = 'HUMAN PRESCRIPTION DRUG'::text) AND ((n.marketing_category)::text = ANY (ARRAY[('NDA'::character varying)::text, ('ANDA'::character varying)::text, ('BLA'::character varying)::text, ('NDA authorized generic'::character varying)::text, ('NDA AUTHORIZED GENERIC'::character varying)::text])))                                                                                                                                                                                                                                                                             +
   GROUP BY mttn.nct_id, n.application_number_or_citation                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
   ORDER BY mttn.nct_id;
 CREATE OR REPLACE VIEW public.view_burdens_cte AS  SELECT b.measure_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
     b.location_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
     b.sex_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
     b.age_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
     b.cause_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
     b.metric_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     b.year,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
     b.val,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
     b.upper_95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
     b.lower_95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
     b.key_column                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
    FROM "DiseaseBurden".burdens b                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
   WHERE ((b.sex_id = 3) AND (b.metric_id = 1) AND (b.measure_id = 2) AND (b.age_id = 22));
 CREATE OR REPLACE VIEW public.view_burdens_cte2 AS  SELECT c1.cause_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
     c1.year,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
     c1.val AS h_sdi_val,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
     c1.upper_95 AS h_sdi_u95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
     c1.lower_95 AS h_sdi_l95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
     c2.val AS hm_sdi_val,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
     c2.upper_95 AS hm_sdi_u95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
     c2.lower_95 AS hm_sdi_l95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
     c3.val AS m_sdi_val,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
     c3.upper_95 AS m_sdi_u95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
     c3.lower_95 AS m_sdi_l95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
     c4.val AS lm_sdi_val,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
     c4.upper_95 AS lm_sdi_u95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
     c4.lower_95 AS lm_sdi_l95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
     c5.val AS l_sdi_val,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
     c5.upper_95 AS l_sdi_u95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
     c5.lower_95 AS l_sdi_l95                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
    FROM ((((view_burdens_cte c1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
      JOIN view_burdens_cte c2 ON (((c1.cause_id = c2.cause_id) AND (c1.year = c2.year))))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
      JOIN view_burdens_cte c3 ON (((c1.cause_id = c3.cause_id) AND (c1.year = c3.year))))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
      JOIN view_burdens_cte c4 ON (((c1.cause_id = c4.cause_id) AND (c1.year = c4.year))))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
      JOIN view_burdens_cte c5 ON (((c1.cause_id = c5.cause_id) AND (c1.year = c5.year))))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
   WHERE ((c1.location_id = 44635) AND (c2.location_id = 44634) AND (c3.location_id = 44639) AND (c4.location_id = 44636) AND (c5.location_id = 44637));
 CREATE OR REPLACE VIEW public.view_cte AS  SELECT ts.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
     ts.primary_completion_date,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
     ts.primary_completion_date_category,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
     ts.enrollment,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
     ts.start_date,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
     ts.enrollment_category,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
     ts.overall_status,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
     min(ts.submission_date) AS earliest_date_observed                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
    FROM history.trial_snapshots ts                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
   WHERE (((ts.nct_id)::text IN ( SELECT DISTINCT tti.nct_id                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
            FROM "DiseaseBurden".trial_to_icd10 tti                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
           WHERE (tti.approved = 'accepted'::"DiseaseBurden".validation_type))) AND (ts.submission_date >= ts.start_date) AND (ts.overall_status <> ALL (ARRAY['Completed'::history.study_statuses, 'Terminated'::history.study_statuses])))                                                                                                                                                                                                                                                                                                                                                                        +
   GROUP BY ts.nct_id, ts.primary_completion_date, ts.primary_completion_date_category, ts.start_date, ts.enrollment, ts.enrollment_category, ts.overall_status;
 CREATE OR REPLACE VIEW public.view_disbur_cte0 AS  SELECT tti.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
     tti.ui,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
     tti.condition,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
     itc.cause_text,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
     ch.cause_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     ch.level                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
    FROM (("DiseaseBurden".trial_to_icd10 tti                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
      JOIN "DiseaseBurden".icd10_to_cause itc ON ((replace(replace((tti.ui)::text, '-'::text, ''::text), '.'::text, ''::text) = replace(replace((itc.code)::text, '-'::text, ''::text), '.'::text, ''::text))))                                                                                                                                                                                                                                                                                                                                                                                                     +
      JOIN "DiseaseBurden".cause_hierarchy ch ON (((itc.cause_text)::text = (ch.cause_name)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
   WHERE (tti.approved = 'accepted'::"DiseaseBurden".validation_type);
 CREATE OR REPLACE VIEW public.view_disbur_cte AS  SELECT view_disbur_cte0.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     max(view_disbur_cte0.level) AS max_level                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
    FROM view_disbur_cte0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
   GROUP BY view_disbur_cte0.nct_id;
 CREATE OR REPLACE VIEW public.view_trial_to_cause AS  SELECT tti.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
     tti.ui,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
     tti.condition,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
     itc.cause_text,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
     ch.cause_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     ch.level                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
    FROM (("DiseaseBurden".trial_to_icd10 tti                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
      JOIN "DiseaseBurden".icd10_to_cause itc ON ((replace(replace((tti.ui)::text, '-'::text, ''::text), '.'::text, ''::text) = replace(replace((itc.code)::text, '-'::text, ''::text), '.'::text, ''::text))))                                                                                                                                                                                                                                                                                                                                                                                                     +
      JOIN "DiseaseBurden".cause_hierarchy ch ON (((itc.cause_text)::text = (ch.cause_name)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
   WHERE (tti.approved = 'accepted'::"DiseaseBurden".validation_type)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               +
   ORDER BY tti.nct_id;
 CREATE OR REPLACE VIEW public.view_disbur_cte2 AS  SELECT ttc.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
     ttc.ui,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
     ttc.condition,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
     ttc.cause_text,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
     ttc.cause_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     disbur_cte.max_level                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
    FROM (view_trial_to_cause ttc                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
      JOIN view_disbur_cte disbur_cte ON (((disbur_cte.nct_id)::text = (ttc.nct_id)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
   WHERE (ttc.level = disbur_cte.max_level)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
   GROUP BY ttc.nct_id, ttc.ui, ttc.condition, ttc.cause_text, ttc.cause_id, disbur_cte.max_level                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
   ORDER BY ttc.nct_id, ttc.ui;
 CREATE OR REPLACE VIEW public.view_disbur_cte3 AS  SELECT disbur_cte2.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
     SUBSTRING(disbur_cte2.ui FROM 1 FOR 3) AS code,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
     disbur_cte2.condition,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
     disbur_cte2.cause_text,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
     disbur_cte2.cause_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
     ic.chapter_code AS category_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
     ic.group_name,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
     disbur_cte2.max_level                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
    FROM (view_disbur_cte2 disbur_cte2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
      JOIN "DiseaseBurden".icd10_categories ic ON (((SUBSTRING(disbur_cte2.ui FROM 1 FOR 3) <= (ic.end_code)::text) AND (SUBSTRING(disbur_cte2.ui FROM 1 FOR 3) >= (ic.start_code)::text))))                                                                                                                                                                                                                                                                                                                                                                                                                        +
   WHERE (ic.level = 1);
 CREATE OR REPLACE VIEW public.formatted_data AS  SELECT cte.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
     cte.start_date,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
     cte.enrollment AS current_enrollment,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
     cte.enrollment_category,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
     cte.overall_status AS current_status,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
     cte.earliest_date_observed,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
     (EXTRACT(epoch FROM (cte.earliest_date_observed - cte.start_date)) / EXTRACT(epoch FROM (cte.primary_completion_date - cte.start_date))) AS elapsed_duration,                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     count(DISTINCT mttmsd.application_number_or_citation) AS n_brands,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             +
     dbc3.code,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     +
     dbc3.condition,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
     dbc3.cause_text,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               +
     dbc3.cause_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
     dbc3.category_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
     dbc3.group_name,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               +
     dbc3.max_level,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
     b.year,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
     b.h_sdi_val,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     b.h_sdi_u95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     b.h_sdi_l95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     b.hm_sdi_val,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     b.hm_sdi_u95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     b.hm_sdi_l95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     b.m_sdi_val,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     b.m_sdi_u95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     b.m_sdi_l95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     b.lm_sdi_val,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     b.lm_sdi_u95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     b.lm_sdi_l95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     b.l_sdi_val,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     b.l_sdi_u95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     b.l_sdi_l95                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
    FROM (((view_cte cte                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
      JOIN match_trial_to_marketing_start_date mttmsd ON (((cte.nct_id)::text = (mttmsd.nct_id)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
      JOIN view_disbur_cte3 dbc3 ON (((dbc3.nct_id)::text = (cte.nct_id)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
      JOIN view_burdens_cte2 b ON (((b.cause_id = dbc3.cause_id) AND (EXTRACT(year FROM b.year) = EXTRACT(year FROM cte.earliest_date_observed)))))                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
   WHERE (mttmsd.min <= cte.earliest_date_observed)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
   GROUP BY cte.nct_id, cte.start_date, cte.enrollment, cte.enrollment_category, cte.overall_status, cte.earliest_date_observed, (EXTRACT(epoch FROM (cte.earliest_date_observed - cte.start_date)) / EXTRACT(epoch FROM (cte.primary_completion_date - cte.start_date))), dbc3.code, dbc3.condition, dbc3.cause_text, dbc3.cause_id, dbc3.category_id, dbc3.group_name, dbc3.max_level, b.cause_id, b.year, b.h_sdi_val, b.h_sdi_u95, b.h_sdi_l95, b.hm_sdi_val, b.hm_sdi_u95, b.hm_sdi_l95, b.m_sdi_val, b.m_sdi_u95, b.m_sdi_l95, b.lm_sdi_val, b.lm_sdi_u95, b.lm_sdi_l95, b.l_sdi_val, b.l_sdi_u95, b.l_sdi_l95+
   ORDER BY cte.nct_id, cte.earliest_date_observed;
 CREATE OR REPLACE VIEW public.formatted_data_with_planned_enrollment AS  SELECT f.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
     f.start_date,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     f.current_enrollment,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          +
     f.enrollment_category,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         +
     f.current_status,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
     f.earliest_date_observed,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      +
     f.elapsed_duration,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +
     f.n_brands,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
     f.code,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
     f.condition,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     f.cause_text,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     f.cause_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
     f.category_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 +
     f.group_name,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     f.max_level,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     f.year,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
     f.h_sdi_val,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     f.h_sdi_u95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     f.h_sdi_l95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     f.hm_sdi_val,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     f.hm_sdi_u95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     f.hm_sdi_l95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     f.m_sdi_val,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     f.m_sdi_u95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     f.m_sdi_l95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     f.lm_sdi_val,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     f.lm_sdi_u95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     f.lm_sdi_l95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
     f.l_sdi_val,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     f.l_sdi_u95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     f.l_sdi_l95,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     s.overall_status AS final_status,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
     c2a.version,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     c2a.enrollment AS planned_enrollment                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
    FROM (((formatted_data f                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        +
      JOIN ctgov.studies s ON (((f.nct_id)::text = (s.nct_id)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
      JOIN enrollment_closest_to_start_view c3e ON (((c3e.nct_id)::text = (f.nct_id)::text)))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
      JOIN rank_proximity_to_start_time_view c2a ON ((((c3e.nct_id)::text = (c2a.nct_id)::text) AND (c3e.enrollment_source = c2a.rownum))));
 CREATE OR REPLACE VIEW http.trials_to_download AS  SELECT most_recent_download_status.nct_id                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
    FROM http.most_recent_download_status                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
   WHERE (most_recent_download_status.status = 'Of Interest'::http.history_download_status);
 CREATE OR REPLACE VIEW public.primary_design_outcomes AS  SELECT do2.id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           +
     do2.nct_id,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    +
     do2.outcome_type,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              +
     do2.measure,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   +
     do2.time_frame,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
     do2.population,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
     do2.description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                +
    FROM ctgov.design_outcomes do2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  +
   WHERE (((do2.outcome_type)::text = 'primary'::text) AND ((do2.nct_id)::text IN ( SELECT DISTINCT fd.nct_id                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       +
            FROM formatted_data fd)));
(40 rows)

