Retail · SQL
Reconciling a board-level revenue discrepancy with conditional SQL aggregation
Independent grading: 97/100(verify, opens in a new tab)
Problem
The CFO at Meridian Retail (simulated retailer) flagged a conflict between two systems: the Finance platform showed NGN 38.5M for October 2023 while the sales dashboard showed NGN 56.9M. Before a board presentation, I was asked to pull October orders broken down by status with both gross and net revenue figures so the two numbers could be reconciled.
What I did
To avoid edge cases with time components, I used a half-open interval date filter rather than a between clause: created_at >= 2023-10-01 AND created_at < 2023-11-01. Gross revenue was computed by summing total_cents for all statuses and dividing by 100 to convert to naira. Net revenue required a CASE inside SUM that zeroes out rows where status is cancelled, returned, or refunded, which kept both measures in a single pass over the data. Sorting by gross_revenue_naira DESC made the seven status rows immediately readable for a board-prep audience.
Finding
Gross revenue for October 2023 totalled NGN 56,876,792.48 across 610 orders, and net revenue after excluding cancelled, returned, and refunded orders totalled NGN 38,450,382.35, reconciling exactly to the dashboard and Finance system figures respectively.
Outcome
The CFO received a single query result that resolved the discrepancy and provided the breakdown needed to present confidently to the board.
SELECT
status,
COUNT(*) AS order_count,
SUM(total_cents) / 100.0 AS gross_revenue_naira,
SUM(CASE
WHEN status NOT IN ('cancelled', 'returned', 'refunded')
THEN total_cents
ELSE 0
END) / 100.0 AS net_revenue_naira
FROM orders
WHERE created_at >= '2023-10-01'
AND created_at < '2023-11-01'
GROUP BY status
ORDER BY gross_revenue_naira DESC;The CASE inside SUM computes gross and net revenue in a single scan, while the half-open date interval ensures no orders are missed or double-counted due to time components in the timestamp.
Next step
A useful follow-on would be running the same gross-vs-net split for each prior month to show whether the gap between the two figures is growing, shrinking, or stable over time.
- SQL conditional aggregation with CASE inside SUM
- Half-open interval date filtering in SQL
- Cents-to-currency unit conversion in SQL
- GROUP BY and ORDER BY for ranked output