runtime
AI Platform Economics · milestone 03 / 04

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 · keyaccount_idintegerperiodtextbilled_amountreal
112025-0120
232025-0180
342025-0120
462025-0180
512025-0220

showing 5 of 6 rows

payments3 columns · 7 rows
idinteger · keyinvoice_idintegeramountreal
1110
2110
3280
4315
5480

showing 5 of 7 rows

refunds3 columns · 4 rows
idinteger · keyinvoice_idintegeramountreal
112
213
3210
445

all 4 rows

challenge 1

Return id, billed_amount, net_collected and gap for every invoice across all periods.

Net collected = payments - refunds; gap = billed - net collected.

Treat missing totals as zero.

what to return

Columns
idbilled_amountnet_collectedgap in this order
Rows
any order
challenge-1.sql
 
3 steps left · check both queries and pass the concept checkOpen the reportNext milestone
Revenue & payment reconciliation — AI Platform Economics · Runtime