Find payment reconciliation discrepancies
Your task
Return order_id, expected_cents, recorded_cents, and issue for discrepancies only. Completed orders expect their full amount; refunded and cancelled orders expect zero. Net ledger charges minus refunds. Use orphan_payment with NULL expected cents if the order is missing, missing_payment with zero recorded cents for a positive expected amount with no ledger, and amount_mismatch for any other nonmatching amount. Sort by order ID.
A reconciliation should reveal unexpected ledger entries, missing payments, and incorrect net amounts.
Sample data
| id | customer_id | ordered_on | channel | status | total_cents |
|---|---|---|---|---|---|
| 201 | 1 | 2025-01-04 | online | completed | 2500 |
| 202 | 2 | 2025-01-10 | store | completed | 1800 |
| 203 | 2 | 2025-01-10 | store | completed | 3200 |
| 204 | 3 | 2025-01-18 | marketplace | refunded | 5000 |
| 205 | 4 | 2025-02-02 | store | completed | 2700 |
| 206 | 1 | 2025-02-04 | online | completed | 4000 |
| 207 | 5 | 2025-02-18 | online | cancelled | 2400 |
| 208 | 6 | 2025-02-28 | online | completed | 1700 |
| 209 | 4 | 2025-03-16 | online | completed | 2900 |
| 210 | 8 | 2025-03-09 | store | completed | 3100 |
Expected output
| order_id | expected_cents | recorded_cents | issue |
|---|---|---|---|
| 205 | 2700 | 2500 | amount_mismatch |
| 206 | 4000 | 3500 | amount_mismatch |
| 207 | 0 | 900 | amount_mismatch |
| 210 | 3100 | 0 | missing_payment |
| 999 | NULL | 800 | orphan_payment |
Show a hint
Full outer join orders with a ledger summary, then classify discrepancies in that order.
Data quality and reconciliation guide
Keep raw values available while you clean them
The staged_orders table is an import, so references, statuses, and amounts are text and may contain missing or malformed values. Trim whitespace before testing a field. An empty string after trimming can be turned into NULL to give missing data one representation.
SELECT import_id, NULLIF(TRIM(order_ref), '') AS clean_ref
FROM staged_orders
ORDER BY import_id;Compare records at the same grain
The payment ledger may have several charges or refunds for one order. Aggregate that ledger to one row per order before comparing it with shop_orders. Keep unmatched keys visible so a reconciliation reveals missing orders and missing payments.
SELECT order_id,
SUM(CASE WHEN kind = 'charge' THEN amount_cents ELSE -amount_cents END) AS net_cents
FROM payment_ledger
GROUP BY order_id
ORDER BY order_id;Worked solution
WITH ledger AS (SELECT order_id,SUM(CASE WHEN kind='charge' THEN amount_cents ELSE -amount_cents END) AS net_cents FROM payment_ledger GROUP BY order_id), orders AS (SELECT id,CASE WHEN status='completed' THEN total_cents ELSE 0 END AS expected_cents FROM shop_orders) SELECT COALESCE(o.id,l.order_id) AS order_id,o.expected_cents,COALESCE(l.net_cents,0) AS recorded_cents,CASE WHEN o.id IS NULL THEN 'orphan_payment' WHEN l.order_id IS NULL THEN 'missing_payment' ELSE 'amount_mismatch' END AS issue FROM orders o FULL OUTER JOIN ledger l ON l.order_id=o.id WHERE o.id IS NULL OR o.expected_cents<>COALESCE(l.net_cents,0) ORDER BY order_id;Press Control+Space for SQL suggestions. Use the arrow keys to select an option and Enter to insert it.
Results
Loading SQLite…
