Start by defining the two 7-day windows relative to the latest order date, then filter to completed, non-ghost-kitchen orders under 10 miles. Use a CTE to compute per-cuisine medians for each window via PERCENTILE_CONT or a window-function rank approach, then join and calculate percent change while applying the 30-delivery threshold on the recent window.
Pro tip: Explicitly state your median method (e.g., PERCENTILE_CONT(0.5) WITHIN GROUP) and note that it interpolates for even counts, which is standard for delivery-time metrics; also clarify how you handle ties in the rank-based alternative to avoid off-by-one errors.
Anchor the recent window to the max order date (or CURRENT_DATE) and define the prior window as the preceding 7 days. Apply filters for completed status, distance < 10 miles, and non-ghost-kitchen in a base CTE.
Use PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY delivery_minutes) grouped by cuisine and window, or a rank-based approach with ROW_NUMBER/AVG to handle ties. Keep the two windows separate or unioned with a window label.
Join recent and prior medians on cuisine (use LEFT JOIN to keep recent cuisines), then filter to cuisines with at least 30 qualifying deliveries in the recent window.
Compute percent change as (recent_median - prior_median) / prior_median * 100, handling NULL prior medians. Select cuisine, both medians, percent change, and recent delivery count.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.