← Sales & Refund Reconciliation My saved work →
Independent practice · SQLite
Build a per-order refund ledger
Practice dataset
Your work
Active 0:00 · Paused 0sKeep a handoff you can replay
Check your current answer, then download the output and its report. The report includes the source, answer and each check so you can review what passed and what needs work.
To replay your work, open this task with the same dataset, import your answer and check it again.
Show the reference answer
WITH clean_refunds AS ( SELECT DISTINCT r.* FROM refunds r WHERE NOT EXISTS ( SELECT 1 FROM refunds x WHERE x.refund_id=r.refund_id AND (x.order_id<>r.order_id OR x.amount<>r.amount OR x.status<>r.status OR x.booked_at<>r.booked_at) ) ), posted AS (SELECT order_id,SUM(amount) AS refunded FROM clean_refunds WHERE status='posted' AND booked_at>='2026-09-01' AND booked_at<'2026-10-01' GROUP BY order_id), ledger AS (SELECT o.*,COALESCE(p.refunded,0) AS refunded FROM orders o LEFT JOIN posted p ON p.order_id=o.order_id WHERE o.status='paid' AND o.ordered_at>='2026-09-01' AND o.ordered_at<'2026-10-01') SELECT order_id,amount,refunded,amount-refunded AS net_cents FROM ledger ORDER BY order_id
There must be exactly one output row per paid cohort order, even with several refund rows.