Queries · q368 · Head of Pricing and Promotions · week 2, night 4
“Using the same single-pass, fan-out-safe construction, does the full-history orphan population by tenant and status reproduce the monthly trend's cumulative canceled-bucket total (~€57,658.66, roughly 1,188 orders by hand-summing last week's monthly report)?”
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
Finance questions the cost of discounts. Which promotions and coupons brought additional sales, which only gave away margin, and is anything being misused?
The manager, at the start of week 2
Subject: Good work — here's where I want to go next
Oliver,
This is exactly the kind of memo I needed — it separates real findings from artifacts, which is rarer than it should be. A few reactions and where I want to push next week.
The lapsed-subscriber giveaway (~€57k) is now the headline for Tobias, not the promo/coupon story — good catch pulling it out. Before I take that number upstairs, I want it airtight. And the Welcome10 broad refund pattern is the one piece of "margin erosion" I think is genuinely actionable rather than a sample-size illusion, so I want to go a layer deeper there before we talk about capping anything.
On incrementality — I accept we may never get a clean causal answer here, but I'd like one more honest attempt before I write it off as "probably just bigger carts clear the threshold anyway."
Questions for this week:
No need to re-litigate the B2B volume-tier outlier — just keep it excluded as you planned. Let's talk Wednesday before the last batch so I can steer based on what the orphan-subscription check turns up.
Thanks, Marco
The analyst · week 2, night 4, request 2 of 3
Before writing anything near-final for Tobias, I need to flag something I should have caught earlier: the numbers across this whole investigation don't reconcile with each other. The original per-tenant orphan count for 2025 (night before last, summing Grind Collective 167 + Honeycomb Larder 127 + Kibblewick 100 + Feather & Fin 49 + Saffron & Sage 17 + The Balm Room 1 = 461 orders, €22,318.18 total) is far smaller than the "same population" reported a night later when split by subscription status (1,322 canceled + 291 active = 1,613 orders, €68,914.08 total) — a ~3.5x gap that I didn't catch before sending Friday's memo. And tonight's attempt to classify the 291 "active" orders by date logic came back internally inconsistent on its own terms: it reports 291 orders in one category but a discount total (€3,363.71) that matches a completely different, smaller 88-order subset from two nights ago — the count and the amount don't belong to the same population. Three different engineers independently "rebuilding the population from scratch" each night has clearly introduced drift (likely a 2025-only vs. full-history mismatch, and probably a join fan-out on the amount side).
I'm treating this as the priority for my last night: I'd rather hand Marco a number I've personally reconciled than compound a questionable one. All three requests tonight are single, self-contained, fan-out-safe queries that explicitly validate against previously-known, trusted reference totals, so I can see exactly where the drift is before writing anything definitive:
I won't write the final headline number until I see these three agree with each other and with the known reference totals.
The engineer · 1 dry run on the empty schema, then the submission
WITH order_sub AS ( SELECT o.id AS order_id, o.tenant_id, o.user_id, o.discount_total, o.currency_code, o.placed_at, ( SELECT s.status FROM invoices i JOIN subscriptions s ON s.id = i.subscription_id WHERE i.order_id = o.id ORDER BY i.issued_at DESC NULLS LAST, i.id DESC LIMIT 1 ) AS subscription_status FROM orders o WHERE o.placed_at >= DATE '2023-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND o.discount_total > 0 AND NOT EXISTS ( SELECT 1 FROM order_discounts od WHERE od.order_id = o.id ) ), rated AS ( SELECT os.tenant_id, os.user_id, os.order_id, CASE WHEN os.subscription_status = 'canceled' THEN 'canceled' WHEN os.subscription_status = 'active' THEN 'active' ELSE 'other-or-no-subscription-record' END AS status_category, CASE WHEN os.currency_code = 'EUR' THEN os.discount_total ELSE os.discount_total / er.rate END AS discount_eur FROM order_sub os LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = os.currency_code AND er.as_of = os.placed_at::date ) SELECT t.name AS tenant_name, r.status_category, COUNT(*) AS orphan_order_count, COUNT(DISTINCT r.user_id) AS distinct_customers, ROUND(SUM(r.discount_eur), 2) AS total_discount_eur FROM rated r JOIN tenants t ON t.id = r.tenant_id GROUP BY t.name, r.status_category ORDER BY t.name, r.status_category
“Submitted. Summary of what I did and why:
- Built the orphan set in a single non-fan-out pass: a per-order subquery flags orders (2023‑01‑01 to 2025‑12‑31, status <> 'cancelled') that have discount_total > 0 but no corresponding order_discounts row (NOT EXISTS), which is the fan-out-safe way to do this since order_discounts can have multiple rows per order.
- Attached each orphan order to the status of its most recent linked subscription (via invoices.subscription_id) using a scalar subquery (ORDER BY issued_at DESC LIMIT 1) — again avoiding any join fan-out — then bucketed into canceled / active / other-or-no-subscription-record.
- Converted discount_total to EUR via exchange_rates (base EUR, quote = order currency, as_of = placed_at date), treating EUR orders as already in EUR.
- Aggregated to tenant × status_category with order count, distinct customers, and EUR discount total, ordered by tenant then category.
Flagged two caveats in the note: I had to infer the "orphan" definition since the earlier 2025-only request wasn't visible to me in this session, and as the engineer I never see query output (dry_run uses an empty schema), so I can't personally verify the ~1,188 orders / ~€57,658.66 reconciliation — that comparison needs to happen once the analyst has the printed report.”
As Oliver Grant saw it, from the small dataset the agents worked on
tenant_name | status_category | orphan_order_count | distinct_customers | total_discount_eur ------------+-----------------+--------------------+--------------------+------------------- (0 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 order_sub AS ( SELECT o.id AS order_id, o.tenant_id, o.user_id, o.discount_total, o.currency_code, o.placed_at, o.tenant_id AS tenant_id, o.user_id AS user_id, o.discount_total AS discount_total, o.currency_code AS currency_code, o.placed_at AS placed_at, ( SELECT s.status FROM invoices i JOIN subscriptions s ON s.id = i.subscription_id WHERE i.order_id = o.id ORDER BY i.issued_at DESC NULLS LAST, i.id DESC LIMIT 1 ORDER BY i.issued_at DESC NULLS LAST, i.id DESC NULLS FIRST {{fetch_first(1)}} ) AS subscription_status FROM orders o WHERE o.placed_at >= DATE '2023-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND o.discount_total > 0 AND NOT EXISTS ( SELECT 1 FROM order_discounts od WHERE od.order_id = o.id )),rated AS ( SELECT os.tenant_id, os.user_id, os.order_id, os.tenant_id AS tenant_id, os.user_id AS user_id, os.order_id AS order_id, CASE WHEN os.subscription_status = 'canceled' THEN 'canceled' WHEN os.subscription_status = 'active' THEN 'active' ELSE 'other-or-no-subscription-record' END AS status_category, CASE WHEN os.currency_code = 'EUR' THEN os.discount_total ELSE os.discount_total / er.rate ELSE CAST(os.discount_total / er.rate AS NUMERIC(18,6)) END AS discount_eur FROM order_sub os LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = os.currency_code AND er.as_of = os.placed_at::date AND er.as_of = CAST(os.placed_at AS date))SELECT t.name AS tenant_name, r.status_category, r.status_category AS status_category, COUNT(*) AS orphan_order_count, COUNT(DISTINCT r.user_id) AS distinct_customers, ROUND(SUM(r.discount_eur), 2) AS total_discount_eur SUM(r.discount_eur) AS total_discount_eurFROM rated rJOIN tenants t ON t.id = r.tenant_idGROUP BY t.name, r.status_categoryORDER BY t.name, r.status_categoryORDER BY t.name NULLS LAST, r.status_category NULLS LAST
WITH order_sub AS ( SELECT o.id AS order_id, o.tenant_id AS tenant_id, o.user_id AS user_id, o.discount_total AS discount_total, o.currency_code AS currency_code, o.placed_at AS placed_at, ( SELECT s.status FROM invoices i JOIN subscriptions s ON s.id = i.subscription_id WHERE i.order_id = o.id ORDER BY i.issued_at DESC NULLS LAST, i.id DESC NULLS FIRST FETCH FIRST 1 ROWS ONLY ) AS subscription_status FROM orders o WHERE o.placed_at >= DATE '2023-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND o.discount_total > 0 AND NOT EXISTS ( SELECT 1 FROM order_discounts od WHERE od.order_id = o.id ) ), rated AS ( SELECT os.tenant_id AS tenant_id, os.user_id AS user_id, os.order_id AS order_id, CASE WHEN os.subscription_status = 'canceled' THEN 'canceled' WHEN os.subscription_status = 'active' THEN 'active' ELSE 'other-or-no-subscription-record' END AS status_category, CASE WHEN os.currency_code = 'EUR' THEN os.discount_total ELSE CAST(os.discount_total / er.rate AS NUMERIC(18,6)) END AS discount_eur FROM order_sub os LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = os.currency_code AND er.as_of = CAST(os.placed_at AS date) ) SELECT t.name AS tenant_name, r.status_category AS status_category, COUNT(*) AS orphan_order_count, COUNT(DISTINCT r.user_id) AS distinct_customers, SUM(r.discount_eur) AS total_discount_eur FROM rated r JOIN tenants t ON t.id = r.tenant_id GROUP BY t.name, r.status_category ORDER BY t.name NULLS LAST, r.status_category NULLS LAST
WITH order_sub AS ( SELECT o.id AS order_id, o.tenant_id AS tenant_id, o.user_id AS user_id, o.discount_total AS discount_total, o.currency_code AS currency_code, o.placed_at AS placed_at, ( SELECT s.status FROM invoices i JOIN subscriptions s ON s.id = i.subscription_id WHERE i.order_id = o.id ORDER BY i.issued_at DESC NULLS LAST, i.id DESC NULLS FIRST LIMIT 1 ) AS subscription_status FROM orders o WHERE o.placed_at >= DATE '2023-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND o.discount_total > 0 AND NOT EXISTS ( SELECT 1 FROM order_discounts od WHERE od.order_id = o.id ) ), rated AS ( SELECT os.tenant_id AS tenant_id, os.user_id AS user_id, os.order_id AS order_id, CASE WHEN os.subscription_status = 'canceled' THEN 'canceled' WHEN os.subscription_status = 'active' THEN 'active' ELSE 'other-or-no-subscription-record' END AS status_category, CASE WHEN os.currency_code = 'EUR' THEN os.discount_total ELSE CAST(os.discount_total / er.rate AS NUMERIC(18,6)) END AS discount_eur FROM order_sub os LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = os.currency_code AND er.as_of = CAST(os.placed_at AS date) ) SELECT t.name AS tenant_name, r.status_category AS status_category, COUNT(*) AS orphan_order_count, COUNT(DISTINCT r.user_id) AS distinct_customers, SUM(r.discount_eur) AS total_discount_eur FROM rated r JOIN tenants t ON t.id = r.tenant_id GROUP BY t.name, r.status_category ORDER BY t.name NULLS LAST, r.status_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 Oliver Grant (analyst for the Head of Pricing and Promotions): Title: Reconciled full-history (2023-2025) orphan population by tenant and subscription status (single pass) Question: Using the same single-pass, fan-out-safe construction, does the full-history orphan population by tenant and status reproduce the monthly trend's cumulative canceled-bucket total (~€57,658.66, roughly 1,188 orders by hand-summing last week's monthly report)? Grain: one row per tenant and subscription-status category (canceled / active / other-or-no-subscription-record) Columns: - tenant name - status category - orphan order count - distinct customers - total discount amount (EUR) Details: Identical construction and definitions to the 2025-only version above (one row per order_id built first via subqueries/window functions, no fan-out joins, then aggregated), but over the full history: orders.placed_at from 2023-01-01 to 2025-12-31, status != 'cancelled'. Convert discount amounts to EUR via exchange_rates (base_code='EUR', quote_code=currency_code, as_of=placed_at). In the engineer's note, state the grand total order count and EUR total for the 'canceled' category across all tenants, and flag whether it's close to the reference figures above (~1,188 orders, ~€57,658.66) or diverges, with a hypothesis why if it diverges. Order by tenant name, then status category. Print all rows. Check your query with the warehouse's dry_run tool, then hand it in with submit.
dry_runWITH order_sub AS ( SELECT o.id AS order_id, o.tenant_id, o.user_id, o.discount_total, o.currency_code, o.placed_at, ( SELECT s.status FROM invoices i JOIN subscriptions s ON s.id = i.subscription_id WHERE i.order_id = o.id ORDER BY i.issued_at DESC NULLS LAST, i.id DESC LIMIT 1 ) AS subscription_status FROM orders o WHERE o.placed_at >= DATE '2023-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND o.discount_total > 0 AND NOT EXISTS ( SELECT 1 FROM order_discounts od WHERE od.order_id = o.id ) ), rated AS ( SELECT os.tenant_id, os.user_id, os.order_id, CASE WHEN os.subscription_status = 'canceled' THEN 'canceled' WHEN os.subscription_status = 'active' THEN 'active' ELSE 'other-or-no-subscription-record' END AS status_category, CASE WHEN os.currency_code = 'EUR' THEN os.discount_total ELSE os.discount_total / er.rate END AS discount_eur FROM order_sub os LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = os.currency_code AND er.as_of = os.placed_at::date ) SELECT t.name AS tenant_name, r.status_category, COUNT(*) AS orphan_order_count, COUNT(DISTINCT r.user_id) AS distinct_customers, ROUND(SUM(r.discount_eur), 2) AS total_discount_eur FROM rated r JOIN tenants t ON t.id = r.tenant_id GROUP BY t.name, r.status_category ORDER BY t.name, r.status_category
STATUS: COMPILES (dry run on the empty schema, no data read) COLUMNS: tenant_name, status_category, orphan_order_count, distinct_customers, total_discount_eur
submitWITH order_sub AS ( SELECT o.id AS order_id, o.tenant_id, o.user_id, o.discount_total, o.currency_code, o.placed_at, ( SELECT s.status FROM invoices i JOIN subscriptions s ON s.id = i.subscription_id WHERE i.order_id = o.id ORDER BY i.issued_at DESC NULLS LAST, i.id DESC LIMIT 1 ) AS subscription_status FROM orders o WHERE o.placed_at >= DATE '2023-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND o.discount_total > 0 AND NOT EXISTS ( SELECT 1 FROM order_discounts od WHERE od.order_id = o.id ) ), rated AS ( SELECT os.tenant_id, os.user_id, os.order_id, CASE WHEN os.subscription_status = 'canceled' THEN 'canceled' WHEN os.subscription_status = 'active' THEN 'active' ELSE 'other-or-no-subscription-record' END AS status_category, CASE WHEN os.currency_code = 'EUR' THEN os.discount_total ELSE os.discount_total / er.rate END AS discount_eur FROM order_sub os LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = os.currency_code AND er.as_of = os.placed_at::date ) SELECT t.name AS tenant_name, r.status_category, COUNT(*) AS orphan_order_count, COUNT(DISTINCT r.user_id) AS distinct_customers, ROUND(SUM(r.discount_eur), 2) AS total_discount_eur FROM rated r JOIN tenants t ON t.id = r.tenant_id GROUP BY t.name, r.status_category ORDER BY t.name, r.status_category
Submitted to tonight's batch. Your work on this request is done.