First, clarify the schema and assumptions (e.g., event types, date bucketing, seller_id consistency). Then outline a SQL strategy: aggregate daily orders and complaints per seller, use a window function with ROWS BETWEEN 6 PRECEDING AND CURRENT ROW to compute rolling sums, and handle division by zero with NULLIF. Finally, address the 'ALL' row by either using GROUPING SETS or a UNION with a separate aggregate query.
Pro tip: Mention that you would validate the rolling window logic with a small test dataset, especially around boundary dates and zero-order windows, to ensure NULLs are correctly produced and the 'ALL' row aggregates properly.
Ask about the events table structure (e.g., event_type values, date column granularity) and confirm that seller_id is consistent across both tables. Confirm that the rolling window is based on calendar days and that missing dates should be treated as zero events.
Write a CTE that groups events by seller_id and date, counting orders and complaints separately. Ensure that dates with no events are included (e.g., by generating a date spine or using a LEFT JOIN from a calendar table) so that rolling windows are correct.
Use window functions SUM() OVER (PARTITION BY seller_id ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) for both orders and complaints. Then compute the ratio as complaints / NULLIF(orders, 0) to yield NULL when orders = 0.
For a specific reference date, compute the aggregate ratio across all sellers. This can be done with a separate query that sums orders and complaints over the same 7-day window for all sellers, then UNION ALL with the per-seller results, labeling seller_id as 'ALL'.
Combine the per-seller and 'ALL' results, order the output, and mentally test edge cases (e.g., zero orders, missing dates). Mention that you would verify the query on a sample dataset.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.