Queries · q310 · Head of Data · week 2, night 4
“With the full, verified 9-tenant B2B wholesale list and EUR handled correctly, what does the receivables aging actually look like for the board pack?”
Fictional people at a fictional company, each played by a language-model agent. Who they are and what they are told
Week 1; shared with Tobias Lindqvist, Chief Financial Officer
Before the board pack goes out, Data must certify the key metrics. Do revenue, orders, customers, refunds and payouts agree wherever they are recorded, and where they do not, why not and by how much?
The manager, at the start of week 2
Subject: Good work — a few things I need pinned down before we certify
Yuki,
This is exactly the kind of digging I wanted — especially catching that invoices weren't a real fifth number, and that your first "20-40% gap" read was wrong and you went back to check it. That's the instinct I want on this team.
Three things surprised me and need explaining before I can tell Tobias we're ready:
Questions for this week:
Keep the FX-adjusted view for USD/NOK on your list — that one's solid and the board will ask about it.
Thanks for the pace you're keeping on this.
Rosa
The analyst · week 2, night 4, request 3 of 3
Tonight's diagnosis solved the B2B mystery and it's bigger than expected. The native-currency report shows all 6 "zero" tenants have real, substantial activity (e.g. Ballycorran Industrial Supply: EUR, 136 real deals worth EUR575,400, 344 orders, 329 captured payments in 2025) - and the one thing all 6 broken tenants share is currency_code = EUR, while the 3 tenants that worked fine two nights ago are NOK, USD and GBP. That is conclusive: the prior query's EUR conversion silently dropped these tenants entirely, almost certainly via a join to exchange_rates for an EUR/EUR identity row that doesn't exist (several other engineers' notes this month explicitly called out and guarded against this exact gap, so it's a known but inconsistently-handled edge case, not a one-off fluke). This means my "B2B wholesale = 38.9% of revenue" number from two nights ago is a real understatement, not just imprecise, since it's missing two-thirds of the B2B tenant list entirely. The Castell Fasteners invoice detail reconciles reasonably well with Merchant Success's figure once you separate "total open AR" (EUR14,481) from "genuinely past due" (EUR~3,450, matching my 23.8% overdue share) - the remaining gap to their EUR5,080 is most likely snapshot-timing, not a real inconsistency, and I don't think it's worth another slot. The refund-share report shows Cragsley Outfitters refunds late at 94.2% vs an 83.7% company average - elevated but in the same range as several other retail peers (Velvet Ash 94.3%, Cinder Peak 93.7%), so it reads as "big merchant, slightly-above-average rate" rather than a uniquely broken merchant - closing that question out. Tonight's three slots: (1) redo the B2B revenue/receivables numbers with EUR correctly treated as identity (rate=1.0, no join needed) to get the real materiality figure, (2) quantify how much company-wide revenue this exact bug class would silently drop if it recurred, as hard evidence for Rosa's currency-handling question, (3) produce the final, corrected B2B receivables aging table for the board pack.
The engineer · 1 dry run on the empty schema, then the submission
WITH target_tenants AS ( SELECT id, name FROM tenants WHERE name IN ( 'Ballycorran Industrial Supply', 'Kontor24 Burobedarf', 'OfficeHub Nordic', 'Midland Safety Wear', 'Castell Fasteners', 'Hanse Industriebedarf', 'Polder Packaging', 'Deskline Supplies', 'Papeterie Grossiste' ) ), rates AS ( SELECT quote_code, rate FROM exchange_rates WHERE base_code = 'EUR' AND as_of = DATE '2025-12-31' ), inv AS ( SELECT i.id, i.amount_due, i.currency_code, i.due_at, CASE WHEN i.currency_code = 'EUR' THEN i.amount_due ELSE i.amount_due / r.rate END AS amount_due_eur, CASE WHEN i.due_at IS NULL OR i.due_at >= DATE '2025-12-31' THEN 'Not yet due' WHEN DATE '2025-12-31' - CAST(i.due_at AS date) BETWEEN 1 AND 30 THEN '1-30 days overdue' WHEN DATE '2025-12-31' - CAST(i.due_at AS date) BETWEEN 31 AND 60 THEN '31-60 days overdue' WHEN DATE '2025-12-31' - CAST(i.due_at AS date) BETWEEN 61 AND 90 THEN '61-90 days overdue' ELSE '90+ days overdue' END AS aging_bucket, CASE WHEN i.due_at IS NULL OR i.due_at >= DATE '2025-12-31' THEN 0 ELSE DATE '2025-12-31' - CAST(i.due_at AS date) END AS days_past_due FROM invoices i JOIN target_tenants t ON t.id = i.tenant_id LEFT JOIN rates r ON r.quote_code = i.currency_code WHERE i.status IN ('open', 'past_due') AND i.amount_due > 0 ), agg AS ( SELECT aging_bucket, COUNT(*) AS invoice_count, SUM(amount_due_eur) AS amount_due_eur, AVG(days_past_due) AS avg_days_past_due FROM inv GROUP BY aging_bucket ) SELECT aging_bucket, invoice_count, ROUND(amount_due_eur, 2) AS amount_due_eur, ROUND(amount_due_eur / SUM(amount_due_eur) OVER () * 100, 1) AS pct_of_total_amount_due, ROUND(avg_days_past_due, 1) AS avg_days_past_due FROM agg ORDER BY amount_due_eur DESC, aging_bucket ASC
“Query compiles. Submitting.”
“Submitted. The report buckets open/past-due invoices (amount_due > 0) for the 9 named B2B wholesale tenants as of 2025-12-31, converts non-EUR amounts via the EUR-based exchange_rates table (EUR itself passes through at 1.0), and returns one row per aging bucket ordered by amount_due_eur descending.”
As Yuki Tanaka saw it, from the small dataset the agents worked on
aging_bucket | invoice_count | amount_due_eur | pct_of_total_amount_due | avg_days_past_due -------------------+---------------+----------------+-------------------------+------------------ Not yet due | 66 | 74706.09 | 93.1 | 0.0 1-30 days overdue | 7 | 5140.32 | 6.4 | 7.0 31-60 days overdue | 1 | 358.26 | 0.4 | 33.0 (3 rows)
The memo and the queries that followed; links found by title, so inferred
It builds on:
Changed only where the benchmark needs the same result on every system
WITH target_tenants AS ( SELECT id, name FROM tenants WHERE name IN ( 'Ballycorran Industrial Supply', 'Kontor24 Burobedarf', 'OfficeHub Nordic', 'Midland Safety Wear', 'Castell Fasteners', 'Hanse Industriebedarf', 'Polder Packaging', 'Deskline Supplies', 'Papeterie Grossiste' )),rates AS ( SELECT quote_code, rate FROM exchange_rates WHERE base_code = 'EUR' AND as_of = DATE '2025-12-31'),inv AS ( SELECT i.id, i.amount_due, i.currency_code, i.due_at, i.id AS id, i.amount_due AS amount_due, i.currency_code AS currency_code, i.due_at AS due_at, CASE WHEN i.currency_code = 'EUR' THEN i.amount_due ELSE i.amount_due / r.rate ELSE CAST(i.amount_due / r.rate AS NUMERIC(18,6)) END AS amount_due_eur, CASE WHEN i.due_at IS NULL OR i.due_at >= DATE '2025-12-31' THEN 'Not yet due' WHEN DATE '2025-12-31' - CAST(i.due_at AS date) BETWEEN 1 AND 30 THEN '1-30 days overdue' WHEN DATE '2025-12-31' - CAST(i.due_at AS date) BETWEEN 31 AND 60 THEN '31-60 days overdue' WHEN DATE '2025-12-31' - CAST(i.due_at AS date) BETWEEN 61 AND 90 THEN '61-90 days overdue' ELSE '90+ days overdue' END AS aging_bucket, CASE WHEN i.due_at IS NULL OR i.due_at >= DATE '2025-12-31' THEN 0 ELSE DATE '2025-12-31' - CAST(i.due_at AS date) END AS days_past_due FROM invoices i JOIN target_tenants t ON t.id = i.tenant_id LEFT JOIN rates r ON r.quote_code = i.currency_code WHERE i.status IN ('open', 'past_due') AND i.amount_due > 0),agg AS ( SELECT aging_bucket, COUNT(*) AS invoice_count, SUM(amount_due_eur) AS amount_due_eur, AVG(days_past_due) AS avg_days_past_due AVG(CAST(days_past_due AS NUMERIC(18,6))) AS avg_days_past_due FROM inv GROUP BY aging_bucket)SELECT aging_bucket, invoice_count, ROUND(amount_due_eur, 2) AS amount_due_eur, ROUND(amount_due_eur / SUM(amount_due_eur) OVER () * 100, 1) AS pct_of_total_amount_due, ROUND(avg_days_past_due, 1) AS avg_days_past_due amount_due_eur AS amount_due_eur, amount_due_eur / SUM(amount_due_eur) OVER () * 100 AS pct_of_total_amount_due, avg_days_past_due AS avg_days_past_dueFROM aggORDER BY amount_due_eur DESC, aging_bucket ASCORDER BY amount_due_eur DESC NULLS FIRST, aging_bucket ASC NULLS LAST
WITH target_tenants AS ( SELECT id, name FROM tenants WHERE name IN ( 'Ballycorran Industrial Supply', 'Kontor24 Burobedarf', 'OfficeHub Nordic', 'Midland Safety Wear', 'Castell Fasteners', 'Hanse Industriebedarf', 'Polder Packaging', 'Deskline Supplies', 'Papeterie Grossiste' ) ), rates AS ( SELECT quote_code, rate FROM exchange_rates WHERE base_code = 'EUR' AND as_of = DATE '2025-12-31' ), inv AS ( SELECT i.id AS id, i.amount_due AS amount_due, i.currency_code AS currency_code, i.due_at AS due_at, CASE WHEN i.currency_code = 'EUR' THEN i.amount_due ELSE CAST(i.amount_due / r.rate AS NUMERIC(18,6)) END AS amount_due_eur, CASE WHEN i.due_at IS NULL OR i.due_at >= DATE '2025-12-31' THEN 'Not yet due' WHEN DATE '2025-12-31' - CAST(i.due_at AS date) BETWEEN 1 AND 30 THEN '1-30 days overdue' WHEN DATE '2025-12-31' - CAST(i.due_at AS date) BETWEEN 31 AND 60 THEN '31-60 days overdue' WHEN DATE '2025-12-31' - CAST(i.due_at AS date) BETWEEN 61 AND 90 THEN '61-90 days overdue' ELSE '90+ days overdue' END AS aging_bucket, CASE WHEN i.due_at IS NULL OR i.due_at >= DATE '2025-12-31' THEN 0 ELSE DATE '2025-12-31' - CAST(i.due_at AS date) END AS days_past_due FROM invoices i JOIN target_tenants t ON t.id = i.tenant_id LEFT JOIN rates r ON r.quote_code = i.currency_code WHERE i.status IN ('open', 'past_due') AND i.amount_due > 0 ), agg AS ( SELECT aging_bucket, COUNT(*) AS invoice_count, SUM(amount_due_eur) AS amount_due_eur, AVG(CAST(days_past_due AS NUMERIC(18,6))) AS avg_days_past_due FROM inv GROUP BY aging_bucket ) SELECT aging_bucket, invoice_count, amount_due_eur AS amount_due_eur, amount_due_eur / SUM(amount_due_eur) OVER () * 100 AS pct_of_total_amount_due, avg_days_past_due AS avg_days_past_due FROM agg ORDER BY amount_due_eur DESC NULLS FIRST, aging_bucket ASC 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 Yuki Tanaka (analyst for the Head of Data):
Title: Final B2B wholesale receivables aging (corrected)
Question: With the full, verified 9-tenant B2B wholesale list and EUR handled correctly, what does the receivables aging actually look like for the board pack?
Grain: receivables aging bucket
Columns:
- aging_bucket
- invoice_count
- amount_due_eur
- pct_of_total_amount_due
- avg_days_past_due
Details: Population: invoices belonging to the 9 verified B2B wholesale tenants (Ballycorran Industrial Supply, Kontor24 Burobedarf, OfficeHub Nordic, Midland Safety Wear, Castell Fasteners, Hanse Industriebedarf, Polder Packaging, Deskline Supplies, Papeterie Grossiste - the full list with at least one deals row), with status in ('open','past_due') and amount_due > 0, as of snapshot 2025-12-31. IMPORTANT: use correct EUR handling as in tonight's other requests - EUR amounts pass through at rate 1.0, never requiring a self-referential exchange_rates row. Bucket each invoice by days overdue (2025-12-31 minus due_at): 'Not yet due' (due_at is null or on/after 2025-12-31), '1-30 days overdue', '31-60 days overdue', '61-90 days overdue', '90+ days overdue'. invoice_count = count in bucket. amount_due_eur = sum of invoices.amount_due converted to EUR per the correct rule, using invoices.currency_code and as_of=2025-12-31. pct_of_total_amount_due = amount_due_eur / total across all buckets * 100, rounded to 1 decimal. avg_days_past_due = average (2025-12-31 minus due_at) in days for the bucket, rounded to 1 decimal, 0 for 'Not yet due'. Tables: invoices, tenants, exchange_rates. Order by amount_due_eur descending.
Check your query with the warehouse's dry_run tool, then hand it in with submit.dry_runWITH target_tenants AS ( SELECT id, name FROM tenants WHERE name IN ( 'Ballycorran Industrial Supply', 'Kontor24 Burobedarf', 'OfficeHub Nordic', 'Midland Safety Wear', 'Castell Fasteners', 'Hanse Industriebedarf', 'Polder Packaging', 'Deskline Supplies', 'Papeterie Grossiste' ) ), rates AS ( SELECT quote_code, rate FROM exchange_rates WHERE base_code = 'EUR' AND as_of = DATE '2025-12-31' ), inv AS ( SELECT i.id, i.amount_due, i.currency_code, i.due_at, CASE WHEN i.currency_code = 'EUR' THEN i.amount_due ELSE i.amount_due / r.rate END AS amount_due_eur, CASE WHEN i.due_at IS NULL OR i.due_at >= DATE '2025-12-31' THEN 'Not yet due' WHEN DATE '2025-12-31' - CAST(i.due_at AS date) BETWEEN 1 AND 30 THEN '1-30 days overdue' WHEN DATE '2025-12-31' - CAST(i.due_at AS date) BETWEEN 31 AND 60 THEN '31-60 days overdue' WHEN DATE '2025-12-31' - CAST(i.due_at AS date) BETWEEN 61 AND 90 THEN '61-90 days overdue' ELSE '90+ days overdue' END AS aging_bucket, CASE WHEN i.due_at IS NULL OR i.due_at >= DATE '2025-12-31' THEN 0 ELSE DATE '2025-12-31' - CAST(i.due_at AS date) END AS days_past_due FROM invoices i JOIN target_tenants t ON t.id = i.tenant_id LEFT JOIN rates r ON r.quote_code = i.currency_code WHERE i.status IN ('open', 'past_due') AND i.amount_due > 0 ), agg AS ( SELECT aging_bucket, COUNT(*) AS invoice_count, SUM(amount_due_eur) AS amount_due_eur, AVG(days_past_due) AS avg_days_past_due FROM inv GROUP BY aging_bucket ) SELECT aging_bucket, invoice_count, ROUND(amount_due_eur, 2) AS amount_due_eur, ROUND(amount_due_eur / SUM(amount_due_eur) OVER () * 100, 1) AS pct_of_total_amount_due, ROUND(avg_days_past_due, 1) AS avg_days_past_due FROM agg ORDER BY amount_due_eur DESC, aging_bucket ASC
STATUS: COMPILES (dry run on the empty schema, no data read) COLUMNS: aging_bucket, invoice_count, amount_due_eur, pct_of_total_amount_due, avg_days_past_due
submitWITH target_tenants AS ( SELECT id, name FROM tenants WHERE name IN ( 'Ballycorran Industrial Supply', 'Kontor24 Burobedarf', 'OfficeHub Nordic', 'Midland Safety Wear', 'Castell Fasteners', 'Hanse Industriebedarf', 'Polder Packaging', 'Deskline Supplies', 'Papeterie Grossiste' ) ), rates AS ( SELECT quote_code, rate FROM exchange_rates WHERE base_code = 'EUR' AND as_of = DATE '2025-12-31' ), inv AS ( SELECT i.id, i.amount_due, i.currency_code, i.due_at, CASE WHEN i.currency_code = 'EUR' THEN i.amount_due ELSE i.amount_due / r.rate END AS amount_due_eur, CASE WHEN i.due_at IS NULL OR i.due_at >= DATE '2025-12-31' THEN 'Not yet due' WHEN DATE '2025-12-31' - CAST(i.due_at AS date) BETWEEN 1 AND 30 THEN '1-30 days overdue' WHEN DATE '2025-12-31' - CAST(i.due_at AS date) BETWEEN 31 AND 60 THEN '31-60 days overdue' WHEN DATE '2025-12-31' - CAST(i.due_at AS date) BETWEEN 61 AND 90 THEN '61-90 days overdue' ELSE '90+ days overdue' END AS aging_bucket, CASE WHEN i.due_at IS NULL OR i.due_at >= DATE '2025-12-31' THEN 0 ELSE DATE '2025-12-31' - CAST(i.due_at AS date) END AS days_past_due FROM invoices i JOIN target_tenants t ON t.id = i.tenant_id LEFT JOIN rates r ON r.quote_code = i.currency_code WHERE i.status IN ('open', 'past_due') AND i.amount_due > 0 ), agg AS ( SELECT aging_bucket, COUNT(*) AS invoice_count, SUM(amount_due_eur) AS amount_due_eur, AVG(days_past_due) AS avg_days_past_due FROM inv GROUP BY aging_bucket ) SELECT aging_bucket, invoice_count, ROUND(amount_due_eur, 2) AS amount_due_eur, ROUND(amount_due_eur / SUM(amount_due_eur) OVER () * 100, 1) AS pct_of_total_amount_due, ROUND(avg_days_past_due, 1) AS avg_days_past_due FROM agg ORDER BY amount_due_eur DESC, aging_bucket ASC
Submitted to tonight's batch. Your work on this request is done.