Queries · q093 · Head of Data · week 1, night 2
“Once refunds are matched to the actual payments inside each payout (not just by calendar month), does net = gross - fee - refunds hold cleanly, confirming refunds are reflected once and only once in what we pay merchants?”
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 1
Subject: Certifying the key metrics before the board pack — what I need from you this week
Hi Yuki,
Tobias and I have to certify revenue, orders, customers, refunds and payouts before they go into the board pack. "Certify" means we're telling the board these numbers are reconcilable — so this week's job is to find out where the different places we record these figures agree, where they don't, and whether any gap is big enough to change the story we tell. If something doesn't reconcile, I need to know why and by how much, not just that it's "close enough." That feeds directly into the go/no-go Tobias and I give on the board pack, so please prioritize breadth across the five metrics over depth on any one of them — we can always dig deeper next week on whichever one looks shakiest.
You only have a handful of report slots a night, so here's what matters most to me, roughly in priority order:
Please flag anything you find inconsistent together with your best read of why, and roughly how big the discrepancy is in practice — that's what Tobias and I need to decide whether we can certify as-is, certify with a caveat, or need to hold a number back.
Thank you — I know this is a lot for one week. Let's talk before you spend your first slot tonight if you want to sanity-check the plan.
Rosa
Subject: What I need for the revenue/payout certification
Hi Yuki,
A few things from my side as you scope this week's work:
If anything else jumps out while you're in there — especially around currency conversion timing or refund treatment — flag it even if it's outside these three.
Thanks, Tobias
The analyst · week 1, night 2, request 3 of 3
Night 1 surfaced three things worth running down before I widen scope further: (1) orders_booked_revenue is consistently 15-40% above captured payments, and critically this gap does NOT shrink for old cohorts (e.g. 2023-04 orders are 2.5 years old and still show a 39% gap, 2025-01 orders are a year old and still show 15.5%) — that rules out simple timing lag and points to a structural cause (orders that never convert to a captured payment, e.g. abandoned/declined/cancelled), which matters a lot for "revenue" certification if orders.grand_total is ever quoted as revenue. I want to confirm this is explained by order status rather than something stranger. (2) invoice_paid_revenue is much smaller than order revenue and I'm not yet sure whether it's incremental (subscription billing on top of orders) or overlapping (already counted inside order revenue) — that changes whether we're under- or double-counting. (3) Report 3's net_check_diff_eur (net vs gross-fee) is large and growing, but shrinks a lot once refunds are subtracted too, suggesting the true formula is net ≈ gross - fee - refunds and refunds aren't double-counted — but I matched refunds by calendar month only, which is a loose proxy and leaves real noise (-6k to +21k swings). I want the structural, payment-linked version of that reconciliation via payout_items to see if the noise disappears, which would let us certify the payout formula with confidence rather than a caveat.
The engineer · 1 dry run on the empty schema, then the submission
WITH payout_eur AS ( SELECT po.id AS payout_id, cd.month_start, cd.month_label, CASE WHEN po.currency_code = 'EUR' THEN po.gross_amount ELSE po.gross_amount / er.rate END AS gross_eur, CASE WHEN po.currency_code = 'EUR' THEN po.fee_amount ELSE po.fee_amount / er.rate END AS fee_eur, CASE WHEN po.currency_code = 'EUR' THEN po.net_amount ELSE po.net_amount / er.rate END AS net_eur FROM payouts po JOIN calendar_days cd ON cd.calendar_date = CAST(po.paid_at AS date) LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = po.currency_code AND er.as_of = cd.calendar_date WHERE po.paid_at >= DATE '2023-01-01' AND po.paid_at < DATE '2026-01-01' ), refund_eur AS ( SELECT po.id AS payout_id, CASE WHEN pr.currency_code = 'EUR' THEN pr.amount ELSE pr.amount / er.rate END AS refund_eur FROM payouts po JOIN payout_items pi ON pi.payout_id = po.id JOIN payment_refunds pr ON pr.payment_id = pi.payment_id AND pr.status = 'succeeded' LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = pr.currency_code AND er.as_of = CAST(pr.processed_at AS date) WHERE po.paid_at >= DATE '2023-01-01' AND po.paid_at < DATE '2026-01-01' ), payout_month_agg AS ( SELECT month_start, month_label, SUM(gross_eur) AS payout_gross_eur, SUM(fee_eur) AS payout_fee_eur, SUM(net_eur) AS payout_net_eur FROM payout_eur GROUP BY month_start, month_label ), refund_month_agg AS ( SELECT pe.month_start, pe.month_label, SUM(re.refund_eur) AS linked_refunds_eur FROM refund_eur re JOIN (SELECT DISTINCT payout_id, month_start, month_label FROM payout_eur) pe ON pe.payout_id = re.payout_id GROUP BY pe.month_start, pe.month_label ), months AS ( SELECT DISTINCT month_start, month_label FROM calendar_days WHERE month_start >= DATE '2023-01-01' AND month_start <= DATE '2025-12-01' ) SELECT m.month_label AS month, ROUND(COALESCE(pg.payout_gross_eur,0),2) AS payout_gross_eur, ROUND(COALESCE(pg.payout_fee_eur,0),2) AS payout_fee_eur, ROUND(COALESCE(pg.payout_net_eur,0),2) AS payout_net_eur, ROUND(COALESCE(rf.linked_refunds_eur,0),2) AS linked_refunds_eur, ROUND(COALESCE(pg.payout_gross_eur,0) - COALESCE(pg.payout_fee_eur,0) - COALESCE(rf.linked_refunds_eur,0),2) AS implied_net_eur, ROUND(COALESCE(pg.payout_net_eur,0) - (COALESCE(pg.payout_gross_eur,0) - COALESCE(pg.payout_fee_eur,0) - COALESCE(rf.linked_refunds_eur,0)),2) AS diff_eur, ROUND( CASE WHEN COALESCE(pg.payout_net_eur,0) = 0 THEN NULL ELSE (COALESCE(pg.payout_net_eur,0) - (COALESCE(pg.payout_gross_eur,0) - COALESCE(pg.payout_fee_eur,0) - COALESCE(rf.linked_refunds_eur,0))) / pg.payout_net_eur * 100 END, 1) AS diff_pct FROM months m LEFT JOIN payout_month_agg pg ON pg.month_start = m.month_start LEFT JOIN refund_month_agg rf ON rf.month_start = m.month_start ORDER BY m.month_start
“Query compiles cleanly with the expected 8 columns at monthly grain. Submitting.”
“Submitted. The report builds a full 2023-01–2025-12 month series, aggregates payout gross/fee/net in EUR by paid_at month, separately sums payment-linked refunds (via payout_items→payments→payment_refunds, status='succeeded') converted to EUR by each refund's own processed_at date, then computes the implied net and the diff/diff_pct versus actual payout net — exactly as specified.”
As Yuki Tanaka saw it, from the small dataset the agents worked on
month | payout_gross_eur | payout_fee_eur | payout_net_eur | linked_refunds_eur | implied_net_eur | diff_eur | diff_pct --------+------------------+----------------+----------------+--------------------+-----------------+-----------+--------- 2023-01 | 94732.34 | 2358.26 | 89903.02 | 18424.65 | 73949.42 | 15953.6 | 17.7 2023-02 | 123011.03 | 2769.23 | 109079.28 | 11395.32 | 108846.49 | 232.79 | 0.2 2023-03 | 149703.05 | 3435.59 | 133587.23 | 19926.41 | 126341.05 | 7246.18 | 5.4 2023-04 | 138160.74 | 3227.3 | 112840.52 | 20267.35 | 114666.08 | -1825.57 | -1.6 2023-05 | 184243.19 | 4105.88 | 166767.36 | 16655.13 | 163482.17 | 3285.19 | 2.0 2023-06 | 116824.01 | 2853.18 | 99170.22 | 12630.82 | 101340.01 | -2169.8 | -2.2 2023-07 | 123832.57 | 2904.58 | 106823.44 | 12360.29 | 108567.71 | -1744.27 | -1.6 2023-08 | 146639.74 | 3253.97 | 129619.57 | 13411.78 | 129973.99 | -354.43 | -0.3 2023-09 | 179187.78 | 3957.2 | 161373.01 | 16827.87 | 158402.71 | 2970.3 | 1.8 2023-10 | 161532.9 | 3908.69 | 139980.97 | 20510.11 | 137114.1 | 2866.87 | 2.0 2023-11 | 374749.1 | 9037.78 | 337886.81 | 65953.12 | 299758.2 | 38128.61 | 11.3 2023-12 | 315250.22 | 7209.32 | 255711.71 | 40591.28 | 267449.62 | -11737.91 | -4.6 2024-01 | 214636.14 | 5121.57 | 158334.74 | 28614.61 | 180899.95 | -22565.21 | -14.3 2024-02 | 212084.59 | 5539.86 | 181653.19 | 16154.98 | 190389.75 | -8736.56 | -4.8 2024-03 | 304127.23 | 8182.78 | 279016.0 | 24766.99 | 271177.46 | 7838.55 | 2.8 2024-04 | 208772.66 | 5249.7 | 177512.11 | 24153.48 | 179369.48 | -1857.37 | -1.0 2024-05 | 211560.68 | 5641.83 | 185379.99 | 19649.83 | 186269.03 | -889.04 | -0.5 2024-06 | 192822.09 | 4767.01 | 166258.72 | 20773.68 | 167281.39 | -1022.67 | -0.6 2024-07 | 227390.96 | 5620.38 | 199746.14 | 39117.94 | 182652.63 | 17093.51 | 8.6 2024-08 | 189437.39 | 4710.35 | 163007.2 | 17924.01 | 166803.03 | -3795.83 | -2.3 2024-09 | 236448.09 | 5717.95 | 213008.92 | 18183.84 | 212546.29 | 462.63 | 0.2 2024-10 | 264461.29 | 6290.06 | 228906.86 | 35648.18 | 222523.05 | 6383.82 | 2.8 2024-11 | 317582.24 | 7443.25 | 279036.62 | 53387.35 | 256751.64 | 22284.98 | 8.0 2024-12 | 510885.63 | 11829.11 | 424628.34 | 91959.47 | 407097.05 | 17531.29 | 4.1 2025-01 | 336997.3 | 7925.21 | 253932.01 | 33021.1 | 296050.99 | -42118.98 | -16.6 2025-02 | 280023.05 | 6459.16 | 243182.77 | 16981.73 | 256582.16 | -13399.39 | -5.5 2025-03 | 255837.34 | 5985.68 | 223413.62 | 35238.5 | 214613.16 | 8800.46 | 3.9 2025-04 | 340937.07 | 7828.37 | 304687.99 | 32404.13 | 300704.57 | 3983.42 | 1.3 2025-05 | 334528.07 | 7178.24 | 278317.0 | 56865.11 | 270484.73 | 7832.27 | 2.8 2025-06 | 229136.55 | 5713.03 | 184551.27 | 23587.61 | 199835.92 | -15284.65 | -8.3 2025-07 | 239376.66 | 6028.64 | 203551.42 | 24232.38 | 209115.63 | -5564.21 | -2.7 2025-08 | 259058.79 | 6808.71 | 225922.27 | 48273.05 | 203977.03 | 21945.24 | 9.7 2025-09 | 259147.15 | 5871.99 | 227184.81 | 34190.15 | 219085.01 | 8099.8 | 3.6 2025-10 | 351845.4 | 8090.78 | 304170.97 | 50124.36 | 293630.26 | 10540.71 | 3.5 2025-11 | 471485.44 | 11427.35 | 412663.97 | 66072.53 | 393985.55 | 18678.41 | 4.5 2025-12 | 614615.16 | 14266.05 | 493277.91 | 71214.53 | 529134.58 | -35856.67 | -7.3 (36 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 payout_eur AS ( SELECT po.id AS payout_id, cd.month_start, cd.month_label, CASE WHEN po.currency_code = 'EUR' THEN po.gross_amount ELSE po.gross_amount / er.rate END AS gross_eur, CASE WHEN po.currency_code = 'EUR' THEN po.fee_amount ELSE po.fee_amount / er.rate END AS fee_eur, CASE WHEN po.currency_code = 'EUR' THEN po.net_amount ELSE po.net_amount / er.rate END AS net_eur cd.month_start AS month_start, cd.month_label AS month_label, CASE WHEN po.currency_code = 'EUR' THEN po.gross_amount ELSE CAST(po.gross_amount / er.rate AS NUMERIC(18,6)) END AS gross_eur, CASE WHEN po.currency_code = 'EUR' THEN po.fee_amount ELSE CAST(po.fee_amount / er.rate AS NUMERIC(18,6)) END AS fee_eur, CASE WHEN po.currency_code = 'EUR' THEN po.net_amount ELSE CAST(po.net_amount / er.rate AS NUMERIC(18,6)) END AS net_eur FROM payouts po JOIN calendar_days cd ON cd.calendar_date = CAST(po.paid_at AS date) LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = po.currency_code AND er.as_of = cd.calendar_date WHERE po.paid_at >= DATE '2023-01-01' AND po.paid_at < DATE '2026-01-01'),refund_eur AS ( SELECT po.id AS payout_id, CASE WHEN pr.currency_code = 'EUR' THEN pr.amount ELSE pr.amount / er.rate END AS refund_eur CASE WHEN pr.currency_code = 'EUR' THEN pr.amount ELSE CAST(pr.amount / er.rate AS NUMERIC(18,6)) END AS refund_eur FROM payouts po JOIN payout_items pi ON pi.payout_id = po.id JOIN payment_refunds pr ON pr.payment_id = pi.payment_id AND pr.status = 'succeeded' LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = pr.currency_code AND er.as_of = CAST(pr.processed_at AS date) WHERE po.paid_at >= DATE '2023-01-01' AND po.paid_at < DATE '2026-01-01'),payout_month_agg AS ( SELECT month_start, month_label, SUM(gross_eur) AS payout_gross_eur, SUM(fee_eur) AS payout_fee_eur, SUM(net_eur) AS payout_net_eur FROM payout_eur GROUP BY month_start, month_label),refund_month_agg AS ( SELECT pe.month_start, pe.month_label, SUM(re.refund_eur) AS linked_refunds_eur SELECT pe.month_start AS month_start, pe.month_label AS month_label, SUM(re.refund_eur) AS linked_refunds_eur FROM refund_eur re JOIN (SELECT DISTINCT payout_id, month_start, month_label FROM payout_eur) pe ON pe.payout_id = re.payout_id GROUP BY pe.month_start, pe.month_label),months AS ( SELECT DISTINCT month_start, month_label FROM calendar_days WHERE month_start >= DATE '2023-01-01' AND month_start <= DATE '2025-12-01')SELECT m.month_label AS month, ROUND(COALESCE(pg.payout_gross_eur,0),2) AS payout_gross_eur, ROUND(COALESCE(pg.payout_fee_eur,0),2) AS payout_fee_eur, ROUND(COALESCE(pg.payout_net_eur,0),2) AS payout_net_eur, ROUND(COALESCE(rf.linked_refunds_eur,0),2) AS linked_refunds_eur, ROUND(COALESCE(pg.payout_gross_eur,0) - COALESCE(pg.payout_fee_eur,0) - COALESCE(rf.linked_refunds_eur,0),2) AS implied_net_eur, ROUND(COALESCE(pg.payout_net_eur,0) - (COALESCE(pg.payout_gross_eur,0) - COALESCE(pg.payout_fee_eur,0) - COALESCE(rf.linked_refunds_eur,0)),2) AS diff_eur, ROUND( CASE WHEN COALESCE(pg.payout_net_eur,0) = 0 THEN NULL ELSE (COALESCE(pg.payout_net_eur,0) - (COALESCE(pg.payout_gross_eur,0) - COALESCE(pg.payout_fee_eur,0) - COALESCE(rf.linked_refunds_eur,0))) / pg.payout_net_eur * 100 END, 1) AS diff_pct COALESCE(pg.payout_gross_eur,0) AS payout_gross_eur, COALESCE(pg.payout_fee_eur,0) AS payout_fee_eur, COALESCE(pg.payout_net_eur,0) AS payout_net_eur, COALESCE(rf.linked_refunds_eur,0) AS linked_refunds_eur, COALESCE(pg.payout_gross_eur,0) - COALESCE(pg.payout_fee_eur,0) - COALESCE(rf.linked_refunds_eur,0) AS implied_net_eur, COALESCE(pg.payout_net_eur,0) - (COALESCE(pg.payout_gross_eur,0) - COALESCE(pg.payout_fee_eur,0) - COALESCE(rf.linked_refunds_eur,0)) AS diff_eur, CASE WHEN COALESCE(pg.payout_net_eur,0) = 0 THEN NULL ELSE (COALESCE(pg.payout_net_eur,0) - (COALESCE(pg.payout_gross_eur,0) - COALESCE(pg.payout_fee_eur,0) - COALESCE(rf.linked_refunds_eur,0))) / pg.payout_net_eur * 100 END AS diff_pctFROM months mLEFT JOIN payout_month_agg pg ON pg.month_start = m.month_startLEFT JOIN refund_month_agg rf ON rf.month_start = m.month_startORDER BY m.month_startORDER BY m.month_start NULLS LAST, m.month_label NULLS LAST, pg.month_label NULLS LAST, rf.month_label NULLS LAST
WITH payout_eur AS ( SELECT po.id AS payout_id, cd.month_start AS month_start, cd.month_label AS month_label, CASE WHEN po.currency_code = 'EUR' THEN po.gross_amount ELSE CAST(po.gross_amount / er.rate AS NUMERIC(18,6)) END AS gross_eur, CASE WHEN po.currency_code = 'EUR' THEN po.fee_amount ELSE CAST(po.fee_amount / er.rate AS NUMERIC(18,6)) END AS fee_eur, CASE WHEN po.currency_code = 'EUR' THEN po.net_amount ELSE CAST(po.net_amount / er.rate AS NUMERIC(18,6)) END AS net_eur FROM payouts po JOIN calendar_days cd ON cd.calendar_date = CAST(po.paid_at AS date) LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = po.currency_code AND er.as_of = cd.calendar_date WHERE po.paid_at >= DATE '2023-01-01' AND po.paid_at < DATE '2026-01-01' ), refund_eur AS ( SELECT po.id AS payout_id, CASE WHEN pr.currency_code = 'EUR' THEN pr.amount ELSE CAST(pr.amount / er.rate AS NUMERIC(18,6)) END AS refund_eur FROM payouts po JOIN payout_items pi ON pi.payout_id = po.id JOIN payment_refunds pr ON pr.payment_id = pi.payment_id AND pr.status = 'succeeded' LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = pr.currency_code AND er.as_of = CAST(pr.processed_at AS date) WHERE po.paid_at >= DATE '2023-01-01' AND po.paid_at < DATE '2026-01-01' ), payout_month_agg AS ( SELECT month_start, month_label, SUM(gross_eur) AS payout_gross_eur, SUM(fee_eur) AS payout_fee_eur, SUM(net_eur) AS payout_net_eur FROM payout_eur GROUP BY month_start, month_label ), refund_month_agg AS ( SELECT pe.month_start AS month_start, pe.month_label AS month_label, SUM(re.refund_eur) AS linked_refunds_eur FROM refund_eur re JOIN (SELECT DISTINCT payout_id, month_start, month_label FROM payout_eur) pe ON pe.payout_id = re.payout_id GROUP BY pe.month_start, pe.month_label ), months AS ( SELECT DISTINCT month_start, month_label FROM calendar_days WHERE month_start >= DATE '2023-01-01' AND month_start <= DATE '2025-12-01' ) SELECT m.month_label AS month, COALESCE(pg.payout_gross_eur,0) AS payout_gross_eur, COALESCE(pg.payout_fee_eur,0) AS payout_fee_eur, COALESCE(pg.payout_net_eur,0) AS payout_net_eur, COALESCE(rf.linked_refunds_eur,0) AS linked_refunds_eur, COALESCE(pg.payout_gross_eur,0) - COALESCE(pg.payout_fee_eur,0) - COALESCE(rf.linked_refunds_eur,0) AS implied_net_eur, COALESCE(pg.payout_net_eur,0) - (COALESCE(pg.payout_gross_eur,0) - COALESCE(pg.payout_fee_eur,0) - COALESCE(rf.linked_refunds_eur,0)) AS diff_eur, CASE WHEN COALESCE(pg.payout_net_eur,0) = 0 THEN NULL ELSE (COALESCE(pg.payout_net_eur,0) - (COALESCE(pg.payout_gross_eur,0) - COALESCE(pg.payout_fee_eur,0) - COALESCE(rf.linked_refunds_eur,0))) / pg.payout_net_eur * 100 END AS diff_pct FROM months m LEFT JOIN payout_month_agg pg ON pg.month_start = m.month_start LEFT JOIN refund_month_agg rf ON rf.month_start = m.month_start ORDER BY m.month_start NULLS LAST, m.month_label NULLS LAST, pg.month_label NULLS LAST, rf.month_label 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: Payout net formula check via payment-linked refunds Question: Once refunds are matched to the actual payments inside each payout (not just by calendar month), does net = gross - fee - refunds hold cleanly, confirming refunds are reflected once and only once in what we pay merchants? Grain: calendar month, 2023-01 through 2025-12, based on payout paid_at month Columns: - month - payout_gross_eur - payout_fee_eur - payout_net_eur - linked_refunds_eur - implied_net_eur - diff_eur - diff_pct Details: For each payout, identify its settled payments via payout_items (payout_items.payout_id = payouts.id, payout_items.payment_id = payments.id). linked_refunds_eur for a month = sum of payment_refunds.amount (status='succeeded') where payment_refunds.payment_id is one of the payments settled in a payout whose paid_at falls in that month, converted to EUR via exchange_rates using payment_refunds.currency_code and processed_at date (refunds counted regardless of when they were processed, i.e. a refund processed any time against a payment that ended up in this month's payout). payout_gross_eur/payout_fee_eur/payout_net_eur = sum of payouts.gross_amount/fee_amount/net_amount for payouts with paid_at in that month, converted to EUR via payouts.currency_code and paid_at date. implied_net_eur = payout_gross_eur - payout_fee_eur - linked_refunds_eur. diff_eur = payout_net_eur - implied_net_eur. diff_pct = diff_eur / payout_net_eur * 100, rounded to 1 decimal. Tables: payouts, payout_items, payments, payment_refunds, exchange_rates. Order by month ascending. Check your query with the warehouse's dry_run tool, then hand it in with submit.
dry_runWITH payout_eur AS ( SELECT po.id AS payout_id, cd.month_start, cd.month_label, CASE WHEN po.currency_code = 'EUR' THEN po.gross_amount ELSE po.gross_amount / er.rate END AS gross_eur, CASE WHEN po.currency_code = 'EUR' THEN po.fee_amount ELSE po.fee_amount / er.rate END AS fee_eur, CASE WHEN po.currency_code = 'EUR' THEN po.net_amount ELSE po.net_amount / er.rate END AS net_eur FROM payouts po JOIN calendar_days cd ON cd.calendar_date = CAST(po.paid_at AS date) LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = po.currency_code AND er.as_of = cd.calendar_date WHERE po.paid_at >= DATE '2023-01-01' AND po.paid_at < DATE '2026-01-01' ), refund_eur AS ( SELECT po.id AS payout_id, CASE WHEN pr.currency_code = 'EUR' THEN pr.amount ELSE pr.amount / er.rate END AS refund_eur FROM payouts po JOIN payout_items pi ON pi.payout_id = po.id JOIN payment_refunds pr ON pr.payment_id = pi.payment_id AND pr.status = 'succeeded' LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = pr.currency_code AND er.as_of = CAST(pr.processed_at AS date) WHERE po.paid_at >= DATE '2023-01-01' AND po.paid_at < DATE '2026-01-01' ), payout_month_agg AS ( SELECT month_start, month_label, SUM(gross_eur) AS payout_gross_eur, SUM(fee_eur) AS payout_fee_eur, SUM(net_eur) AS payout_net_eur FROM payout_eur GROUP BY month_start, month_label ), refund_month_agg AS ( SELECT pe.month_start, pe.month_label, SUM(re.refund_eur) AS linked_refunds_eur FROM refund_eur re JOIN (SELECT DISTINCT payout_id, month_start, month_label FROM payout_eur) pe ON pe.payout_id = re.payout_id GROUP BY pe.month_start, pe.month_label ), months AS ( SELECT DISTINCT month_start, month_label FROM calendar_days WHERE month_start >= DATE '2023-01-01' AND month_start <= DATE '2025-12-01' ) SELECT m.month_label AS month, ROUND(COALESCE(pg.payout_gross_eur,0),2) AS payout_gross_eur, ROUND(COALESCE(pg.payout_fee_eur,0),2) AS payout_fee_eur, ROUND(COALESCE(pg.payout_net_eur,0),2) AS payout_net_eur, ROUND(COALESCE(rf.linked_refunds_eur,0),2) AS linked_refunds_eur, ROUND(COALESCE(pg.payout_gross_eur,0) - COALESCE(pg.payout_fee_eur,0) - COALESCE(rf.linked_refunds_eur,0),2) AS implied_net_eur, ROUND(COALESCE(pg.payout_net_eur,0) - (COALESCE(pg.payout_gross_eur,0) - COALESCE(pg.payout_fee_eur,0) - COALESCE(rf.linked_refunds_eur,0)),2) AS diff_eur, ROUND( CASE WHEN COALESCE(pg.payout_net_eur,0) = 0 THEN NULL ELSE (COALESCE(pg.payout_net_eur,0) - (COALESCE(pg.payout_gross_eur,0) - COALESCE(pg.payout_fee_eur,0) - COALESCE(rf.linked_refunds_eur,0))) / pg.payout_net_eur * 100 END, 1) AS diff_pct FROM months m LEFT JOIN payout_month_agg pg ON pg.month_start = m.month_start LEFT JOIN refund_month_agg rf ON rf.month_start = m.month_start ORDER BY m.month_start
STATUS: COMPILES (dry run on the empty schema, no data read) COLUMNS: month, payout_gross_eur, payout_fee_eur, payout_net_eur, linked_refunds_eur, implied_net_eur, diff_eur, diff_pct
submitWITH payout_eur AS ( SELECT po.id AS payout_id, cd.month_start, cd.month_label, CASE WHEN po.currency_code = 'EUR' THEN po.gross_amount ELSE po.gross_amount / er.rate END AS gross_eur, CASE WHEN po.currency_code = 'EUR' THEN po.fee_amount ELSE po.fee_amount / er.rate END AS fee_eur, CASE WHEN po.currency_code = 'EUR' THEN po.net_amount ELSE po.net_amount / er.rate END AS net_eur FROM payouts po JOIN calendar_days cd ON cd.calendar_date = CAST(po.paid_at AS date) LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = po.currency_code AND er.as_of = cd.calendar_date WHERE po.paid_at >= DATE '2023-01-01' AND po.paid_at < DATE '2026-01-01' ), refund_eur AS ( SELECT po.id AS payout_id, CASE WHEN pr.currency_code = 'EUR' THEN pr.amount ELSE pr.amount / er.rate END AS refund_eur FROM payouts po JOIN payout_items pi ON pi.payout_id = po.id JOIN payment_refunds pr ON pr.payment_id = pi.payment_id AND pr.status = 'succeeded' LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = pr.currency_code AND er.as_of = CAST(pr.processed_at AS date) WHERE po.paid_at >= DATE '2023-01-01' AND po.paid_at < DATE '2026-01-01' ), payout_month_agg AS ( SELECT month_start, month_label, SUM(gross_eur) AS payout_gross_eur, SUM(fee_eur) AS payout_fee_eur, SUM(net_eur) AS payout_net_eur FROM payout_eur GROUP BY month_start, month_label ), refund_month_agg AS ( SELECT pe.month_start, pe.month_label, SUM(re.refund_eur) AS linked_refunds_eur FROM refund_eur re JOIN (SELECT DISTINCT payout_id, month_start, month_label FROM payout_eur) pe ON pe.payout_id = re.payout_id GROUP BY pe.month_start, pe.month_label ), months AS ( SELECT DISTINCT month_start, month_label FROM calendar_days WHERE month_start >= DATE '2023-01-01' AND month_start <= DATE '2025-12-01' ) SELECT m.month_label AS month, ROUND(COALESCE(pg.payout_gross_eur,0),2) AS payout_gross_eur, ROUND(COALESCE(pg.payout_fee_eur,0),2) AS payout_fee_eur, ROUND(COALESCE(pg.payout_net_eur,0),2) AS payout_net_eur, ROUND(COALESCE(rf.linked_refunds_eur,0),2) AS linked_refunds_eur, ROUND(COALESCE(pg.payout_gross_eur,0) - COALESCE(pg.payout_fee_eur,0) - COALESCE(rf.linked_refunds_eur,0),2) AS implied_net_eur, ROUND(COALESCE(pg.payout_net_eur,0) - (COALESCE(pg.payout_gross_eur,0) - COALESCE(pg.payout_fee_eur,0) - COALESCE(rf.linked_refunds_eur,0)),2) AS diff_eur, ROUND( CASE WHEN COALESCE(pg.payout_net_eur,0) = 0 THEN NULL ELSE (COALESCE(pg.payout_net_eur,0) - (COALESCE(pg.payout_gross_eur,0) - COALESCE(pg.payout_fee_eur,0) - COALESCE(rf.linked_refunds_eur,0))) / pg.payout_net_eur * 100 END, 1) AS diff_pct FROM months m LEFT JOIN payout_month_agg pg ON pg.month_start = m.month_start LEFT JOIN refund_month_agg rf ON rf.month_start = m.month_start ORDER BY m.month_start
Submitted to tonight's batch. Your work on this request is done.