Compute active ad revenue by creation source
MetaData Scientist · OnsiteLast updated
This question evaluates proficiency in data manipulation and analytics, testing skills such as joining event and impression tables, aggregating time-series revenue, and counting distinct active ads broken down by creation source.
You work on an ads platform and need to report active ad revenue broken down by the ad’s creation source.
Tables
ads
ad_idBIGINT PKadvertiser_idBIGINTcreation_sourceVARCHAR Examples:'SELF_SERVE','MANAGED','API'created_atTIMESTAMP
ad_impressions
impression_idBIGINT PKad_idBIGINT FK → ads.ad_idimpression_timeTIMESTAMP
ad_revenue_events
event_idBIGINT PKad_idBIGINT FK → ads.ad_idevent_timeTIMESTAMPrevenue_usdDECIMAL(18,6) (Assume revenue is recorded at the time it is earned, e.g., per impression/click.)
Definitions / assumptions
- Timezone: UTC.
- A paid ad is active on a day if it has ≥ 1 impression that day.
- Active ad revenue on a day = sum of
revenue_usdfromad_revenue_eventsthat occurred that day for ads that are active that same day.
Task
Write a SQL query to compute, for each calendar day in a given date range (e.g., :start_date to :end_date, inclusive):
report_datecreation_sourceactive_ads(count of distinct activead_id)active_ad_revenue_usd(sum of revenue for active ads)
Order results by report_date, then creation_source.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.