This took me way longer to structure than I expected.
Start by outlining a CTE-based pipeline: deduplicate raw events, compute local dates using per-user timezone offsets, aggregate daily metrics per region, then apply window functions for 7-day median and revenue drop flag. Emphasize correctness (idempotency, timezone handling) and clarity, and mention performance considerations like indexing and partitioning.
Pro tip: Mention that you'd validate the query against a small sample and check edge cases like users with missing timezone offsets or events crossing midnight boundaries, showing production readiness.
Use a CTE with DISTINCT or ROW_NUMBER() to remove duplicate events sharing user, timestamp, event type, and session. Join with a user timezone mapping to compute local date as (timestamp + offset)::date.
Group by local_date and region (US vs Asia) to compute DAU (distinct users), buyers (distinct users with purchase), revenue (sum of purchase amounts), and view-to-purchase conversion rate (purchases / views).
For each user and local_date, determine if they were active on any prior date. Use a window function like MIN(local_date) OVER (PARTITION BY user) or a self-join to flag new vs returning, then aggregate counts per region and date.
ARPPU = revenue / buyers. Use a window function with ROWS BETWEEN 6 PRECEDING AND CURRENT ROW to compute the 7-day median revenue per region (using PERCENTILE_CONT or a subquery).
Compare daily revenue to the 7-day median; flag days where revenue < 0.8 * median. Select all required columns, order by region and local_date, and ensure the query is idempotent and uses '2025-09-01' as today for any date filtering.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.