Why Your AI's 'New Customers' Is Everyone Who Ordered
Asked for new customers in December and got 400. That is everyone who placed an order in December. Forty of them placed their first order then. The other 360 have been customers for months. The first-order date was computed after the month filter.
You ask: "How many new customers in December?" The AI says 400. Four hundred people ordered in December. 40 of them had never ordered before. The other 360 are returning.
SELECT COUNT(*)
FROM (
SELECT customer_id, MIN(created_at) AS first_order
FROM orders
WHERE created_at >= TIMESTAMPTZ '2026-12-01'
AND created_at < TIMESTAMPTZ '2027-01-01'
GROUP BY customer_id
) s
MIN runs after WHERE. For each customer, the earliest order still in the query is a December order, because July was filtered out. Every December buyer looks new. The query is a count of buyers, wearing a first-order costume.
The first order has to be chosen from all of their orders
Filter the month after you know the first date, not before.
SELECT COUNT(*)
FROM (
SELECT customer_id, MIN(created_at) AS first_order
FROM orders
GROUP BY customer_id
) s
WHERE first_order >= TIMESTAMPTZ '2026-12-01'
AND first_order < TIMESTAMPTZ '2027-01-01'
That is 40. The inner query sees July, so December is not the minimum for the 360 who came back.
COUNT(DISTINCT customer_id) on December orders is the 400 again. Distinct collapses two December orders into one person. It never asks whether that person existed in November. This is not a rolling "last month" that starts mid-month. The December bounds can be exactly right and the population is still "anyone who bought."
If the business timezone is not the session timezone, convert the timestamp before you decide which month the first order fell in. Otherwise a December 31 evening becomes January, the same way a UTC cast moves Monday.
How to catch it
Run both counts.
-- buyers in December
SELECT COUNT(DISTINCT customer_id)
FROM orders
WHERE created_at >= TIMESTAMPTZ '2026-12-01'
AND created_at < TIMESTAMPTZ '2027-01-01'
If that equals the AI's "new customers," the first-order date was taken inside the month. The gap between that count and the unfiltered MIN query is the returning customers who were labeled new.
The prompt that heads this off
When I ask for new customers, a new customer is someone whose first order
ever falls in the range. Compute MIN(created_at) from all orders, then
filter that first date. Do not filter the month before the MIN. Also show
me the count of distinct customers who ordered in the range, labeled as
buyers, not as new.
The short version
MIN(created_at) after a December filter is the first December order, so every December buyer counts as new. Take the first order over all time, then keep the ones whose first order is in December.
Read-only MCP gateway: mcpserver.design.
Related reading: