Queries · q224 · Chief Financial Officer · week 2, night 2
“Across all of 2025's payments, does the 'ledger credit = payment + refund' pattern seen in the sample hold universally, and how large are the 'no ledger entries' and 'other mismatch' buckets?”
Fictional people at a fictional company, each played by a language-model agent. Who they are and what they are told
Week 2
The external auditors start the 2025 year-end audit in two weeks. They will want to see that the money adds up across the whole chain: what shoppers paid, what was refunded or credited, what merchants received in payouts, what Tidewick kept, and that invoices agree with all of it. They will also trace sample transactions end to end.
The manager, at the start of week 2
Subject: Good progress — but the audit clock is now the priority
Kofi,
Thanks — this is useful, and I appreciate you flagging the caveats rather than smoothing them over. A few reactions and then where I want to focus next week.
What stood out:
The big new driver: external audit starts in two weeks. They'll want to walk the full money trail — what a shopper paid, what was refunded or credited back, what the merchant was paid out, what we kept as our fee, and that it all agrees with the invoices and accounts — and they'll pick sample transactions and trace them start to finish. I'd rather we got ahead of that than react to it.
Questions for this week, in priority order:
Audit readiness (#1) is the one I most need — please make that the lead request even if it means FX and B2B receivables slip another week.
Thanks, Tobias
The analyst · week 2, night 2, request 2 of 3
Report 1's ledger columns are the big finding. For every refunded payment, ledger_credit_total exactly equals payment_amount + amount_refunded, not net_amount: e.g. OfficeHub Nordic SO-100363 (265,275.15 paid, fully refunded) shows ledger_credit_total = 530,550.30 = 2x the payment, and Fell & Fjell #1179 shows 69,408.75 = 34,885.85 + 34,522.90 exactly. My ledger_debit_credit_diff column is useless for catching this (it's 0 everywhere by construction, since any balanced journal entry nets to zero regardless of which accounts are touched) — I designed that check too naively. Before I tell Tobias "the ledger balances," I need to see the actual account-level entries for a few of these to confirm this is just a normal (and explainable) refund-accounting pattern — e.g. a reversing entry on a different account — rather than revenue being double-booked.
Two more things in Report 1 don't fit any pattern and need chasing at scale rather than left as anecdotes: Castell Fasteners' SO-100233 (no refund, not yet paid out) has ledger_credit_total of 805.56 against a payment_amount of 752.91 — an unexplained ~7% gap; and Marrowby & Vane's #3307 (a tiny $20 payment) has NULL ledger_credit_total, meaning no ledger entries exist for it at all. Given this week's pattern across other teams (CISO: deletions and role changes with zero audit trail), an "orphan payment with no ledger entry" is exactly the kind of gap auditors will zero in on, so I want to know if that's a one-off or systemic before Friday.
Tonight's three slots: (1) pull the actual ledger line items for the flagged refunded + anomalous sample payments to explain the credit-total pattern; (2) run the same credit-total-vs-payment reconciliation across ALL of 2025's payments (not just the 25-sample) to see if the "+refund" pattern is universal/explainable and how big the "other mismatch" and "no ledger entries" buckets really are; (3) quantify the no-ledger-entry gap by merchant to see if it's concentrated or spread thin.
The engineer · 1 dry run on the empty schema, then the submission
WITH qualifying_payments AS ( SELECT p.id, p.currency_code, p.amount, p.amount_refunded, p.captured_at FROM payments p WHERE p.status IN ('captured', 'refunded', 'partially_refunded') AND p.captured_at >= DATE '2025-01-01' AND p.captured_at < DATE '2026-01-01' ), payment_amounts_eur AS ( SELECT qp.id, qp.amount / er.rate AS payment_amount_eur, qp.amount_refunded / er.rate AS amount_refunded_eur FROM qualifying_payments qp JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = qp.currency_code AND er.as_of = date(qp.captured_at) ), ledger_entries_eur AS ( SELECT le.payment_id, le.direction, le.amount / er.rate AS amount_eur FROM ledger_entries le JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = le.currency_code AND er.as_of = date(le.occurred_at) WHERE le.payment_id IN (SELECT id FROM qualifying_payments) ), ledger_agg AS ( SELECT payment_id, SUM(CASE WHEN direction = 'credit' THEN amount_eur ELSE 0 END) AS total_credit_eur, SUM(CASE WHEN direction = 'debit' THEN amount_eur ELSE 0 END) AS total_debit_eur, COUNT(*) AS entry_count FROM ledger_entries_eur GROUP BY payment_id ), classified AS ( SELECT pa.id, pa.payment_amount_eur, pa.amount_refunded_eur, COALESCE(la.total_credit_eur, 0) AS total_credit_eur, COALESCE(la.total_debit_eur, 0) AS total_debit_eur, COALESCE(la.entry_count, 0) AS entry_count, (pa.payment_amount_eur - pa.amount_refunded_eur) AS target_net, (pa.payment_amount_eur + pa.amount_refunded_eur) AS target_gross FROM payment_amounts_eur pa LEFT JOIN ledger_agg la ON la.payment_id = pa.id ), categorized AS ( SELECT id, payment_amount_eur, total_credit_eur, total_debit_eur, CASE WHEN entry_count = 0 THEN 'no ledger entries' WHEN ABS(total_credit_eur - target_net) <= GREATEST(0.01 * ABS(target_net), 1) THEN 'credit = net amount' WHEN ABS(total_credit_eur - target_gross) <= GREATEST(0.01 * ABS(target_gross), 1) THEN 'credit = payment + refund' ELSE 'other mismatch' END AS reconciliation_category FROM classified ) SELECT reconciliation_category, COUNT(*) AS payment_count, ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER (), 2) AS pct_of_payments, ROUND(SUM(payment_amount_eur), 2) AS total_payment_amount_eur, ROUND(SUM(total_credit_eur), 2) AS total_ledger_credit_eur, ROUND(SUM(total_debit_eur), 2) AS total_ledger_debit_eur FROM categorized GROUP BY reconciliation_category ORDER BY payment_count DESC
“Query compiles and produces the requested grain/columns. Submitting.”
“Submitted. Summary of the approach:
- Scoped to payments with status in (captured, refunded, partially_refunded) and captured_at in 2025.
- Converted each payment's amount/amount_refunded to EUR using the exchange rate on captured_at's date, and converted each linked ledger_entries.amount to EUR independently using that entry's own currency_code and occurred_at date, as requested.
- Classified each payment into no ledger entries, credit = net amount, credit = payment + refund, or other mismatch using a tolerance of max(1%, €1) against the net and gross targets respectively, checking the net-amount pattern first (noted in the submit note since it affects edge cases where refund = 0).
- Aggregated to one row per category with count, share, and summed EUR totals, ordered by payment_count descending.”
As Kofi Mensah saw it, from the small dataset the agents worked on
reconciliation_category | payment_count | pct_of_payments | total_payment_amount_eur | total_ledger_credit_eur | total_ledger_debit_eur --------------------------+---------------+-----------------+--------------------------+-------------------------+----------------------- credit = net amount | 9143 | 78.55 | 2169737.87 | 2169721.91 | 2169721.91 credit = payment + refund | 2181 | 18.74 | 553394.46 | 829231.76 | 829231.76 other mismatch | 249 | 2.14 | 84443.43 | 135386.7 | 135386.7 no ledger entries | 67 | 0.58 | 3380.6 | 0.0 | 0.0 (4 rows)
The memo and the queries that followed; links found by title, so inferred
Later queries that build on this report:
It builds on:
Changed only where the benchmark needs the same result on every system
WITH qualifying_payments AS ( SELECT p.id, p.currency_code, p.amount, p.amount_refunded, p.captured_at p.id AS id, p.currency_code AS currency_code, p.amount AS amount, p.amount_refunded AS amount_refunded, p.captured_at AS captured_at FROM payments p WHERE p.status IN ('captured', 'refunded', 'partially_refunded') AND p.captured_at >= DATE '2025-01-01' AND p.captured_at < DATE '2026-01-01'),payment_amounts_eur AS ( SELECT qp.id, qp.amount / er.rate AS payment_amount_eur, qp.amount_refunded / er.rate AS amount_refunded_eur qp.id AS id, CAST(qp.amount / er.rate AS NUMERIC(18,6)) AS payment_amount_eur, CAST(qp.amount_refunded / er.rate AS NUMERIC(18,6)) AS amount_refunded_eur FROM qualifying_payments qp JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = qp.currency_code AND er.as_of = date(qp.captured_at) AND er.as_of = CAST(qp.captured_at AS DATE)),ledger_entries_eur AS ( SELECT le.payment_id, le.direction, le.amount / er.rate AS amount_eur le.payment_id AS payment_id, le.direction AS direction, CAST(le.amount / er.rate AS NUMERIC(18,6)) AS amount_eur FROM ledger_entries le JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = le.currency_code AND er.as_of = date(le.occurred_at) AND er.as_of = CAST(le.occurred_at AS DATE) WHERE le.payment_id IN (SELECT id FROM qualifying_payments)),ledger_agg AS ( SELECT payment_id, SUM(CASE WHEN direction = 'credit' THEN amount_eur ELSE 0 END) AS total_credit_eur, SUM(CASE WHEN direction = 'debit' THEN amount_eur ELSE 0 END) AS total_debit_eur, COUNT(*) AS entry_count FROM ledger_entries_eur GROUP BY payment_id),classified AS ( SELECT pa.id, pa.payment_amount_eur, pa.amount_refunded_eur, pa.id AS id, pa.payment_amount_eur AS payment_amount_eur, pa.amount_refunded_eur AS amount_refunded_eur, COALESCE(la.total_credit_eur, 0) AS total_credit_eur, COALESCE(la.total_debit_eur, 0) AS total_debit_eur, COALESCE(la.entry_count, 0) AS entry_count, (pa.payment_amount_eur - pa.amount_refunded_eur) AS target_net, (pa.payment_amount_eur + pa.amount_refunded_eur) AS target_gross FROM payment_amounts_eur pa LEFT JOIN ledger_agg la ON la.payment_id = pa.id),categorized AS ( SELECT id, payment_amount_eur, total_credit_eur, total_debit_eur, CASE WHEN entry_count = 0 THEN 'no ledger entries' WHEN ABS(total_credit_eur - target_net) <= GREATEST(0.01 * ABS(target_net), 1) THEN 'credit = net amount' WHEN ABS(total_credit_eur - target_gross) <= GREATEST(0.01 * ABS(target_gross), 1) THEN 'credit = payment + refund' ELSE 'other mismatch' END AS reconciliation_category FROM classified)SELECT reconciliation_category, COUNT(*) AS payment_count, ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER (), 2) AS pct_of_payments, ROUND(SUM(payment_amount_eur), 2) AS total_payment_amount_eur, ROUND(SUM(total_credit_eur), 2) AS total_ledger_credit_eur, ROUND(SUM(total_debit_eur), 2) AS total_ledger_debit_eur 100.0 * COUNT(*) / SUM(COUNT(*)) OVER () AS pct_of_payments, SUM(payment_amount_eur) AS total_payment_amount_eur, SUM(total_credit_eur) AS total_ledger_credit_eur, SUM(total_debit_eur) AS total_ledger_debit_eurFROM categorizedGROUP BY reconciliation_categoryORDER BY payment_count DESCORDER BY payment_count DESC NULLS FIRST, reconciliation_category NULLS LAST
WITH qualifying_payments AS ( SELECT p.id AS id, p.currency_code AS currency_code, p.amount AS amount, p.amount_refunded AS amount_refunded, p.captured_at AS captured_at FROM payments p WHERE p.status IN ('captured', 'refunded', 'partially_refunded') AND p.captured_at >= DATE '2025-01-01' AND p.captured_at < DATE '2026-01-01' ), payment_amounts_eur AS ( SELECT qp.id AS id, CAST(qp.amount / er.rate AS NUMERIC(18,6)) AS payment_amount_eur, CAST(qp.amount_refunded / er.rate AS NUMERIC(18,6)) AS amount_refunded_eur FROM qualifying_payments qp JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = qp.currency_code AND er.as_of = CAST(qp.captured_at AS DATE) ), ledger_entries_eur AS ( SELECT le.payment_id AS payment_id, le.direction AS direction, CAST(le.amount / er.rate AS NUMERIC(18,6)) AS amount_eur FROM ledger_entries le JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = le.currency_code AND er.as_of = CAST(le.occurred_at AS DATE) WHERE le.payment_id IN (SELECT id FROM qualifying_payments) ), ledger_agg AS ( SELECT payment_id, SUM(CASE WHEN direction = 'credit' THEN amount_eur ELSE 0 END) AS total_credit_eur, SUM(CASE WHEN direction = 'debit' THEN amount_eur ELSE 0 END) AS total_debit_eur, COUNT(*) AS entry_count FROM ledger_entries_eur GROUP BY payment_id ), classified AS ( SELECT pa.id AS id, pa.payment_amount_eur AS payment_amount_eur, pa.amount_refunded_eur AS amount_refunded_eur, COALESCE(la.total_credit_eur, 0) AS total_credit_eur, COALESCE(la.total_debit_eur, 0) AS total_debit_eur, COALESCE(la.entry_count, 0) AS entry_count, (pa.payment_amount_eur - pa.amount_refunded_eur) AS target_net, (pa.payment_amount_eur + pa.amount_refunded_eur) AS target_gross FROM payment_amounts_eur pa LEFT JOIN ledger_agg la ON la.payment_id = pa.id ), categorized AS ( SELECT id, payment_amount_eur, total_credit_eur, total_debit_eur, CASE WHEN entry_count = 0 THEN 'no ledger entries' WHEN ABS(total_credit_eur - target_net) <= GREATEST(0.01 * ABS(target_net), 1) THEN 'credit = net amount' WHEN ABS(total_credit_eur - target_gross) <= GREATEST(0.01 * ABS(target_gross), 1) THEN 'credit = payment + refund' ELSE 'other mismatch' END AS reconciliation_category FROM classified ) SELECT reconciliation_category, COUNT(*) AS payment_count, 100.0 * COUNT(*) / SUM(COUNT(*)) OVER () AS pct_of_payments, SUM(payment_amount_eur) AS total_payment_amount_eur, SUM(total_credit_eur) AS total_ledger_credit_eur, SUM(total_debit_eur) AS total_ledger_debit_eur FROM categorized GROUP BY reconciliation_category ORDER BY payment_count DESC NULLS FIRST, reconciliation_category NULLS LAST
The agents' own records, as the role play wrote them; only the personal data of the person who ran it is redacted
Report request from Kofi Mensah (analyst for the Chief Financial Officer): Title: Ledger-to-Payment Reconciliation Pattern at Scale, 2025 Question: Across all of 2025's payments, does the 'ledger credit = payment + refund' pattern seen in the sample hold universally, and how large are the 'no ledger entries' and 'other mismatch' buckets? Grain: one reconciliation category, over all payments captured in 2025 Columns: - reconciliation_category - payment_count - pct_of_payments - total_payment_amount_eur - total_ledger_credit_eur - total_ledger_debit_eur Details: Source: payments with status in a captured/refunded/partially_refunded state and captured_at in 2025. For each payment, aggregate linked ledger_entries (via payment_id) into total credit amount and total debit amount, converting each ledger_entries.amount to EUR via exchange_rates matched on ledger_entries.currency_code and date(ledger_entries.occurred_at) — do not assume the ledger entry's currency matches the payment's currency, convert each independently. Also convert payments.amount and amount_refunded to EUR via exchange_rates on captured_at. Classify each payment into exactly one reconciliation_category: 'no ledger entries' (zero linked ledger_entries rows); else 'credit = net amount' if total_ledger_credit_eur is within 1% (or €1, whichever is larger) of (payment_amount_eur - amount_refunded_eur); else 'credit = payment + refund' if total_ledger_credit_eur is within the same tolerance of (payment_amount_eur + amount_refunded_eur); else 'other mismatch'. Count payments and sum amounts per category; pct_of_payments is each category's share of all qualifying 2025 payments. Order by payment_count descending. Check your query with the warehouse's dry_run tool, then hand it in with submit.
dry_runWITH qualifying_payments AS ( SELECT p.id, p.currency_code, p.amount, p.amount_refunded, p.captured_at FROM payments p WHERE p.status IN ('captured', 'refunded', 'partially_refunded') AND p.captured_at >= DATE '2025-01-01' AND p.captured_at < DATE '2026-01-01' ), payment_amounts_eur AS ( SELECT qp.id, qp.amount / er.rate AS payment_amount_eur, qp.amount_refunded / er.rate AS amount_refunded_eur FROM qualifying_payments qp JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = qp.currency_code AND er.as_of = date(qp.captured_at) ), ledger_entries_eur AS ( SELECT le.payment_id, le.direction, le.amount / er.rate AS amount_eur FROM ledger_entries le JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = le.currency_code AND er.as_of = date(le.occurred_at) WHERE le.payment_id IN (SELECT id FROM qualifying_payments) ), ledger_agg AS ( SELECT payment_id, SUM(CASE WHEN direction = 'credit' THEN amount_eur ELSE 0 END) AS total_credit_eur, SUM(CASE WHEN direction = 'debit' THEN amount_eur ELSE 0 END) AS total_debit_eur, COUNT(*) AS entry_count FROM ledger_entries_eur GROUP BY payment_id ), classified AS ( SELECT pa.id, pa.payment_amount_eur, pa.amount_refunded_eur, COALESCE(la.total_credit_eur, 0) AS total_credit_eur, COALESCE(la.total_debit_eur, 0) AS total_debit_eur, COALESCE(la.entry_count, 0) AS entry_count, (pa.payment_amount_eur - pa.amount_refunded_eur) AS target_net, (pa.payment_amount_eur + pa.amount_refunded_eur) AS target_gross FROM payment_amounts_eur pa LEFT JOIN ledger_agg la ON la.payment_id = pa.id ), categorized AS ( SELECT id, payment_amount_eur, total_credit_eur, total_debit_eur, CASE WHEN entry_count = 0 THEN 'no ledger entries' WHEN ABS(total_credit_eur - target_net) <= GREATEST(0.01 * ABS(target_net), 1) THEN 'credit = net amount' WHEN ABS(total_credit_eur - target_gross) <= GREATEST(0.01 * ABS(target_gross), 1) THEN 'credit = payment + refund' ELSE 'other mismatch' END AS reconciliation_category FROM classified ) SELECT reconciliation_category, COUNT(*) AS payment_count, ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER (), 2) AS pct_of_payments, ROUND(SUM(payment_amount_eur), 2) AS total_payment_amount_eur, ROUND(SUM(total_credit_eur), 2) AS total_ledger_credit_eur, ROUND(SUM(total_debit_eur), 2) AS total_ledger_debit_eur FROM categorized GROUP BY reconciliation_category ORDER BY payment_count DESC
STATUS: COMPILES (dry run on the empty schema, no data read) COLUMNS: reconciliation_category, payment_count, pct_of_payments, total_payment_amount_eur, total_ledger_credit_eur, total_ledger_debit_eur
submitWITH qualifying_payments AS ( SELECT p.id, p.currency_code, p.amount, p.amount_refunded, p.captured_at FROM payments p WHERE p.status IN ('captured', 'refunded', 'partially_refunded') AND p.captured_at >= DATE '2025-01-01' AND p.captured_at < DATE '2026-01-01' ), payment_amounts_eur AS ( SELECT qp.id, qp.amount / er.rate AS payment_amount_eur, qp.amount_refunded / er.rate AS amount_refunded_eur FROM qualifying_payments qp JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = qp.currency_code AND er.as_of = date(qp.captured_at) ), ledger_entries_eur AS ( SELECT le.payment_id, le.direction, le.amount / er.rate AS amount_eur FROM ledger_entries le JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = le.currency_code AND er.as_of = date(le.occurred_at) WHERE le.payment_id IN (SELECT id FROM qualifying_payments) ), ledger_agg AS ( SELECT payment_id, SUM(CASE WHEN direction = 'credit' THEN amount_eur ELSE 0 END) AS total_credit_eur, SUM(CASE WHEN direction = 'debit' THEN amount_eur ELSE 0 END) AS total_debit_eur, COUNT(*) AS entry_count FROM ledger_entries_eur GROUP BY payment_id ), classified AS ( SELECT pa.id, pa.payment_amount_eur, pa.amount_refunded_eur, COALESCE(la.total_credit_eur, 0) AS total_credit_eur, COALESCE(la.total_debit_eur, 0) AS total_debit_eur, COALESCE(la.entry_count, 0) AS entry_count, (pa.payment_amount_eur - pa.amount_refunded_eur) AS target_net, (pa.payment_amount_eur + pa.amount_refunded_eur) AS target_gross FROM payment_amounts_eur pa LEFT JOIN ledger_agg la ON la.payment_id = pa.id ), categorized AS ( SELECT id, payment_amount_eur, total_credit_eur, total_debit_eur, CASE WHEN entry_count = 0 THEN 'no ledger entries' WHEN ABS(total_credit_eur - target_net) <= GREATEST(0.01 * ABS(target_net), 1) THEN 'credit = net amount' WHEN ABS(total_credit_eur - target_gross) <= GREATEST(0.01 * ABS(target_gross), 1) THEN 'credit = payment + refund' ELSE 'other mismatch' END AS reconciliation_category FROM classified ) SELECT reconciliation_category, COUNT(*) AS payment_count, ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER (), 2) AS pct_of_payments, ROUND(SUM(payment_amount_eur), 2) AS total_payment_amount_eur, ROUND(SUM(total_credit_eur), 2) AS total_ledger_credit_eur, ROUND(SUM(total_debit_eur), 2) AS total_ledger_debit_eur FROM categorized GROUP BY reconciliation_category ORDER BY payment_count DESC
Submitted to tonight's batch. Your work on this request is done.