This one had way more moving parts than it looked.
Use a CTE to aggregate daily impressions and clicks per advertiser, filtering out flagged ads, then compute 7-day sums for the current and prior windows using date ranges relative to the latest date in the data. Join the two windows, enforce the 10k impression threshold on both, compute CTRs with proper casting to avoid integer division, and calculate the relative drop, ordering by drop descending.
Pro tip: Explicitly state your assumptions about the date windows (e.g., whether the most recent 7 days are based on the max date in the data or the current date) and how you handle missing days—this shows you think about data completeness and edge cases.
Filter out flagged suspicious ads and aggregate impressions and clicks per advertiser per day. Ensure missing days are treated as zero by using a left join with a date spine or by summing over a date range.
Use conditional aggregation or separate CTEs to calculate total impressions and clicks for the most recent 7-day window and the prior 7-day window. Define the windows based on the latest date in the data.
Join the two window aggregates, filter to advertisers with at least 10,000 impressions in both windows, and compute CTR as clicks divided by impressions, casting to float to avoid integer division.
Compute the relative drop as (prior_ctr - current_ctr) / prior_ctr, guarding against division by zero (e.g., using NULLIF). Filter to drops >= 0.20 and order by drop descending.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.