Queries · q365 · Head of Pricing and Promotions · week 2, night 3
“For the 291 orphan orders whose customer's most-recent subscription is 'active', does that subscription's own start/end dates show it started after the order, show it marked active while already past its end date, or show dates that should have overlapped (meaning the original orphan flag was a bug)?”
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 3, request 1 of 2
Night 2's categorization fix produced a clean but puzzling result: all 291 active-bucket orders now land in a single "unexplained" category (100%, €11,746.39) — meaning none of them precede a first subscription and none fall between two stints. Logically that leaves only one explanation: the "active" subscription record itself doesn't actually cover the order date (otherwise the order wouldn't have been flagged as orphan in the first place), which most likely means either (a) that subscription started after the free order — a different, more benign situation than leakage — or (b) the subscription is marked status='active' but already has a past ended_at on it, a genuine data inconsistency where the status field disagrees with the subscription's own dates. I need to split these apart with a precise, narrow query before I can tell Marco whether this €11.7k belongs in the leakage total, is a separate curiosity, or signals a real bug in the original orphan flag.
The good news from night 2: the incrementality re-cut (matched by item count) shows positive lift in every single bucket (+13% to +117%), which is a much stronger, more defensible signal than last week's confounded numbers — I'm now reasonably confident the top discounts are associated with genuinely bigger baskets, not just selection into already-large carts. And the tenant-by-half-year leakage breakdown confirms the 2024-peak/2025-decline pattern is broad-based across all 6 tenants, not one outlier fixing itself — so "slow-burning historical gap, now resolving" is a solid, well-checked claim for the memo. Neither needs more digging.
Tonight's two priorities: (1) cleanly split the 291 "unexplained" active-bucket orders into their true underlying cause (started-after-order vs. active-status-but-already-ended vs. a genuine bug in the original orphan flag), which determines the final €-figure I give Tobias; (2) check whether "subscription marked active/trialing/past_due but already has a past ended_at" is a one-off to these 6 tenants or a platform-wide data-quality issue, since that changes how I'd frame the finding and who else should know about it. I'm holding back a third slot rather than manufacturing a question — the other open items (incrementality, trend concentration, Welcome10 drivers) are already well-answered and I'd rather save the slot for whatever the diagnostic turns up.
The engineer · 1 dry run on the empty schema, then the submission
WITH matching_discounts AS ( SELECT od.order_id, od.tenant_id, SUM(od.amount) AS total_discount_amount, MAX(od.currency_code) AS currency_code FROM order_discounts od JOIN discounts d ON d.id = od.discount_id AND d.tenant_id = od.tenant_id WHERE d.name = 'Subscription delivery (prepaid)' GROUP BY od.order_id, od.tenant_id ), orphan_orders AS ( SELECT o.id AS order_id, o.tenant_id, o.user_id, o.placed_at, s.started_at AS sub_started_at, s.ended_at AS sub_ended_at, md.total_discount_amount, md.currency_code FROM orders o JOIN matching_discounts md ON md.order_id = o.id AND md.tenant_id = o.tenant_id JOIN LATERAL ( SELECT sub.* FROM subscriptions sub WHERE sub.user_id = o.user_id AND sub.tenant_id = o.tenant_id ORDER BY sub.started_at DESC LIMIT 1 ) s ON true WHERE o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND s.status = 'active' AND NOT EXISTS ( SELECT 1 FROM subscriptions sub2 WHERE sub2.user_id = o.user_id AND sub2.tenant_id = o.tenant_id AND sub2.status IN ('active','trialing','past_due') AND sub2.started_at <= o.placed_at AND (sub2.ended_at IS NULL OR sub2.ended_at >= o.placed_at) ) ), classified AS ( SELECT oo.*, CASE WHEN sub_started_at > placed_at THEN 'subscription started after this order' WHEN sub_started_at <= placed_at AND sub_ended_at IS NOT NULL AND sub_ended_at < placed_at THEN 'active status but already past its end date' ELSE 'dates indicate overlap - likely a bug in the original orphan flag' END AS category, total_discount_amount / er.rate AS discount_amount_eur FROM orphan_orders oo LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = oo.currency_code AND er.as_of = CAST(oo.placed_at AS DATE) ) SELECT category, COUNT(*) AS order_count, ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER (), 1) AS pct_of_bucket, COUNT(DISTINCT user_id) AS distinct_customers, ROUND(SUM(discount_amount_eur), 2) AS total_discount_eur, SUM(COUNT(*)) OVER () AS check_total_291 FROM classified GROUP BY category ORDER BY order_count DESC, category
“Query compiles and returns the expected columns. Submitting.”
“Submitted. The report classifies the orphan-order population into the three mutually-exclusive date-comparison categories, with order count, % share, distinct customers, and EUR-converted discount totals per category, plus a running total column to verify the 291 check.”
As Oliver Grant saw it, from the small dataset the agents worked on
category | order_count | pct_of_bucket | distinct_customers | total_discount_eur | check_total_291 --------------------------------------+-------------+---------------+--------------------+--------------------+---------------- subscription started after this order | 291 | 100.0 | 60 | 3363.71 | 291 (1 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 matching_discounts AS ( SELECT od.order_id, od.tenant_id, SUM(od.amount) AS total_discount_amount, MAX(od.currency_code) AS currency_code SELECT od.order_id AS order_id, od.tenant_id AS tenant_id, SUM(od.amount) AS total_discount_amount, MAX(od.currency_code) AS currency_code FROM order_discounts od JOIN discounts d ON d.id = od.discount_id AND d.tenant_id = od.tenant_id WHERE d.name = 'Subscription delivery (prepaid)' GROUP BY od.order_id, od.tenant_id),orphan_orders AS ( SELECT o.id AS order_id, o.tenant_id, o.user_id, o.placed_at, o.tenant_id AS tenant_id, o.user_id AS user_id, o.placed_at AS placed_at, s.started_at AS sub_started_at, s.ended_at AS sub_ended_at, md.total_discount_amount, md.currency_code md.total_discount_amount AS total_discount_amount, md.currency_code AS currency_code FROM orders o JOIN matching_discounts md ON md.order_id = o.id AND md.tenant_id = o.tenant_id JOIN LATERAL ( SELECT sub.* FROM subscriptions sub WHERE sub.user_id = o.user_id AND sub.tenant_id = o.tenant_id ORDER BY sub.started_at DESC LIMIT 1 ORDER BY sub.started_at DESC NULLS FIRST, sub.id DESC NULLS FIRST {{fetch_first(1)}} ) s ON true WHERE o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND s.status = 'active' AND NOT EXISTS ( SELECT 1 FROM subscriptions sub2 WHERE sub2.user_id = o.user_id AND sub2.tenant_id = o.tenant_id AND sub2.status IN ('active','trialing','past_due') AND sub2.started_at <= o.placed_at AND (sub2.ended_at IS NULL OR sub2.ended_at >= o.placed_at) )),classified AS ( SELECT oo.*, CASE WHEN sub_started_at > placed_at THEN 'subscription started after this order' WHEN sub_started_at <= placed_at AND sub_ended_at IS NOT NULL AND sub_ended_at < placed_at THEN 'active status but already past its end date' ELSE 'dates indicate overlap - likely a bug in the original orphan flag' END AS category, total_discount_amount / er.rate AS discount_amount_eur CAST(total_discount_amount / er.rate AS NUMERIC(18,6)) AS discount_amount_eur FROM orphan_orders oo LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = oo.currency_code AND er.as_of = CAST(oo.placed_at AS DATE))SELECT category, COUNT(*) AS order_count, ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER (), 1) AS pct_of_bucket, 100.0 * COUNT(*) / SUM(COUNT(*)) OVER () AS pct_of_bucket, COUNT(DISTINCT user_id) AS distinct_customers, ROUND(SUM(discount_amount_eur), 2) AS total_discount_eur, SUM(discount_amount_eur) AS total_discount_eur, SUM(COUNT(*)) OVER () AS check_total_291FROM classifiedGROUP BY categoryORDER BY order_count DESC, categoryORDER BY order_count DESC NULLS FIRST, category NULLS LAST
WITH matching_discounts AS ( SELECT od.order_id AS order_id, od.tenant_id AS tenant_id, SUM(od.amount) AS total_discount_amount, MAX(od.currency_code) AS currency_code FROM order_discounts od JOIN discounts d ON d.id = od.discount_id AND d.tenant_id = od.tenant_id WHERE d.name = 'Subscription delivery (prepaid)' GROUP BY od.order_id, od.tenant_id ), orphan_orders AS ( SELECT o.id AS order_id, o.tenant_id AS tenant_id, o.user_id AS user_id, o.placed_at AS placed_at, s.started_at AS sub_started_at, s.ended_at AS sub_ended_at, md.total_discount_amount AS total_discount_amount, md.currency_code AS currency_code FROM orders o JOIN matching_discounts md ON md.order_id = o.id AND md.tenant_id = o.tenant_id JOIN LATERAL ( SELECT sub.* FROM subscriptions sub WHERE sub.user_id = o.user_id AND sub.tenant_id = o.tenant_id ORDER BY sub.started_at DESC NULLS FIRST, sub.id DESC NULLS FIRST FETCH FIRST 1 ROWS ONLY ) s ON true WHERE o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND s.status = 'active' AND NOT EXISTS ( SELECT 1 FROM subscriptions sub2 WHERE sub2.user_id = o.user_id AND sub2.tenant_id = o.tenant_id AND sub2.status IN ('active','trialing','past_due') AND sub2.started_at <= o.placed_at AND (sub2.ended_at IS NULL OR sub2.ended_at >= o.placed_at) ) ), classified AS ( SELECT oo.*, CASE WHEN sub_started_at > placed_at THEN 'subscription started after this order' WHEN sub_started_at <= placed_at AND sub_ended_at IS NOT NULL AND sub_ended_at < placed_at THEN 'active status but already past its end date' ELSE 'dates indicate overlap - likely a bug in the original orphan flag' END AS category, CAST(total_discount_amount / er.rate AS NUMERIC(18,6)) AS discount_amount_eur FROM orphan_orders oo LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = oo.currency_code AND er.as_of = CAST(oo.placed_at AS DATE) ) SELECT category, COUNT(*) AS order_count, 100.0 * COUNT(*) / SUM(COUNT(*)) OVER () AS pct_of_bucket, COUNT(DISTINCT user_id) AS distinct_customers, SUM(discount_amount_eur) AS total_discount_eur, SUM(COUNT(*)) OVER () AS check_total_291 FROM classified GROUP BY category ORDER BY order_count DESC NULLS FIRST, category NULLS LAST
WITH matching_discounts AS ( SELECT od.order_id AS order_id, od.tenant_id AS tenant_id, SUM(od.amount) AS total_discount_amount, MAX(od.currency_code) AS currency_code FROM order_discounts od JOIN discounts d ON d.id = od.discount_id AND d.tenant_id = od.tenant_id WHERE d.name = 'Subscription delivery (prepaid)' GROUP BY od.order_id, od.tenant_id ), orphan_orders AS ( SELECT o.id AS order_id, o.tenant_id AS tenant_id, o.user_id AS user_id, o.placed_at AS placed_at, s.started_at AS sub_started_at, s.ended_at AS sub_ended_at, md.total_discount_amount AS total_discount_amount, md.currency_code AS currency_code FROM orders o JOIN matching_discounts md ON md.order_id = o.id AND md.tenant_id = o.tenant_id JOIN LATERAL ( SELECT sub.* FROM subscriptions sub WHERE sub.user_id = o.user_id AND sub.tenant_id = o.tenant_id ORDER BY sub.started_at DESC NULLS FIRST, sub.id DESC NULLS FIRST LIMIT 1 ) s ON true WHERE o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND s.status = 'active' AND NOT EXISTS ( SELECT 1 FROM subscriptions sub2 WHERE sub2.user_id = o.user_id AND sub2.tenant_id = o.tenant_id AND sub2.status IN ('active','trialing','past_due') AND sub2.started_at <= o.placed_at AND (sub2.ended_at IS NULL OR sub2.ended_at >= o.placed_at) ) ), classified AS ( SELECT oo.*, CASE WHEN sub_started_at > placed_at THEN 'subscription started after this order' WHEN sub_started_at <= placed_at AND sub_ended_at IS NOT NULL AND sub_ended_at < placed_at THEN 'active status but already past its end date' ELSE 'dates indicate overlap - likely a bug in the original orphan flag' END AS category, CAST(total_discount_amount / er.rate AS NUMERIC(18,6)) AS discount_amount_eur FROM orphan_orders oo LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = oo.currency_code AND er.as_of = CAST(oo.placed_at AS DATE) ) SELECT category, COUNT(*) AS order_count, 100.0 * COUNT(*) / SUM(COUNT(*)) OVER () AS pct_of_bucket, COUNT(DISTINCT user_id) AS distinct_customers, SUM(discount_amount_eur) AS total_discount_eur, SUM(COUNT(*)) OVER () AS check_total_291 FROM classified GROUP BY category ORDER BY order_count DESC NULLS FIRST, 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: Root cause of the 291 'unexplained' active-bucket orphan orders Question: For the 291 orphan orders whose customer's most-recent subscription is 'active', does that subscription's own start/end dates show it started after the order, show it marked active while already past its end date, or show dates that should have overlapped (meaning the original orphan flag was a bug)? Grain: one row per explanation category, covering the full 291-order population Columns: - category: 'subscription started after this order' (S.started_at > order.placed_at) / 'active status but already past its end date' (S.started_at <= placed_at AND S.ended_at is not null AND S.ended_at < placed_at) / 'dates indicate overlap - likely a bug in the original orphan flag' (S.started_at <= placed_at AND (S.ended_at is null OR S.ended_at >= placed_at)) - order count - % of the 291-order bucket - distinct customers - total discount amount (EUR) Details: Source: the exact same 291 orders from last week's report (order_discounts + discounts name='Subscription delivery (prepaid)' + orders placed_at 2025, status != 'cancelled', no overlapping active/trialing/past_due subscription, most-recent subscription by started_at has status='active'). For each order, use S = that same most-recent-by-started_at subscription row (not a different join), and classify purely by comparing S.started_at and S.ended_at to order.placed_at per the three mutually-exclusive, exhaustive categories above. Convert discount amounts to EUR via exchange_rates (base_code='EUR', quote_code=currency_code, as_of=placed_at). Add an explicit check that the three category counts sum to 291. Order by order count descending. Print all categories. Check your query with the warehouse's dry_run tool, then hand it in with submit.
dry_runWITH matching_discounts AS ( SELECT od.order_id, od.tenant_id, SUM(od.amount) AS total_discount_amount, MAX(od.currency_code) AS currency_code FROM order_discounts od JOIN discounts d ON d.id = od.discount_id AND d.tenant_id = od.tenant_id WHERE d.name = 'Subscription delivery (prepaid)' GROUP BY od.order_id, od.tenant_id ), orphan_orders AS ( SELECT o.id AS order_id, o.tenant_id, o.user_id, o.placed_at, s.started_at AS sub_started_at, s.ended_at AS sub_ended_at, md.total_discount_amount, md.currency_code FROM orders o JOIN matching_discounts md ON md.order_id = o.id AND md.tenant_id = o.tenant_id JOIN LATERAL ( SELECT sub.* FROM subscriptions sub WHERE sub.user_id = o.user_id AND sub.tenant_id = o.tenant_id ORDER BY sub.started_at DESC LIMIT 1 ) s ON true WHERE o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND s.status = 'active' AND NOT EXISTS ( SELECT 1 FROM subscriptions sub2 WHERE sub2.user_id = o.user_id AND sub2.tenant_id = o.tenant_id AND sub2.status IN ('active','trialing','past_due') AND sub2.started_at <= o.placed_at AND (sub2.ended_at IS NULL OR sub2.ended_at >= o.placed_at) ) ), classified AS ( SELECT oo.*, CASE WHEN sub_started_at > placed_at THEN 'subscription started after this order' WHEN sub_started_at <= placed_at AND sub_ended_at IS NOT NULL AND sub_ended_at < placed_at THEN 'active status but already past its end date' ELSE 'dates indicate overlap - likely a bug in the original orphan flag' END AS category, total_discount_amount / er.rate AS discount_amount_eur FROM orphan_orders oo LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = oo.currency_code AND er.as_of = CAST(oo.placed_at AS DATE) ) SELECT category, COUNT(*) AS order_count, ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER (), 1) AS pct_of_bucket, COUNT(DISTINCT user_id) AS distinct_customers, ROUND(SUM(discount_amount_eur), 2) AS total_discount_eur, SUM(COUNT(*)) OVER () AS check_total_291 FROM classified GROUP BY category ORDER BY order_count DESC, category
STATUS: COMPILES (dry run on the empty schema, no data read) COLUMNS: category, order_count, pct_of_bucket, distinct_customers, total_discount_eur, check_total_291
submitWITH matching_discounts AS ( SELECT od.order_id, od.tenant_id, SUM(od.amount) AS total_discount_amount, MAX(od.currency_code) AS currency_code FROM order_discounts od JOIN discounts d ON d.id = od.discount_id AND d.tenant_id = od.tenant_id WHERE d.name = 'Subscription delivery (prepaid)' GROUP BY od.order_id, od.tenant_id ), orphan_orders AS ( SELECT o.id AS order_id, o.tenant_id, o.user_id, o.placed_at, s.started_at AS sub_started_at, s.ended_at AS sub_ended_at, md.total_discount_amount, md.currency_code FROM orders o JOIN matching_discounts md ON md.order_id = o.id AND md.tenant_id = o.tenant_id JOIN LATERAL ( SELECT sub.* FROM subscriptions sub WHERE sub.user_id = o.user_id AND sub.tenant_id = o.tenant_id ORDER BY sub.started_at DESC LIMIT 1 ) s ON true WHERE o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND s.status = 'active' AND NOT EXISTS ( SELECT 1 FROM subscriptions sub2 WHERE sub2.user_id = o.user_id AND sub2.tenant_id = o.tenant_id AND sub2.status IN ('active','trialing','past_due') AND sub2.started_at <= o.placed_at AND (sub2.ended_at IS NULL OR sub2.ended_at >= o.placed_at) ) ), classified AS ( SELECT oo.*, CASE WHEN sub_started_at > placed_at THEN 'subscription started after this order' WHEN sub_started_at <= placed_at AND sub_ended_at IS NOT NULL AND sub_ended_at < placed_at THEN 'active status but already past its end date' ELSE 'dates indicate overlap - likely a bug in the original orphan flag' END AS category, total_discount_amount / er.rate AS discount_amount_eur FROM orphan_orders oo LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = oo.currency_code AND er.as_of = CAST(oo.placed_at AS DATE) ) SELECT category, COUNT(*) AS order_count, ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER (), 1) AS pct_of_bucket, COUNT(DISTINCT user_id) AS distinct_customers, ROUND(SUM(discount_amount_eur), 2) AS total_discount_eur, SUM(COUNT(*)) OVER () AS check_total_291 FROM classified GROUP BY category ORDER BY order_count DESC, category
Submitted to tonight's batch. Your work on this request is done.