milestone 3/4 Revenue & payment reconciliation
Reconcile revenue with payments and identify differences.
AI Platform Economics · Calculate success rates and usage costs, prevent join fan-out, and reconcile split payments with refunds.
approach
Reconciliation compares independent totals. Sum payments and refunds by invoice before joining. Net collected is payments minus refunds; gap is billed minus net collected. A refund creates a gap under this definition, even when the original bill was paid.
worked-example.sql
WITH paid AS( SELECT invoice_id,SUM(amount) AS paid_amount FROM payments GROUP BY invoice_id ),returned AS( SELECT invoice_id,SUM(amount) AS refund_amount FROM refunds GROUP BY invoice_id ) SELECT i.id, i.billed_amount, COALESCE(p.paid_amount,0)-COALESCE(r.refund_amount,0) AS net_collected, i.billed_amount-COALESCE(p.paid_amount,0)+COALESCE(r.refund_amount,0) AS gap FROM invoices i LEFT JOIN paid p ON p.invoice_id=i.id LEFT JOIN returned r ON r.invoice_id=i.id;
data for this milestone
invoices4 columns · 6 rows
| idinteger · key | account_idinteger | periodtext | billed_amountreal |
|---|---|---|---|
| 1 | 1 | 2025-01 | 20 |
| 2 | 3 | 2025-01 | 80 |
| 3 | 4 | 2025-01 | 20 |
| 4 | 6 | 2025-01 | 80 |
| 5 | 1 | 2025-02 | 20 |
showing 5 of 6 rows
payments3 columns · 7 rows
| idinteger · key | invoice_idinteger | amountreal |
|---|---|---|
| 1 | 1 | 10 |
| 2 | 1 | 10 |
| 3 | 2 | 80 |
| 4 | 3 | 15 |
| 5 | 4 | 80 |
showing 5 of 7 rows
refunds3 columns · 4 rows
| idinteger · key | invoice_idinteger | amountreal |
|---|---|---|
| 1 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 2 | 10 |
| 4 | 4 | 5 |
all 4 rows