Clinical Trials & Research Analytics Path · Mission 10 of 25Medium

SAE rate per site

Two-CTE pattern: numerator (SAE counts) and denominator (enrollment counts) computed separately, joined on site_id, with float-promoted ratio. Practice the canonical 'rate per X' shape that drives every safety, quality, and risk-adjusted comparison report.

The Brief

Marcus WebbDrug Safety Analystslack-dm

Pharmacovigilance Q2 report needs SAE rate per site. For every site in dim_sites, count serious AEs (serious_flag = 1) and divide by total enrollments at that site. Output site_name, saes (with COALESCE to 0 for sites with no SAEs), enroll_count, and sae_rate_pct (100.0 * saes / enroll_count, rounded to 1 decimal). Sort by sae_rate_pct DESC, site_name ASC. Use a two-CTE pattern — one for SAE counts, one for enrollment totals — then LEFT JOIN sae onto the site list so sites with zero SAEs still show up at 0%. Path is fact_adverse_events → fact_enrollments (on enrollment_id) for the SAE site attribution.

You'll practice

Two-CTE patternLEFT JOIN + COALESCERate metric

Tables & columns available

fact_adverse_eventsfact8 columns
ColumnTypeKey
ae_idINTPK
enrollment_idINTFK → fact_enrollments
event_dateTEXT
meddra_termTEXT
severityINT
serious_flagINT
related_to_interventionTEXT
outcomeTEXT
fact_enrollmentsfact8 columns
ColumnTypeKey
enrollment_idINTPK
trial_idINTFK → dim_trials
protocol_idINTFK → dim_protocols
patient_idINTFK → dim_trial_patients
site_idINTFK → dim_sites
enrolled_dateTEXT
statusTEXT
withdrawal_reasonTEXT
dim_sitesdim9 columns
ColumnTypeKey
site_idINTPK
site_nameTEXT
cityTEXT
stateTEXT
regionTEXT
site_typeTEXT
enrollment_capacityINT
pi_nameTEXT
activated_dateTEXT

Hints (3)

Each hint you reveal reduces the XP you can earn. Try the query first.

Hint 1

Two-CTE pattern. First CTE = SAE numerator per site (filter serious_flag, count, group by site). Second CTE = enrollment denominator per site (group by site, count). The CTEs reach the site through the enrollment table; AEs don't have a direct site FK.

Hint 2

Final SELECT joins the site dimension to the totals CTE (inner) and the SAE CTE (LEFT — zero-SAE sites still need to appear). Use COALESCE on the SAE count to render NULL as 0.

Hint 3

Float-promote the percentage. Sort by rate descending with site name as the alphabetic tiebreaker. One row per site in the dimension.