-- Sample queries for the ClinicalTrials.gov version-history sample dataset (DuckDB). -- -- Run them from the directory that holds the sample files (one folder per table, plus -- setup.sql). In the DuckDB CLI: -- .read setup.sql -- .read sample_queries.sql -- setup.sql creates one view per table, named after its folder ("core", "interventions", -- "results_outcome_measures", ...). -- -- Reading the data: -- * The dataset is a version history: nct_version numbers a trial's versions from 0, and -- nearly every table has one set of rows per (nct_id, nct_version). -- * There is no "current" flag. The current record is the highest nct_version held for an -- nct_id. That is the latest version in this dataset, which can be older than the live -- registry record if the trial has changed since it was last collected. -- * Every table ends with is_obsolete / obsolete_by: true when ClinicalTrials.gov now -- resolves the ID to a different registration. -- -- Each query starts with "-- name:" and "-- level:" lines; the blocks are independent. -- name: current_record -- level: basic -- The current record of every trial: one row per nct_id, its highest nct_version. SELECT nct_id, nct_version, overall_status, lead_sponsor_name, start_date, primary_completion_date, enrollment_count, last_update_post_date FROM core QUALIFY row_number() OVER (PARTITION BY nct_id ORDER BY nct_version DESC) = 1 ORDER BY nct_id; -- name: versions_per_trial -- level: basic -- How many versions each trial has, and the span of dates they were posted. SELECT nct_id, count(*) AS versions, min(last_update_post_date) AS first_posted, max(last_update_post_date) AS last_posted, count(DISTINCT overall_status) AS distinct_statuses FROM core GROUP BY nct_id ORDER BY versions DESC; -- name: status_changes_for_one_trial -- level: basic -- Every version of one trial in which the overall status changed. WITH v AS ( SELECT nct_id, nct_version, last_update_post_date, overall_status, lag(overall_status) OVER (PARTITION BY nct_id ORDER BY nct_version) AS previous_status FROM core WHERE nct_id = 'NCT03709680' ) SELECT nct_version, last_update_post_date, previous_status, overall_status FROM v WHERE previous_status IS DISTINCT FROM overall_status ORDER BY nct_version; -- name: current_interventions -- level: basic -- Interventions of each trial's current version (core joined to interventions on the -- two-column version key). WITH current AS ( SELECT nct_id, max(nct_version) AS nct_version FROM core GROUP BY nct_id ) SELECT c.nct_id, c.nct_version, i.intervention_type, i.intervention_name FROM current AS cur INNER JOIN core AS c ON c.nct_id = cur.nct_id AND c.nct_version = cur.nct_version INNER JOIN interventions AS i ON i.nct_id = c.nct_id AND i.nct_version = c.nct_version ORDER BY c.nct_id, i.intervention_type, i.intervention_name; -- name: current_trials_by_phase_and_status -- level: basic -- Current trials by phase and overall status. A trial with several phases (for example -- PHASE2 and PHASE3) counts once per phase. WITH current AS ( SELECT nct_id, max(nct_version) AS nct_version FROM core GROUP BY nct_id ) SELECT p.phase, c.overall_status, count(*) AS trials FROM current AS cur INNER JOIN core AS c ON c.nct_id = cur.nct_id AND c.nct_version = cur.nct_version INNER JOIN phases AS p ON p.nct_id = c.nct_id AND p.nct_version = c.nct_version GROUP BY p.phase, c.overall_status ORDER BY p.phase, trials DESC; -- name: trial_profile -- level: advanced -- One row per trial combining six tables at the current version. Each child table is -- aggregated to one row per trial first, so the joins cannot multiply rows. WITH current AS ( SELECT nct_id, max(nct_version) AS nct_version FROM core GROUP BY nct_id ), phase_agg AS ( SELECT nct_id, nct_version, string_agg(phase, ', ' ORDER BY phase) AS phases FROM phases GROUP BY nct_id, nct_version ), condition_agg AS ( SELECT nct_id, nct_version, count(*) AS conditions, string_agg(condition, '; ' ORDER BY condition) AS condition_list FROM conditions GROUP BY nct_id, nct_version ), intervention_agg AS ( SELECT nct_id, nct_version, count(*) AS interventions, string_agg(DISTINCT intervention_type, ', ') AS intervention_types FROM interventions GROUP BY nct_id, nct_version ), location_agg AS ( SELECT nct_id, nct_version, count(*) AS sites, count(DISTINCT country) AS countries FROM locations GROUP BY nct_id, nct_version ), collaborator_agg AS ( SELECT nct_id, nct_version, count(*) AS collaborators FROM collaborators GROUP BY nct_id, nct_version ) SELECT c.nct_id, c.overall_status, c.lead_sponsor_name, ph.phases, c.enrollment_count, cd.conditions, i.interventions, i.intervention_types, coalesce(l.sites, 0) AS sites, coalesce(l.countries, 0) AS countries, coalesce(co.collaborators, 0) AS collaborators FROM current AS cur INNER JOIN core AS c ON c.nct_id = cur.nct_id AND c.nct_version = cur.nct_version LEFT JOIN phase_agg AS ph ON ph.nct_id = c.nct_id AND ph.nct_version = c.nct_version LEFT JOIN condition_agg AS cd ON cd.nct_id = c.nct_id AND cd.nct_version = c.nct_version LEFT JOIN intervention_agg AS i ON i.nct_id = c.nct_id AND i.nct_version = c.nct_version LEFT JOIN location_agg AS l ON l.nct_id = c.nct_id AND l.nct_version = c.nct_version LEFT JOIN collaborator_agg AS co ON co.nct_id = c.nct_id AND co.nct_version = c.nct_version ORDER BY c.nct_id; -- name: registry_as_of_date -- level: advanced -- What the registry said about each trial on a past date: the latest version posted on or -- before that date, with its conditions, compared with today's status. Change as_of below. WITH params AS ( SELECT DATE '2019-01-01' AS as_of ), as_of_version AS ( SELECT c.nct_id, c.nct_version FROM core AS c CROSS JOIN params AS p WHERE c.last_update_post_date <= p.as_of QUALIFY row_number() OVER (PARTITION BY c.nct_id ORDER BY c.nct_version DESC) = 1 ), current AS ( SELECT nct_id, max(nct_version) AS nct_version FROM core GROUP BY nct_id ), condition_agg AS ( SELECT nct_id, nct_version, string_agg(condition, '; ' ORDER BY condition) AS conditions FROM conditions GROUP BY nct_id, nct_version ) SELECT then_.nct_id, then_.nct_version AS version_then, then_.overall_status AS status_then, then_.primary_completion_date AS primary_completion_then, cd.conditions AS conditions_then, now_.nct_version AS version_now, now_.overall_status AS status_now, now_.primary_completion_date AS primary_completion_now FROM as_of_version AS a INNER JOIN core AS then_ ON then_.nct_id = a.nct_id AND then_.nct_version = a.nct_version LEFT JOIN condition_agg AS cd ON cd.nct_id = then_.nct_id AND cd.nct_version = then_.nct_version INNER JOIN current AS cur ON cur.nct_id = then_.nct_id INNER JOIN core AS now_ ON now_.nct_id = cur.nct_id AND now_.nct_version = cur.nct_version ORDER BY then_.nct_id; -- name: primary_completion_date_slips_by_phase -- level: advanced -- How often sponsors push out a trial's primary completion date, by phase. Each version is -- compared with the one before it (core), dated from version_history (ct_gov_version equals -- nct_version) and attributed to the phases of the version that made the change. -- A trial with several phases counts once per phase. Days are gross: a date moved earlier -- and later again counts each push. WITH dated AS ( SELECT c.nct_id, c.nct_version, c.primary_completion_date, lag(c.primary_completion_date) OVER (PARTITION BY c.nct_id ORDER BY c.nct_version) AS previous_date, h.change_date FROM core AS c INNER JOIN version_history AS h ON h.nct_id = c.nct_id AND h.ct_gov_version = c.nct_version ), slips AS ( SELECT *, date_diff('day', previous_date, primary_completion_date) AS slip_days FROM dated WHERE primary_completion_date > previous_date ) SELECT p.phase, count(DISTINCT s.nct_id) AS trials_pushed, count(*) AS pushes, round(avg(s.slip_days)) AS avg_days_per_push, max(s.slip_days) AS longest_push_days, min(s.change_date) AS first_push_posted, max(s.change_date) AS last_push_posted FROM slips AS s INNER JOIN phases AS p ON p.nct_id = s.nct_id AND p.nct_version = s.nct_version GROUP BY p.phase ORDER BY pushes DESC; -- name: primary_outcome_results -- level: advanced -- Reported results for primary outcomes at the current version: outcome, group, category -- and measured value. Joins five tables through the outcome / class / category ordinals. WITH current AS ( SELECT nct_id, max(nct_version) AS nct_version FROM core GROUP BY nct_id ) SELECT om.nct_id, om.title AS outcome, om.unit_of_measure, g.title AS group_title, cat.title AS category, ms.value, ms.spread FROM current AS cur INNER JOIN results_outcome_measures AS om ON om.nct_id = cur.nct_id AND om.nct_version = cur.nct_version AND om.type = 'PRIMARY' INNER JOIN results_outcome_classes AS cl ON cl.nct_id = om.nct_id AND cl.nct_version = om.nct_version AND cl.outcome_ord = om.outcome_ord INNER JOIN results_outcome_categories AS cat ON cat.nct_id = cl.nct_id AND cat.nct_version = cl.nct_version AND cat.outcome_ord = cl.outcome_ord AND cat.class_ord = cl.class_ord INNER JOIN results_outcome_measurements AS ms ON ms.nct_id = cat.nct_id AND ms.nct_version = cat.nct_version AND ms.outcome_ord = cat.outcome_ord AND ms.class_ord = cat.class_ord AND ms.category_ord = cat.category_ord INNER JOIN results_outcome_groups AS g ON g.nct_id = ms.nct_id AND g.nct_version = ms.nct_version AND g.outcome_ord = ms.outcome_ord AND g.group_id = ms.group_id ORDER BY om.nct_id, om.outcome_ord, cl.class_ord, cat.category_ord, g.group_id; -- name: serious_adverse_event_rates_by_group -- level: advanced -- Serious adverse events at the current version: for each trial, arm group and event term, -- how many participants were affected out of those at risk (events, event stats and groups). WITH current AS ( SELECT nct_id, max(nct_version) AS nct_version FROM core GROUP BY nct_id ) SELECT e.nct_id, g.title AS group_title, e.organ_system, e.term, s.num_affected, s.num_at_risk, round(100.0 * s.num_affected / nullif(s.num_at_risk, 0), 2) AS pct_affected FROM current AS cur INNER JOIN results_adverse_serious_events AS e ON e.nct_id = cur.nct_id AND e.nct_version = cur.nct_version INNER JOIN results_adverse_serious_event_stats AS s ON s.nct_id = e.nct_id AND s.nct_version = e.nct_version AND s.event_ord = e.event_ord INNER JOIN results_adverse_event_groups AS g ON g.nct_id = s.nct_id AND g.nct_version = s.nct_version AND g.group_id = s.group_id WHERE s.num_affected > 0 ORDER BY pct_affected DESC, e.nct_id, e.term LIMIT 50; -- name: arms_and_their_interventions -- level: advanced -- Which intervention each study arm receives, at the current version (arm_groups joined to -- the arm/intervention link table and to interventions for the description). WITH current AS ( SELECT nct_id, max(nct_version) AS nct_version FROM core GROUP BY nct_id ) SELECT a.nct_id, a.arm_label, a.arm_type, i.intervention_type, i.intervention_name FROM current AS cur INNER JOIN arm_groups AS a ON a.nct_id = cur.nct_id AND a.nct_version = cur.nct_version INNER JOIN intervention_arm_links AS l ON l.nct_id = a.nct_id AND l.nct_version = a.nct_version AND l.arm_label = a.arm_label INNER JOIN interventions AS i ON i.nct_id = l.nct_id AND i.nct_version = l.nct_version AND i.intervention_name = l.intervention_name AND i.intervention_type = l.intervention_type ORDER BY a.nct_id, a.arm_label, i.intervention_name; -- name: sponsor_site_footprint -- level: advanced -- Each lead sponsor's current trials and where they run: trials, sites and countries -- (core joined to locations at the current version). WITH current AS ( SELECT nct_id, max(nct_version) AS nct_version FROM core GROUP BY nct_id ) SELECT c.lead_sponsor_name, count(DISTINCT c.nct_id) AS trials, count(*) AS sites, count(DISTINCT l.country) AS countries FROM current AS cur INNER JOIN core AS c ON c.nct_id = cur.nct_id AND c.nct_version = cur.nct_version INNER JOIN locations AS l ON l.nct_id = c.nct_id AND l.nct_version = c.nct_version GROUP BY c.lead_sponsor_name ORDER BY sites DESC;