The DATE_TRUNC part was fine, that's muscle memory at this point.
Start by aggregating revenue per calendar week using a date truncation function, then use a window function like LAG to access the prior week's revenue. Compute the percentage change with a safe division to handle nulls or zero values, and order the final result by week.
Pro tip: Explicitly discuss how you would handle edge cases like the first week (no prior week) and weeks with zero revenue, and mention that you'd validate the query against a small sample to ensure correctness.
Use DATE_TRUNC or equivalent to group transactions into calendar weeks and sum revenue for each week.
Apply the LAG window function over the weekly revenue, ordered by week, to get the previous week's total.
Calculate (current_revenue - prior_revenue) / prior_revenue * 100, using NULLIF or CASE to avoid division by zero.
Select the week and percentage change, order by week ascending, and optionally round the percentage for readability.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.