Why Your AI's Weekend Revenue Is Missing Sunday
Asked for weekend revenue and got Saturday only. Postgres EXTRACT(DOW) numbers Sunday as 0 and Saturday as 6. The filter IN (6, 7) keeps Saturday and a day that does not exist.
You ask: "What was weekend revenue?" The AI says $9,000. Saturday was $9,000. Sunday was $7,000. Sunday is not in the result.
SELECT SUM(amount)
FROM orders
WHERE EXTRACT(DOW FROM created_at) IN (6, 7)
In Postgres, DOW is 0 for Sunday, 1 for Monday, and 6 for Saturday. The filter keeps 6, which is Saturday. Nothing in the column is 7. Sunday is 0, and 0 is not in the list.
The number 7 is not Sunday here
The query is a reasonable writing of "Saturday and Sunday" if Sunday is 7. That is ISO, and it is what EXTRACT(ISODOW FROM ...) returns: Monday 1 through Sunday 7. DOW is the other scale. The AI picked the function and the numbers from different scales.
-- Saturday and Sunday, Postgres DOW (Sunday = 0)
WHERE EXTRACT(DOW FROM created_at) IN (0, 6)
-- same days, ISO numbering (Sunday = 7)
WHERE EXTRACT(ISODOW FROM created_at) IN (6, 7)
Weekdays hide the mistake. DOW BETWEEN 1 AND 5 really is Monday through Friday, so a weekday total can match the spreadsheet while the weekend total is missing a day. This is not the week bucket that starts on Monday. There, Sunday's orders are present and filed under the previous week. Here, Sunday's orders are gone.
MySQL DAYOFWEEK starts at 1 for Sunday and ends at 7 for Saturday. Weekend is IN (1, 7). IN (6, 7) on MySQL is Friday and Saturday, so the same literal filter drops Sunday and adds Friday.
EXTRACT(DOW) on a timestamptz follows the session timezone. Fix the zone before the day number, or Sunday evening moves to Monday the way a UTC date cast moves midnight.
How to catch it
SELECT EXTRACT(DOW FROM created_at)::int AS dow, COUNT(*), SUM(amount)
FROM orders
WHERE <the AI's date range>
GROUP BY 1
ORDER BY 1
You want a row for 0 and a row for 6 if both days had orders. If the AI's filter was IN (6, 7) and the 0-row has money, that money is the missing Sunday. Add the 0-row to the AI's total. If the sums match your weekend, the day number was the whole bug.
The prompt that heads this off
When I say weekend, use Sunday and Saturday in this timezone: America/New_York.
In Postgres, EXTRACT(DOW) has Sunday as 0 and Saturday as 6. Do not use 7
with DOW. Prefer ISODOW if you want Sunday as 7, and say which function
you used. Show the day numbers you kept.
The short version
Postgres DOW numbers Sunday as 0. IN (6, 7) keeps Saturday and a day that does not exist. Use 0 and 6, or ISODOW with 6 and 7, and convert the timezone first.
Read-only MCP gateway: mcpserver.design.
Related reading: