Aggregate Event Signal Quality Across Advertiser Pixels
SQL & DataModel answerLast updated
Compute pixel-level signal metrics aggregated to advertiser-day grain, then correlate data quality scores with conversion and revenue performance.
You are building analytics for a digital advertising platform's event tracking system. All timestamps use UTC. Your analysis covers the most recent 30 complete calendar days (excluding today).
Schema
advertisers
advertiser_idBIGINTverticalSTRINGcreated_atTIMESTAMP
pixels
pixel_idBIGINTadvertiser_idBIGINTinstalled_atTIMESTAMP
pixel_signal_daily
dsDATEpixel_idBIGINTevents_receivedBIGINTvalid_event_rateDOUBLE — fraction of received events passing validation (0.0 to 1.0)match_rateDOUBLE — fraction of valid events successfully matched to ad traffic (0.0 to 1.0)
ad_performance_daily
dsDATEadvertiser_idBIGINTimpressionsBIGINTclicksBIGINTspend_usdDOUBLEattributed_conversionsBIGINTattributed_revenue_usdDOUBLE
Relationships
- Each advertiser may have multiple pixels
- Each pixel generates daily signal records
- Each advertiser generates daily performance records
Part 1: Daily Advertiser Signal Summary
Write a query returning one row per advertiser per day with:
dsadvertiser_idactive_pixels— count of pixels that received ≥1 event that daytotal_events_receivedavg_valid_event_rate— event-weighted average ofvalid_event_rateacross active pixels
Part 2: Quality Correlation Analysis
Investigate how signal data quality relates to advertising outcomes.
Definitions:
pixel_day_quality_score=valid_event_rate×match_rateadvertiser_day_quality= event-weighted average ofpixel_day_quality_scoreacross all active pixels for that advertiser-dayCVR=attributed_conversions/clicks(handle division by zero)ROAS=attributed_revenue_usd/spend_usd(handle division by zero)
Quality buckets:
'low'whenadvertiser_day_quality< 0.60'medium'when 0.60 ≤advertiser_day_quality< 0.85'high'whenadvertiser_day_quality≥ 0.85
Return one row per quality bucket across the 30-day window:
quality_bucketadvertiser_day_countavg_quality_scoretotal_spend_usdtotal_clickstotal_conversionscvrroas
Hints
Hint 1
Aggregate pixel-level data to advertiser-day grain before joining with performance data to avoid fan-out multiplication
Hint 2
Event-weighted averages require SUM(events × metric) / SUM(events), not AVG(metric)
Hint 3
Compute CVR and ROAS as ratios of bucket-level totals, not averages of daily ratios
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Solution Approach
The core strategy is aggregate before joining. Both parts require collapsing pixel-grain data to advertiser-day grain before combining with performance metrics.
Part 1: Advertiser-Day Signal Summary
WITH active_pixel_days AS (
SELECT
s.ds,
p.advertiser_id,
s.pixel_id,
s.events_received,
s.valid_event_rate
FROM pixel_signal_daily s
INNER JOIN pixels p ON s.pixel_id = p.pixel_id
WHERE s.ds >= CURRENT_DATE - INTERVAL '30' DAY
AND s.ds < CURRENT_DATE
AND s.events_received > 0
)
SELECT
ds,
advertiser_id,
COUNT(DISTINCT pixel_id) AS active_pixels,
SUM(events_received) AS total_events_received,
SUM(events_received * valid_event_rate) /
NULLIF(SUM(CASE WHEN valid_event_rate IS NOT NULL
THEN events_received END), 0) AS avg_valid_event_rate
FROM active_pixel_days
GROUP BY ds, advertiser_id
ORDER BY ds, advertiser_id;
Key decisions:
- Filter to
events_received > 0to identify truly active pixels - Event-weighted average requires
SUM(events × rate) / SUM(events), notAVG(rate). A pixel processing 1 million events should dominate the average over a pixel processing 10 events. - The denominator excludes events where
valid_event_rateIS NULL to avoid incorrectly treating missing data as zero quality
Part 2: Quality-Performance Correlation
WITH advertiser_quality AS (
SELECT
s.ds,
p.advertiser_id,
SUM(s.events_received * s.valid_event_rate * s.match_rate) /
NULLIF(SUM(CASE WHEN s.valid_event_rate IS NOT NULL
AND s.match_rate IS NOT NULL
THEN s.events_received END), 0) AS quality_score
FROM pixel_signal_daily s
INNER JOIN pixels p ON s.pixel_id = p.pixel_id
WHERE s.ds >= CURRENT_DATE - INTERVAL '30' DAY
AND s.ds < CURRENT_DATE
AND s.events_received > 0
GROUP BY s.ds, p.advertiser_id
),
performance_agg AS (
SELECT
ds,
advertiser_id,
SUM(clicks) AS clicks,
SUM(spend_usd) AS spend_usd,
SUM(attributed_conversions) AS conversions,
SUM(attributed_revenue_usd) AS revenue_usd
FROM ad_performance_daily
WHERE ds >= CURRENT_DATE - INTERVAL '30' DAY
AND ds < CURRENT_DATE
GROUP BY ds, advertiser_id
),
combined AS (
SELECT
q.ds,
q.advertiser_id,
q.quality_score,
CASE
WHEN q.quality_score < 0.60 THEN 'low'
WHEN q.quality_score < 0.85 THEN 'medium'
ELSE 'high'
END AS quality_bucket,
p.clicks,
p.spend_usd,
p.conversions,
p.revenue_usd
FROM advertiser_quality q
INNER JOIN performance_agg p
ON q.ds = p.ds AND q.advertiser_id = p.advertiser_id
WHERE q.quality_score IS NOT NULL
)
SELECT
quality_bucket,
COUNT(*) AS advertiser_day_count,
AVG(quality_score) AS avg_quality_score,
SUM(spend_usd) AS total_spend_usd,
SUM(clicks) AS total_clicks,
SUM(conversions) AS total_conversions,
CAST(SUM(conversions) AS DOUBLE) / NULLIF(SUM(clicks), 0) AS cvr,
SUM(revenue_usd) / NULLIF(SUM(spend_usd), 0) AS roas
FROM combined
GROUP BY quality_bucket
ORDER BY CASE quality_bucket
WHEN 'low' THEN 1
WHEN 'medium' THEN 2
ELSE 3 END;
Critical implementation details:
Preventing fan-out: Joining pixel_signal_daily directly to ad_performance_daily on (ds, advertiser_id) creates a many-to-many join. An advertiser with 5 pixels would have spend counted 5 times. Pre-aggregating to advertiser-day grain guarantees 1:1 joins.
Correct weighting: Quality is computed as the product valid_event_rate × match_rate per pixel-day, then weighted by event volume. You cannot compute separate averages and multiply them because E[X × Y] ≠ E[X] × E[Y].
Pooled ratios for CVR and ROAS: Computing SUM(conversions) / SUM(clicks) gives the correct bucket-level CVR. Computing AVG(daily_cvr) would give equal weight to a day with 3 clicks and a day with 300,000 clicks, which is incorrect for business analysis.
Bucket boundaries: The CASE expression evaluates top-to-bottom. When quality_score = 0.85, the first condition fails and the second succeeds, placing it correctly in 'high'.
Complexity
- Time: O(P + A) where P = pixel-day rows and A = advertiser-day performance rows. Modern query engines use partition pruning on
ds, making scans very efficient. Hash joins and aggregations are linear in filtered data size. - Space: O(distinct advertiser-days) for intermediate CTEs. Final output has exactly 3 rows.
Edge Cases to Address
Inactive pixels: Rows with events_received = 0 are excluded. Including them would inflate active_pixels counts without contributing signal.
Missing performance data: The INNER JOIN excludes advertiser-days with signal but no ad spend. If the business wants to see these (perhaps as a 'no_spend' category), switch to a LEFT JOIN and add conditional bucketing.
Missing signal data: Advertiser-days with ad spend but no active pixels are excluded by the INNER JOIN. This is correct since quality metrics are undefined without signal data.
NULL rates: The denominator in weighted averages only counts events where both rate fields are non-NULL, preventing NULL propagation from distorting averages.
Division by zero: NULLIF prevents errors when a bucket has zero clicks or zero spend, returning NULL for undefined ratios.
This query structure is production-ready and handles the typical data quality issues found in real advertising platforms.