Retail · Email
Delivering a phone-ready clean customer count summary to a stakeholder mid-meeting
Independent grading: 97/100(verify, opens in a new tab)
Problem
With the investor meeting already underway, a senior stakeholder at Meridian Retail (simulated retailer) needed a short, accurate email on his phone giving the clean customer count, one sentence explaining why that number is the right one to use, and a heads-up about the duplicate volume. The deliverable was the formatted email body itself, produced directly from SQL.
What I did
I used a subquery to derive all three counts, total, duplicates, and clean records, in a single pass using COUNT(*) FILTER (WHERE ...) for conditional aggregation. The outer query used format() with to_char() and the FM9,999 mask so the numbers appear comma-formatted and human-readable in the email body. Structuring the prose as number, then explanation, then caveat matched the stakeholder's stated reading context: a phone screen between meetings with no time for re-reading.
Finding
The clean customer count of 2,131 is the correct figure for investor use, with 200 duplicate records, roughly 9 percent of all rows, noted as the reason a higher raw total may appear elsewhere.
Outcome
The stakeholder received a single, self-contained email body with verified numbers and a clear narrative that pre-empted confusion about differing counts from other teams.
SELECT format( E'Hi Chidi,\n\nOur clean customer count is %s.\n\nIt''s the right number to use because it counts each real customer once: it starts from all %s records and removes the %s that are flagged as duplicates.\n\nHeads-up so nobody is surprised later: those %s duplicates (about %s%% of records) are why Marketing''s %s runs high, so if anyone quotes that figure, that''s the reason.\n\nRegards,\nEmmanuel ', to_char(clean, 'FM9,999'), to_char(total, 'FM9,999'), dup, dup, ROUND(100.0 * dup / total), to_char(total, 'FM9,999') ) AS email_to_chidi FROM ( SELECT COUNT(*) AS total, COUNT(*) FILTER (WHERE is_duplicate) AS dup, COUNT(*) FILTER (WHERE NOT is_duplicate) AS clean FROM retail.customers ) AS counts;
The subquery produces all three counts in one scan, and to_char with FM9,999 formatting ensures numbers are comma-separated and readable directly inside the email string.
Next step
The email flags that Marketing may quote the higher raw total, so a useful follow-up would be identifying which specific marketing reports pull from the unfiltered table so those sources can be corrected at the root.
- SQL COUNT FILTER (WHERE ...) for conditional aggregation
- SQL subquery with derived column references
- SQL format() with to_char() for readable output