Queries · q157 · Head of Pricing and Promotions · week 1, night 4
“What subscription status do the 'orphan' Subscription-delivery-discount customers (no active subscription overlap found) actually have on record, since neither 'lapsed within 30 days' nor 'no subscription at all' explained them?”
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 1
Subject: Priorities for this week's discount cost-review with Tobias
Oliver,
Tobias and I need a credible answer to "are our promotions and coupons earning their keep," in time for Friday's memo. This feeds a joint decision on which promotions/coupons we keep, retire, or cap going into next quarter — so I'd rather have four solid, well-checked answers than a pile of rushed ones. Please pick the nights' reports accordingly and flag anything suspicious you find along the way rather than burying it in a footnote.
Here's what I actually want to know, in my own words:
Keep each report to one clear question — I'd rather get three clean answers this week than one bundled one that gets bounced. Let's sync briefly before you submit Thursday's batch in case the first results change what we ask.
Thanks, Marco
Subject: Finance's angle on the promotions/coupons review
Oliver,
As you and Marco dig into discount performance this week, a few things I'd like the analysis to keep an eye on from the finance side:
Keep it tight; I mainly need the "so what" for the annual accounts and the 2026 budget assumptions on discount cost.
Thanks, Tobias
The analyst · week 1, night 4, request 1 of 3
Last night before the memo, so I'm closing out the three loose threads that actually change what I tell Marco and Tobias, and skipping anything merely confirmatory:
These three reports close out Marco's Q3/Q4 and Tobias's Q1/Q2 with concrete, checked numbers for Friday.
The engineer · 2 dry runs on the empty schema, then the submission
WITH 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, od.amount AS discount_amount, od.currency_code AS discount_currency FROM orders o JOIN order_discounts od ON od.order_id = o.id AND od.tenant_id = o.tenant_id JOIN discounts d ON d.id = od.discount_id AND d.tenant_id = o.tenant_id WHERE d.name = 'Subscription delivery (prepaid)' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND NOT EXISTS ( SELECT 1 FROM subscriptions s WHERE s.tenant_id = o.tenant_id AND s.user_id = o.user_id AND s.status IN ('trialing','active','past_due') AND s.started_at <= o.placed_at AND (s.ended_at IS NULL OR s.ended_at >= o.placed_at) ) ), latest_subscription AS ( SELECT s.tenant_id AS tenant_id, s.user_id AS user_id, s.status AS status, s.ended_at AS ended_at, s.started_at AS started_at, ROW_NUMBER() OVER (PARTITION BY s.tenant_id, s.user_id ORDER BY s.started_at DESC, s.id DESC) AS rn FROM subscriptions s ), orphan_with_category AS ( SELECT oo.order_id AS order_id, oo.tenant_id AS tenant_id, oo.user_id AS user_id, oo.placed_at AS placed_at, oo.discount_amount AS discount_amount, oo.discount_currency AS discount_currency, COALESCE(ls.status, 'no_subscription_record') AS status_category, ls.ended_at AS sub_ended_at FROM orphan_orders oo LEFT JOIN latest_subscription ls ON ls.tenant_id = oo.tenant_id AND ls.user_id = oo.user_id AND ls.rn = 1 ), t0 AS ( SELECT DISTINCT status_category FROM orphan_with_category ), t1 AS ( SELECT status_category, COUNT(DISTINCT tenant_id, user_id) AS orphan_customer_count FROM orphan_with_category GROUP BY status_category ), t2 AS ( SELECT status_category, COUNT(*) AS orphan_order_count FROM orphan_with_category GROUP BY status_category ), t3 AS ( SELECT COUNT(*) AS total_orphan_orders FROM orphan_with_category ), t4 AS ( SELECT status_category, AVG(COALESCE(sub_ended_at::date, DATE '2026-01-05') - placed_at::date) AS avg_days_sub_end_to_order FROM orphan_with_category WHERE status_category <> 'no_subscription_record' GROUP BY status_category ), t5 AS ( SELECT ow.status_category, SUM( CASE WHEN ow.discount_currency = 'EUR' THEN ow.discount_amount ELSE ow.discount_amount / er.rate END ) AS total_discount_eur FROM orphan_with_category ow LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = ow.discount_currency AND er.as_of = ow.placed_at::date GROUP BY ow.status_category ) SELECT t0.status_category AS status_category, COALESCE(t1.orphan_customer_count, 0) AS orphan_customer_count, COALESCE(t2.orphan_order_count, 0) AS orphan_order_count, ROUND(100.0 * COALESCE(t2.orphan_order_count, 0) / NULLIF(t3.total_orphan_orders, 0), 2) AS pct_of_all_orphan_orders, ROUND(t4.avg_days_sub_end_to_order, 1) AS avg_days_sub_end_to_order, ROUND(COALESCE(t5.total_discount_eur, 0), 2) AS total_discount_amount_eur FROM t0 LEFT JOIN t1 ON t1.status_category = t0.status_category LEFT JOIN t2 ON t2.status_category = t0.status_category CROSS JOIN t3 LEFT JOIN t4 ON t4.status_category = t0.status_category LEFT JOIN t5 ON t5.status_category = t0.status_category ORDER BY COALESCE(t2.orphan_order_count, 0) DESC, t0.status_category ASC
WITH 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, od.amount AS discount_amount, od.currency_code AS discount_currency FROM orders o JOIN order_discounts od ON od.order_id = o.id AND od.tenant_id = o.tenant_id JOIN discounts d ON d.id = od.discount_id AND d.tenant_id = o.tenant_id WHERE d.name = 'Subscription delivery (prepaid)' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND NOT EXISTS ( SELECT 1 FROM subscriptions s WHERE s.tenant_id = o.tenant_id AND s.user_id = o.user_id AND s.status IN ('trialing','active','past_due') AND s.started_at <= o.placed_at AND (s.ended_at IS NULL OR s.ended_at >= o.placed_at) )),latest_subscription AS ( SELECT s.tenant_id AS tenant_id, s.user_id AS user_id, s.status AS status, s.ended_at AS ended_at, s.started_at AS started_at, ROW_NUMBER() OVER (PARTITION BY s.tenant_id, s.user_id ORDER BY s.started_at DESC, s.id DESC) AS rn FROM subscriptions s),orphan_with_category AS ( SELECT oo.order_id AS order_id, oo.tenant_id AS tenant_id, oo.user_id AS user_id, oo.placed_at AS placed_at, oo.discount_amount AS discount_amount, oo.discount_currency AS discount_currency, COALESCE(ls.status, 'no_subscription_record') AS status_category, ls.ended_at AS sub_ended_at FROM orphan_orders oo LEFT JOIN latest_subscription ls ON ls.tenant_id = oo.tenant_id AND ls.user_id = oo.user_id AND ls.rn = 1),t0 AS ( SELECT DISTINCT status_category FROM orphan_with_category),t1 AS ( SELECT status_category, COUNT(DISTINCT tenant_id, user_id) AS orphan_customer_count FROM orphan_with_category SELECT status_category, COUNT(*) AS orphan_customer_count FROM ( SELECT DISTINCT status_category, tenant_id, user_id FROM orphan_with_category ) dc GROUP BY status_category),t2 AS ( SELECT status_category, COUNT(*) AS orphan_order_count FROM orphan_with_category GROUP BY status_category),t3 AS ( SELECT COUNT(*) AS total_orphan_orders FROM orphan_with_category),t4 AS ( SELECT status_category, AVG(COALESCE(sub_ended_at::date, DATE '2026-01-05') - placed_at::date) AS avg_days_sub_end_to_order FROM orphan_with_category WHERE status_category <> 'no_subscription_record' GROUP BY status_category),t5 AS ( SELECT ow.status_category, SUM( CASE WHEN ow.discount_currency = 'EUR' THEN ow.discount_amount ELSE ow.discount_amount / er.rate END ) AS total_discount_eur FROM orphan_with_category ow LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = ow.discount_currency AND er.as_of = ow.placed_at::date GROUP BY ow.status_category)SELECT t0.status_category AS status_category, COALESCE(t1.orphan_customer_count, 0) AS orphan_customer_count, COALESCE(t2.orphan_order_count, 0) AS orphan_order_count, ROUND(100.0 * COALESCE(t2.orphan_order_count, 0) / NULLIF(t3.total_orphan_orders, 0), 2) AS pct_of_all_orphan_orders, ROUND(t4.avg_days_sub_end_to_order, 1) AS avg_days_sub_end_to_order, ROUND(COALESCE(t5.total_discount_eur, 0), 2) AS total_discount_amount_eurFROM t0LEFT JOIN t1 ON t1.status_category = t0.status_categoryLEFT JOIN t2 ON t2.status_category = t0.status_categoryCROSS JOIN t3LEFT JOIN t4 ON t4.status_category = t0.status_categoryLEFT JOIN t5 ON t5.status_category = t0.status_categoryORDER BY COALESCE(t2.orphan_order_count, 0) DESC, t0.status_category ASC
“The query compiles cleanly with exactly the requested columns. Submitting.”
“Submitted. Summary of what the report delivers:
- Grain: one row per subscription-status category (trialing / active / past_due / canceled / paused / incomplete / no_subscription_record), derived from each orphan customer's single most recent subscription record (any status) by started_at.
- Orphan population: orders with a 'Subscription delivery (prepaid)' discount, placed in 2025, status ≠ cancelled, and no subscription in ('trialing','active','past_due') whose date range covers the order's placed_at — matching the prior report's definition.
- Columns: status category, orphan customer count, orphan order count, % of all orphan orders, avg days from subscription ended_at (or 2026-01-05 if still open) to the order's placed_at (null for no_subscription_record, since it doesn't apply), and total discount amount converted to EUR via same-day exchange_rates.
- Ordered by orphan order count descending, tie-broken by category name for determinism.
One judgment call noted in the submit note: since the exact "prior report" SQL wasn't available to me, I reconstructed the overlap/orphan logic directly from the definition given in this request.”
As Oliver Grant saw it, from the small dataset the agents worked on
status_category | orphan_customer_count | orphan_order_count | pct_of_all_orphan_orders | avg_days_sub_end_to_order | total_discount_amount_eur ----------------+-----------------------+--------------------+--------------------------+---------------------------+-------------------------- canceled | 238 | 1322 | 81.96 | 116.9 | 57167.69 active | 60 | 291 | 18.04 | 248.8 | 11746.39 (2 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 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, od.amount AS discount_amount, od.currency_code AS discount_currency FROM orders o JOIN order_discounts od ON od.order_id = o.id AND od.tenant_id = o.tenant_id JOIN discounts d ON d.id = od.discount_id AND d.tenant_id = o.tenant_id WHERE d.name = 'Subscription delivery (prepaid)' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND NOT EXISTS ( SELECT 1 FROM subscriptions s WHERE s.tenant_id = o.tenant_id AND s.user_id = o.user_id AND s.status IN ('trialing','active','past_due') AND s.started_at <= o.placed_at AND (s.ended_at IS NULL OR s.ended_at >= o.placed_at) )),latest_subscription AS ( SELECT s.tenant_id AS tenant_id, s.user_id AS user_id, s.status AS status, s.ended_at AS ended_at, s.started_at AS started_at, ROW_NUMBER() OVER (PARTITION BY s.tenant_id, s.user_id ORDER BY s.started_at DESC, s.id DESC) AS rn ROW_NUMBER() OVER (PARTITION BY s.tenant_id, s.user_id ORDER BY s.started_at DESC NULLS FIRST, s.id DESC NULLS FIRST) AS rn FROM subscriptions s),orphan_with_category AS ( SELECT oo.order_id AS order_id, oo.tenant_id AS tenant_id, oo.user_id AS user_id, oo.placed_at AS placed_at, oo.discount_amount AS discount_amount, oo.discount_currency AS discount_currency, COALESCE(ls.status, 'no_subscription_record') AS status_category, ls.ended_at AS sub_ended_at FROM orphan_orders oo LEFT JOIN latest_subscription ls ON ls.tenant_id = oo.tenant_id AND ls.user_id = oo.user_id AND ls.rn = 1),t0 AS ( SELECT DISTINCT status_category FROM orphan_with_category),t1 AS ( SELECT status_category, COUNT(*) AS orphan_customer_count FROM ( SELECT DISTINCT status_category, tenant_id, user_id FROM orphan_with_category ) dc GROUP BY status_category),t2 AS ( SELECT status_category, COUNT(*) AS orphan_order_count FROM orphan_with_category GROUP BY status_category),t3 AS ( SELECT COUNT(*) AS total_orphan_orders FROM orphan_with_category),t4 AS ( SELECT status_category, AVG(COALESCE(sub_ended_at::date, DATE '2026-01-05') - placed_at::date) AS avg_days_sub_end_to_order AVG(CAST(COALESCE(CAST(sub_ended_at AS date), DATE '2026-01-05') - CAST(placed_at AS date) AS NUMERIC(18,6))) AS avg_days_sub_end_to_order FROM orphan_with_category WHERE status_category <> 'no_subscription_record' GROUP BY status_category),t5 AS ( SELECT ow.status_category, SELECT ow.status_category AS status_category, SUM( CASE WHEN ow.discount_currency = 'EUR' THEN ow.discount_amount ELSE ow.discount_amount / er.rate ELSE CAST(ow.discount_amount / er.rate AS NUMERIC(18,6)) END ) AS total_discount_eur FROM orphan_with_category ow LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = ow.discount_currency AND er.as_of = ow.placed_at::date AND er.as_of = CAST(ow.placed_at AS date) GROUP BY ow.status_category)SELECT t0.status_category AS status_category, COALESCE(t1.orphan_customer_count, 0) AS orphan_customer_count, COALESCE(t2.orphan_order_count, 0) AS orphan_order_count, ROUND(100.0 * COALESCE(t2.orphan_order_count, 0) / NULLIF(t3.total_orphan_orders, 0), 2) AS pct_of_all_orphan_orders, ROUND(t4.avg_days_sub_end_to_order, 1) AS avg_days_sub_end_to_order, ROUND(COALESCE(t5.total_discount_eur, 0), 2) AS total_discount_amount_eur 100.0 * COALESCE(t2.orphan_order_count, 0) / NULLIF(t3.total_orphan_orders, 0) AS pct_of_all_orphan_orders, t4.avg_days_sub_end_to_order AS avg_days_sub_end_to_order, COALESCE(t5.total_discount_eur, 0) AS total_discount_amount_eurFROM t0LEFT JOIN t1 ON t1.status_category = t0.status_categoryLEFT JOIN t2 ON t2.status_category = t0.status_categoryCROSS JOIN t3LEFT JOIN t4 ON t4.status_category = t0.status_categoryLEFT JOIN t5 ON t5.status_category = t0.status_categoryORDER BY COALESCE(t2.orphan_order_count, 0) DESC, t0.status_category ASCORDER BY COALESCE(t2.orphan_order_count, 0) DESC NULLS FIRST, t0.status_category ASC NULLS LAST
WITH 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, od.amount AS discount_amount, od.currency_code AS discount_currency FROM orders o JOIN order_discounts od ON od.order_id = o.id AND od.tenant_id = o.tenant_id JOIN discounts d ON d.id = od.discount_id AND d.tenant_id = o.tenant_id WHERE d.name = 'Subscription delivery (prepaid)' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND NOT EXISTS ( SELECT 1 FROM subscriptions s WHERE s.tenant_id = o.tenant_id AND s.user_id = o.user_id AND s.status IN ('trialing','active','past_due') AND s.started_at <= o.placed_at AND (s.ended_at IS NULL OR s.ended_at >= o.placed_at) ) ), latest_subscription AS ( SELECT s.tenant_id AS tenant_id, s.user_id AS user_id, s.status AS status, s.ended_at AS ended_at, s.started_at AS started_at, ROW_NUMBER() OVER (PARTITION BY s.tenant_id, s.user_id ORDER BY s.started_at DESC NULLS FIRST, s.id DESC NULLS FIRST) AS rn FROM subscriptions s ), orphan_with_category AS ( SELECT oo.order_id AS order_id, oo.tenant_id AS tenant_id, oo.user_id AS user_id, oo.placed_at AS placed_at, oo.discount_amount AS discount_amount, oo.discount_currency AS discount_currency, COALESCE(ls.status, 'no_subscription_record') AS status_category, ls.ended_at AS sub_ended_at FROM orphan_orders oo LEFT JOIN latest_subscription ls ON ls.tenant_id = oo.tenant_id AND ls.user_id = oo.user_id AND ls.rn = 1 ), t0 AS ( SELECT DISTINCT status_category FROM orphan_with_category ), t1 AS ( SELECT status_category, COUNT(*) AS orphan_customer_count FROM ( SELECT DISTINCT status_category, tenant_id, user_id FROM orphan_with_category ) dc GROUP BY status_category ), t2 AS ( SELECT status_category, COUNT(*) AS orphan_order_count FROM orphan_with_category GROUP BY status_category ), t3 AS ( SELECT COUNT(*) AS total_orphan_orders FROM orphan_with_category ), t4 AS ( SELECT status_category, AVG(CAST(COALESCE(CAST(sub_ended_at AS date), DATE '2026-01-05') - CAST(placed_at AS date) AS NUMERIC(18,6))) AS avg_days_sub_end_to_order FROM orphan_with_category WHERE status_category <> 'no_subscription_record' GROUP BY status_category ), t5 AS ( SELECT ow.status_category AS status_category, SUM( CASE WHEN ow.discount_currency = 'EUR' THEN ow.discount_amount ELSE CAST(ow.discount_amount / er.rate AS NUMERIC(18,6)) END ) AS total_discount_eur FROM orphan_with_category ow LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = ow.discount_currency AND er.as_of = CAST(ow.placed_at AS date) GROUP BY ow.status_category ) SELECT t0.status_category AS status_category, COALESCE(t1.orphan_customer_count, 0) AS orphan_customer_count, COALESCE(t2.orphan_order_count, 0) AS orphan_order_count, 100.0 * COALESCE(t2.orphan_order_count, 0) / NULLIF(t3.total_orphan_orders, 0) AS pct_of_all_orphan_orders, t4.avg_days_sub_end_to_order AS avg_days_sub_end_to_order, COALESCE(t5.total_discount_eur, 0) AS total_discount_amount_eur FROM t0 LEFT JOIN t1 ON t1.status_category = t0.status_category LEFT JOIN t2 ON t2.status_category = t0.status_category CROSS JOIN t3 LEFT JOIN t4 ON t4.status_category = t0.status_category LEFT JOIN t5 ON t5.status_category = t0.status_category ORDER BY COALESCE(t2.orphan_order_count, 0) DESC NULLS FIRST, t0.status_category 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 Oliver Grant (analyst for the Head of Pricing and Promotions):
Title: Subscription status distribution behind orphan comped-delivery orders
Question: What subscription status do the 'orphan' Subscription-delivery-discount customers (no active subscription overlap found) actually have on record, since neither 'lapsed within 30 days' nor 'no subscription at all' explained them?
Grain: one row per subscription-status category (trialing, active, past_due, canceled, paused, incomplete, or 'no_subscription_record')
Columns:
- status category (customer's most recent subscription status for that tenant, by started_at, or 'no_subscription_record' if none exists)
- orphan customer count
- orphan order count (2025 'Subscription delivery (prepaid)' orders with no active-status date overlap, as defined in the prior report)
- % of all orphan orders
- avg days between subscription's ended_at (or current date if still open) and the orphan order's placed_at, where applicable
- total discount amount on these orders (EUR)
Details: Source: same orphan-order population as the prior report (order_discounts + discounts name='Subscription delivery (prepaid)' + orders placed_at in 2025, status != 'cancelled', with no overlapping subscription of status in ('trialing','active','past_due') per the prior definition). For each orphan order's customer (user_id, tenant_id), find their single most recent subscription record by started_at and report its status (not restricted to active ones this time) as the category, or 'no_subscription_record' if the customer has zero subscription rows for that tenant. Convert discount amounts to EUR via exchange_rates (base_code='EUR', quote_code=currency_code, as_of=placed_at). Order by orphan order count descending. Print all categories (expect 5-8 rows).
Check your query with the warehouse's dry_run tool, then hand it in with submit.dry_runWITH 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, od.amount AS discount_amount, od.currency_code AS discount_currency FROM orders o JOIN order_discounts od ON od.order_id = o.id AND od.tenant_id = o.tenant_id JOIN discounts d ON d.id = od.discount_id AND d.tenant_id = o.tenant_id WHERE d.name = 'Subscription delivery (prepaid)' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND NOT EXISTS ( SELECT 1 FROM subscriptions s WHERE s.tenant_id = o.tenant_id AND s.user_id = o.user_id AND s.status IN ('trialing','active','past_due') AND s.started_at <= o.placed_at AND (s.ended_at IS NULL OR s.ended_at >= o.placed_at) ) ), latest_subscription AS ( SELECT s.tenant_id AS tenant_id, s.user_id AS user_id, s.status AS status, s.ended_at AS ended_at, s.started_at AS started_at, ROW_NUMBER() OVER (PARTITION BY s.tenant_id, s.user_id ORDER BY s.started_at DESC, s.id DESC) AS rn FROM subscriptions s ), orphan_with_category AS ( SELECT oo.order_id AS order_id, oo.tenant_id AS tenant_id, oo.user_id AS user_id, oo.placed_at AS placed_at, oo.discount_amount AS discount_amount, oo.discount_currency AS discount_currency, COALESCE(ls.status, 'no_subscription_record') AS status_category, ls.ended_at AS sub_ended_at FROM orphan_orders oo LEFT JOIN latest_subscription ls ON ls.tenant_id = oo.tenant_id AND ls.user_id = oo.user_id AND ls.rn = 1 ), t0 AS ( SELECT DISTINCT status_category FROM orphan_with_category ), t1 AS ( SELECT status_category, COUNT(DISTINCT tenant_id, user_id) AS orphan_customer_count FROM orphan_with_category GROUP BY status_category ), t2 AS ( SELECT status_category, COUNT(*) AS orphan_order_count FROM orphan_with_category GROUP BY status_category ), t3 AS ( SELECT COUNT(*) AS total_orphan_orders FROM orphan_with_category ), t4 AS ( SELECT status_category, AVG(COALESCE(sub_ended_at::date, DATE '2026-01-05') - placed_at::date) AS avg_days_sub_end_to_order FROM orphan_with_category WHERE status_category <> 'no_subscription_record' GROUP BY status_category ), t5 AS ( SELECT ow.status_category, SUM( CASE WHEN ow.discount_currency = 'EUR' THEN ow.discount_amount ELSE ow.discount_amount / er.rate END ) AS total_discount_eur FROM orphan_with_category ow LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = ow.discount_currency AND er.as_of = ow.placed_at::date GROUP BY ow.status_category ) SELECT t0.status_category AS status_category, COALESCE(t1.orphan_customer_count, 0) AS orphan_customer_count, COALESCE(t2.orphan_order_count, 0) AS orphan_order_count, ROUND(100.0 * COALESCE(t2.orphan_order_count, 0) / NULLIF(t3.total_orphan_orders, 0), 2) AS pct_of_all_orphan_orders, ROUND(t4.avg_days_sub_end_to_order, 1) AS avg_days_sub_end_to_order, ROUND(COALESCE(t5.total_discount_eur, 0), 2) AS total_discount_amount_eur FROM t0 LEFT JOIN t1 ON t1.status_category = t0.status_category LEFT JOIN t2 ON t2.status_category = t0.status_category CROSS JOIN t3 LEFT JOIN t4 ON t4.status_category = t0.status_category LEFT JOIN t5 ON t5.status_category = t0.status_category ORDER BY COALESCE(t2.orphan_order_count, 0) DESC, t0.status_category ASC
STATUS: REJECTED - does not compile: Binder Error: No function matches the given name and argument types 'count(BIGINT, BIGINT)'. You might need to add explicit type casts.
dry_runWITH 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, od.amount AS discount_amount, od.currency_code AS discount_currency FROM orders o JOIN order_discounts od ON od.order_id = o.id AND od.tenant_id = o.tenant_id JOIN discounts d ON d.id = od.discount_id AND d.tenant_id = o.tenant_id WHERE d.name = 'Subscription delivery (prepaid)' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND NOT EXISTS ( SELECT 1 FROM subscriptions s WHERE s.tenant_id = o.tenant_id AND s.user_id = o.user_id AND s.status IN ('trialing','active','past_due') AND s.started_at <= o.placed_at AND (s.ended_at IS NULL OR s.ended_at >= o.placed_at) ) ), latest_subscription AS ( SELECT s.tenant_id AS tenant_id, s.user_id AS user_id, s.status AS status, s.ended_at AS ended_at, s.started_at AS started_at, ROW_NUMBER() OVER (PARTITION BY s.tenant_id, s.user_id ORDER BY s.started_at DESC, s.id DESC) AS rn FROM subscriptions s ), orphan_with_category AS ( SELECT oo.order_id AS order_id, oo.tenant_id AS tenant_id, oo.user_id AS user_id, oo.placed_at AS placed_at, oo.discount_amount AS discount_amount, oo.discount_currency AS discount_currency, COALESCE(ls.status, 'no_subscription_record') AS status_category, ls.ended_at AS sub_ended_at FROM orphan_orders oo LEFT JOIN latest_subscription ls ON ls.tenant_id = oo.tenant_id AND ls.user_id = oo.user_id AND ls.rn = 1 ), t0 AS ( SELECT DISTINCT status_category FROM orphan_with_category ), t1 AS ( SELECT status_category, COUNT(*) AS orphan_customer_count FROM ( SELECT DISTINCT status_category, tenant_id, user_id FROM orphan_with_category ) dc GROUP BY status_category ), t2 AS ( SELECT status_category, COUNT(*) AS orphan_order_count FROM orphan_with_category GROUP BY status_category ), t3 AS ( SELECT COUNT(*) AS total_orphan_orders FROM orphan_with_category ), t4 AS ( SELECT status_category, AVG(COALESCE(sub_ended_at::date, DATE '2026-01-05') - placed_at::date) AS avg_days_sub_end_to_order FROM orphan_with_category WHERE status_category <> 'no_subscription_record' GROUP BY status_category ), t5 AS ( SELECT ow.status_category, SUM( CASE WHEN ow.discount_currency = 'EUR' THEN ow.discount_amount ELSE ow.discount_amount / er.rate END ) AS total_discount_eur FROM orphan_with_category ow LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = ow.discount_currency AND er.as_of = ow.placed_at::date GROUP BY ow.status_category ) SELECT t0.status_category AS status_category, COALESCE(t1.orphan_customer_count, 0) AS orphan_customer_count, COALESCE(t2.orphan_order_count, 0) AS orphan_order_count, ROUND(100.0 * COALESCE(t2.orphan_order_count, 0) / NULLIF(t3.total_orphan_orders, 0), 2) AS pct_of_all_orphan_orders, ROUND(t4.avg_days_sub_end_to_order, 1) AS avg_days_sub_end_to_order, ROUND(COALESCE(t5.total_discount_eur, 0), 2) AS total_discount_amount_eur FROM t0 LEFT JOIN t1 ON t1.status_category = t0.status_category LEFT JOIN t2 ON t2.status_category = t0.status_category CROSS JOIN t3 LEFT JOIN t4 ON t4.status_category = t0.status_category LEFT JOIN t5 ON t5.status_category = t0.status_category ORDER BY COALESCE(t2.orphan_order_count, 0) DESC, t0.status_category ASC
STATUS: COMPILES (dry run on the empty schema, no data read) COLUMNS: status_category, orphan_customer_count, orphan_order_count, pct_of_all_orphan_orders, avg_days_sub_end_to_order, total_discount_amount_eur
submitWITH 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, od.amount AS discount_amount, od.currency_code AS discount_currency FROM orders o JOIN order_discounts od ON od.order_id = o.id AND od.tenant_id = o.tenant_id JOIN discounts d ON d.id = od.discount_id AND d.tenant_id = o.tenant_id WHERE d.name = 'Subscription delivery (prepaid)' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND NOT EXISTS ( SELECT 1 FROM subscriptions s WHERE s.tenant_id = o.tenant_id AND s.user_id = o.user_id AND s.status IN ('trialing','active','past_due') AND s.started_at <= o.placed_at AND (s.ended_at IS NULL OR s.ended_at >= o.placed_at) ) ), latest_subscription AS ( SELECT s.tenant_id AS tenant_id, s.user_id AS user_id, s.status AS status, s.ended_at AS ended_at, s.started_at AS started_at, ROW_NUMBER() OVER (PARTITION BY s.tenant_id, s.user_id ORDER BY s.started_at DESC, s.id DESC) AS rn FROM subscriptions s ), orphan_with_category AS ( SELECT oo.order_id AS order_id, oo.tenant_id AS tenant_id, oo.user_id AS user_id, oo.placed_at AS placed_at, oo.discount_amount AS discount_amount, oo.discount_currency AS discount_currency, COALESCE(ls.status, 'no_subscription_record') AS status_category, ls.ended_at AS sub_ended_at FROM orphan_orders oo LEFT JOIN latest_subscription ls ON ls.tenant_id = oo.tenant_id AND ls.user_id = oo.user_id AND ls.rn = 1 ), t0 AS ( SELECT DISTINCT status_category FROM orphan_with_category ), t1 AS ( SELECT status_category, COUNT(*) AS orphan_customer_count FROM ( SELECT DISTINCT status_category, tenant_id, user_id FROM orphan_with_category ) dc GROUP BY status_category ), t2 AS ( SELECT status_category, COUNT(*) AS orphan_order_count FROM orphan_with_category GROUP BY status_category ), t3 AS ( SELECT COUNT(*) AS total_orphan_orders FROM orphan_with_category ), t4 AS ( SELECT status_category, AVG(COALESCE(sub_ended_at::date, DATE '2026-01-05') - placed_at::date) AS avg_days_sub_end_to_order FROM orphan_with_category WHERE status_category <> 'no_subscription_record' GROUP BY status_category ), t5 AS ( SELECT ow.status_category, SUM( CASE WHEN ow.discount_currency = 'EUR' THEN ow.discount_amount ELSE ow.discount_amount / er.rate END ) AS total_discount_eur FROM orphan_with_category ow LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = ow.discount_currency AND er.as_of = ow.placed_at::date GROUP BY ow.status_category ) SELECT t0.status_category AS status_category, COALESCE(t1.orphan_customer_count, 0) AS orphan_customer_count, COALESCE(t2.orphan_order_count, 0) AS orphan_order_count, ROUND(100.0 * COALESCE(t2.orphan_order_count, 0) / NULLIF(t3.total_orphan_orders, 0), 2) AS pct_of_all_orphan_orders, ROUND(t4.avg_days_sub_end_to_order, 1) AS avg_days_sub_end_to_order, ROUND(COALESCE(t5.total_discount_eur, 0), 2) AS total_discount_amount_eur FROM t0 LEFT JOIN t1 ON t1.status_category = t0.status_category LEFT JOIN t2 ON t2.status_category = t0.status_category CROSS JOIN t3 LEFT JOIN t4 ON t4.status_category = t0.status_category LEFT JOIN t5 ON t5.status_category = t0.status_category ORDER BY COALESCE(t2.orphan_order_count, 0) DESC, t0.status_category ASC
Submitted to tonight's batch. Your work on this request is done.