Back to Blog

Why Your AI's Revenue Ignores Every Refund

Asked for revenue and got $100,000. Refunds are stored as -$8,000 in the same column. The query kept amount > 0, so the refunds never reduced the total. Finance's net is $92,000.

You ask: "What's revenue for November?" The AI says $100,000. Finance has $92,000. The payments table holds charges as positive amounts and refunds as negative amounts. November charges are $100,000. November refunds are -$8,000.

SELECT SUM(amount)
FROM payments
WHERE created_at >= TIMESTAMPTZ '2026-11-01'
  AND created_at <  TIMESTAMPTZ '2026-12-01'
  AND amount > 0

amount > 0 looks like housekeeping. It throws away every refund. SUM of what remains is gross charges. Net is $100,000 + (-$8,000) = $92,000.

A negative amount is usually the refund

SUM(amount) already does the right net, because adding a negative subtracts it. The filter is what breaks that.

-- net: charges and refunds
SELECT SUM(amount) AS net
FROM payments
WHERE created_at >= TIMESTAMPTZ '2026-11-01'
  AND created_at <  TIMESTAMPTZ '2026-12-01'

-- gross and refunds, side by side
SELECT
  SUM(amount) FILTER (WHERE amount > 0) AS gross,
  SUM(amount) FILTER (WHERE amount < 0) AS refunds
FROM payments
WHERE created_at >= TIMESTAMPTZ '2026-11-01'
  AND created_at <  TIMESTAMPTZ '2026-12-01'

If refunds are a separate table and the amounts there are positive, do not add that table to charges. Subtract it:

SELECT
  (SELECT COALESCE(SUM(amount), 0) FROM charges WHERE <the month>)
  - (SELECT COALESCE(SUM(amount), 0) FROM refunds WHERE <the month>) AS net

Say which one you reported. "Gross charges $100,000, refunds $8,000, net $92,000" cannot be misread. "Revenue $100,000" can.

This is not cents read as dollars, which is off by 100×. It is not dollars added to euros. The unit is right. The sign was filtered off. A refund of zero would not explain an $8,000 gap. The negatives do.

How to catch it

SELECT COUNT(*) FILTER (WHERE amount < 0) AS refund_rows,
       SUM(amount) FILTER (WHERE amount < 0) AS refunds
FROM payments
WHERE <the filter the AI used, without the amount > 0>

If refund_rows is more than zero and the AI's total equals SUM of only the positive rows, the refunds were excluded. Subtract them, or label the figure gross.

The prompt that heads this off

When I ask for revenue, check whether refunds are negative amounts in the
same column or positive amounts in another table. Do not filter to
amount > 0 unless I asked for gross. Report gross, refunds, and net, and
say which one is the answer.

The short version

SUM(amount) nets refunds that are stored as negatives. WHERE amount > 0 drops them and leaves gross charges. If refunds are their own table, subtract it. Say gross or net next to the number.


Read-only MCP gateway: mcpserver.design.

Related reading: