I started writing a straightforward group-by and got maybe 60% of the way there before realizing I'd completely forgotten to generate the date spine.
Start by generating a complete date series for the last 7 UTC days using generate_series, then left join aggregated call metrics to ensure all dates appear. Compute metrics with conditional aggregation: count calls for initiated, average of completed indicator for completion rate, average of dropped indicator for drop rate, and percentile_cont(0.5) within group for median duration, filtering out employee calls and zero/negative durations for median.
Pro tip: Mention that you'd verify the query's performance with EXPLAIN ANALYZE and consider indexing on (start_time, caller_is_employee, callee_is_employee) to avoid full scans, showing you think about production readiness.
Use generate_series to create exactly 7 rows for the last 7 UTC calendar dates, ensuring no gaps.
Restrict calls to those where neither caller nor callee is an employee, and derive the call date from start_time in UTC.
For each date, compute calls initiated (count), completion rate (avg of completed flag), drop rate (avg of dropped flag), and median duration using percentile_cont(0.5) over completed calls with duration > 0.
Left join the date spine to the aggregated metrics, using COALESCE to replace nulls with zeros for counts and rates, and leave median as null if no completed calls.
Ensure the final query returns exactly 7 rows with correct column names and data types, and mentally test edge cases like days with no calls.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.