Meta Ads analysts want to compare campaign performance using the median click-through rate (CTR) across daily delivery records. Write a PostgreSQL query to return the median CTR for each ad campaign using window functions.
CTR for a row is defined as clicks::numeric / impressions. Rows with impressions = 0 or impressions IS NULL should be excluded from the median calculation.
ad_delivery.campaign_id, calculate the median CTR using window functions rather than PERCENTILE_CONT.campaign_id, campaign_name, and median_ctr, ordered by campaign_id.| Column | Type | Description |
|---|---|---|
| campaign_idPK | INT | Unique campaign identifier |
| campaign_name | VARCHAR(100) | Campaign name in Meta Ads Manager |
| objective | VARCHAR(50) | Campaign objective |
| Column | Type | Description |
|---|---|---|
| delivery_idPK | INT | Unique delivery record |
| campaign_id | INT | Campaign identifier |
| delivery_date | DATE | Daily delivery date |
| impressions | INT | Number of impressions served |
| clicks | INT | Number of clicks received |