Queries · q156 · Head of Pricing and Promotions · week 1, night 3
“Do orders placed under our top discounts return or get refunded at a higher rate than normal, meaning the real cost is higher than the discount line alone suggests?”
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 3, request 3 of 3
Night 2 sharpened three things:
Tonight's 3 reports: (A) drill into the subscription-delivery orders with no overlapping active subscription — concentration and whether it traces to lapsed subs or true giveaways; (B) a corrected incrementality read on true marketing discounts using pre-discount subtotal; (C) the still-untouched margin question — do discounted orders return/refund at a higher rate than baseline, eating further into margin. This covers the open pieces of Marco's and Tobias's questions before Thursday's sync.
The engineer · 2 dry runs on the empty schema, then the submission
WITH discount_eur AS ( SELECT od.discount_id, SUM(od.amount / er.rate) AS discount_amount_eur FROM order_discounts od JOIN orders o ON o.id = od.order_id JOIN discounts d ON d.id = od.discount_id JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = od.currency_code AND er.as_of = CAST(o.placed_at AS date) WHERE o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND d.name <> 'Subscription delivery' GROUP BY od.discount_id ), top15 AS ( SELECT discount_id, discount_amount_eur, ROW_NUMBER() OVER (ORDER BY discount_amount_eur DESC, discount_id ASC) AS rn FROM discount_eur ), discounted_orders AS ( SELECT DISTINCT od.discount_id, o.id AS order_id, o.tenant_id FROM order_discounts od JOIN orders o ON o.id = od.order_id WHERE od.discount_id IN (SELECT discount_id FROM top15 WHERE rn <= 15) AND o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' ), order_rma_flag AS ( SELECT do.discount_id, do.order_id, EXISTS (SELECT 1 FROM order_returns orr WHERE orr.order_id = do.order_id) AS has_rma FROM discounted_orders do ), discount_summary AS ( SELECT do.discount_id, COUNT(DISTINCT do.order_id) AS order_count, COUNT(DISTINCT do.order_id) FILTER (WHERE f.has_rma) AS orders_with_rma FROM discounted_orders do JOIN order_rma_flag f ON f.discount_id = do.discount_id AND f.order_id = do.order_id GROUP BY do.discount_id ), refunds AS ( SELECT do.discount_id, SUM(orr.refund_total / er.rate) AS refund_eur FROM discounted_orders do JOIN order_returns orr ON orr.order_id = do.order_id AND orr.status = 'refunded' JOIN orders o ON o.id = do.order_id JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = orr.currency_code AND er.as_of = CAST(COALESCE(orr.refunded_at, o.placed_at) AS date) GROUP BY do.discount_id ), baseline AS ( SELECT o.tenant_id, COUNT(DISTINCT o.id) AS baseline_order_count, COUNT(DISTINCT o.id) FILTER ( WHERE EXISTS (SELECT 1 FROM order_returns orr2 WHERE orr2.order_id = o.id) ) AS baseline_rma_count FROM orders o WHERE o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND NOT EXISTS (SELECT 1 FROM order_discounts od2 WHERE od2.order_id = o.id) GROUP BY o.tenant_id ) SELECT d.name AS discount_name, COALESCE(cc.code, 'automatic') AS coupon_code, ds.order_count, ds.orders_with_rma, ROUND(100.0 * ds.orders_with_rma / NULLIF(ds.order_count, 0), 2) AS return_rate_pct, ROUND(COALESCE(r.refund_eur, 0), 2) AS total_refunded_eur, ROUND(100.0 * COALESCE(r.refund_eur, 0) / NULLIF(t.discount_amount_eur, 0), 2) AS refund_pct_of_discount_cost, ROUND(100.0 * b.baseline_rma_count / NULLIF(b.baseline_order_count, 0), 2) AS baseline_return_rate_pct, ROUND( 100.0 * ds.orders_with_rma / NULLIF(ds.order_count, 0) - 100.0 * b.baseline_rma_count / NULLIF(b.baseline_order_count, 0), 2 ) AS return_rate_lift_pp FROM top15 t JOIN discounts d ON d.id = t.discount_id JOIN discount_summary ds ON ds.discount_id = t.discount_id LEFT JOIN refunds r ON r.discount_id = t.discount_id LEFT JOIN baseline b ON b.tenant_id = d.tenant_id LEFT JOIN LATERAL ( SELECT c.code FROM coupons c WHERE c.discount_id = d.id ORDER BY c.id LIMIT 1 ) cc ON true WHERE t.rn <= 15 ORDER BY return_rate_lift_pp DESC NULLS LAST, discount_name ASC
WITH discount_eur AS ( SELECT od.discount_id, SUM(od.amount / er.rate) AS discount_amount_eur FROM order_discounts od JOIN orders o ON o.id = od.order_id JOIN discounts d ON d.id = od.discount_id JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = od.currency_code AND er.as_of = CAST(o.placed_at AS date) WHERE o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND d.name <> 'Subscription delivery' GROUP BY od.discount_id),top15 AS ( SELECT discount_id, discount_amount_eur, ROW_NUMBER() OVER (ORDER BY discount_amount_eur DESC, discount_id ASC) AS rn FROM discount_eur),discounted_orders AS ( SELECT DISTINCT od.discount_id, o.id AS order_id, o.tenant_id FROM order_discounts od JOIN orders o ON o.id = od.order_id WHERE od.discount_id IN (SELECT discount_id FROM top15 WHERE rn <= 15) AND o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01'),order_rma_flag AS ( SELECT do.discount_id, do.order_id, EXISTS (SELECT 1 FROM order_returns orr WHERE orr.order_id = do.order_id) AS has_rma FROM discounted_orders do SELECT dco.discount_id, dco.order_id, EXISTS (SELECT 1 FROM order_returns orr WHERE orr.order_id = dco.order_id) AS has_rma FROM discounted_orders dco),discount_summary AS ( SELECT do.discount_id, COUNT(DISTINCT do.order_id) AS order_count, COUNT(DISTINCT do.order_id) FILTER (WHERE f.has_rma) AS orders_with_rma FROM discounted_orders do JOIN order_rma_flag f ON f.discount_id = do.discount_id AND f.order_id = do.order_id GROUP BY do.discount_id SELECT dco.discount_id, COUNT(DISTINCT dco.order_id) AS order_count, COUNT(DISTINCT dco.order_id) FILTER (WHERE f.has_rma) AS orders_with_rma FROM discounted_orders dco JOIN order_rma_flag f ON f.discount_id = dco.discount_id AND f.order_id = dco.order_id GROUP BY dco.discount_id),refunds AS ( SELECT do.discount_id, SELECT dco.discount_id, SUM(orr.refund_total / er.rate) AS refund_eur FROM discounted_orders do JOIN order_returns orr ON orr.order_id = do.order_id AND orr.status = 'refunded' JOIN orders o ON o.id = do.order_id FROM discounted_orders dco JOIN order_returns orr ON orr.order_id = dco.order_id AND orr.status = 'refunded' JOIN orders o ON o.id = dco.order_id JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = orr.currency_code AND er.as_of = CAST(COALESCE(orr.refunded_at, o.placed_at) AS date) GROUP BY do.discount_id GROUP BY dco.discount_id),baseline AS ( SELECT o.tenant_id, COUNT(DISTINCT o.id) AS baseline_order_count, COUNT(DISTINCT o.id) FILTER ( WHERE EXISTS (SELECT 1 FROM order_returns orr2 WHERE orr2.order_id = o.id) ) AS baseline_rma_count FROM orders o WHERE o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND NOT EXISTS (SELECT 1 FROM order_discounts od2 WHERE od2.order_id = o.id) GROUP BY o.tenant_id)SELECT d.name AS discount_name, COALESCE(cc.code, 'automatic') AS coupon_code, ds.order_count, ds.orders_with_rma, ROUND(100.0 * ds.orders_with_rma / NULLIF(ds.order_count, 0), 2) AS return_rate_pct, ROUND(COALESCE(r.refund_eur, 0), 2) AS total_refunded_eur, ROUND(100.0 * COALESCE(r.refund_eur, 0) / NULLIF(t.discount_amount_eur, 0), 2) AS refund_pct_of_discount_cost, ROUND(100.0 * b.baseline_rma_count / NULLIF(b.baseline_order_count, 0), 2) AS baseline_return_rate_pct, ROUND( 100.0 * ds.orders_with_rma / NULLIF(ds.order_count, 0) - 100.0 * b.baseline_rma_count / NULLIF(b.baseline_order_count, 0), 2 ) AS return_rate_lift_ppFROM top15 tJOIN discounts d ON d.id = t.discount_idJOIN discount_summary ds ON ds.discount_id = t.discount_idLEFT JOIN refunds r ON r.discount_id = t.discount_idLEFT JOIN baseline b ON b.tenant_id = d.tenant_idLEFT JOIN LATERAL ( SELECT c.code FROM coupons c WHERE c.discount_id = d.id ORDER BY c.id LIMIT 1) cc ON trueWHERE t.rn <= 15ORDER BY return_rate_lift_pp DESC NULLS LAST, discount_name ASC
“Query compiles and returns the expected columns. Submitting.”
“Submitted. Summary of the approach:
- Top-15 set: recomputed by summing order_discounts.amount (converted to EUR via same-day exchange_rates) for 2025, non-cancelled orders, grouped by discount_id, excluding discounts named "Subscription delivery", then taking the top 15 by that EUR total.
- Discounted side: distinct 2025 non-cancelled orders carrying each top-15 discount; return rate = orders with any order_returns row ÷ order count; refunded EUR = order_returns.refund_total (status='refunded') converted via the exchange rate as of refunded_at (falling back to placed_at).
- Baseline: same tenant's other 2025 non-cancelled orders with no order_discounts row at all, same return-rate calculation.
- Lift: discount return rate minus baseline return rate, in percentage points, driving the sort order.
Flagged in the submit note that the top-15 ranking was recomputed here rather than reused verbatim from a prior night's query, since I didn't have that exact discount_id list on hand.”
As Oliver Grant saw it, from the small dataset the agents worked on
discount_name | coupon_code | order_count | orders_with_rma | return_rate_pct | total_refunded_eur | refund_pct_of_discount_cost | baseline_return_rate_pct | return_rate_lift_pp --------------------------------+-------------+-------------+-----------------+-----------------+--------------------+-----------------------------+--------------------------+-------------------- Welcome 10% off | WELCOME10 | 4 | 1 | 25.0 | 157.79 | 4.16 | 3.57 | 21.43 January Sale 2025 | automatic | 124 | 53 | 42.74 | 4490.24 | 50.34 | 33.21 | 9.53 Volume tier 10+ | automatic | 38 | 4 | 10.53 | 1144.28 | 14.79 | 3.57 | 6.95 Volume tier 10+ | automatic | 102 | 10 | 9.8 | 3442.67 | 35.7 | 3.13 | 6.68 Volume tier 10+ | automatic | 104 | 8 | 7.69 | 18050.36 | 161.71 | 1.89 | 5.81 Summer Sale 2025 | automatic | 146 | 58 | 39.73 | 4415.98 | 53.47 | 37.31 | 2.42 Welcome 10% off | WELCOME10 | 260 | 103 | 39.62 | 12243.62 | 188.81 | 37.31 | 2.31 Subscription delivery (prepaid) | automatic | 43 | 0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 Subscription delivery (prepaid) | automatic | 650 | 0 | 0.0 | 0.0 | 0.0 | 1.05 | -1.05 Subscription delivery (prepaid) | automatic | 326 | 0 | 0.0 | 0.0 | 0.0 | 1.05 | -1.05 Subscription delivery (prepaid) | automatic | 354 | 0 | 0.0 | 0.0 | 0.0 | 2.97 | -2.97 Volume tier 50+ | automatic | 15 | 0 | 0.0 | 0.0 | 0.0 | 3.13 | -3.13 Welcome 10% off | WELCOME10 | 7 | 0 | 0.0 | 0.0 | 0.0 | 3.13 | -3.13 Summer Sale 2025 | automatic | 24 | 4 | 16.67 | 404.73 | 11.34 | 21.52 | -4.85 Subscription delivery (prepaid) | automatic | 106 | 0 | 0.0 | 0.0 | 0.0 | 5.05 | -5.05 (15 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 discount_eur AS ( SELECT od.discount_id, SUM(od.amount / er.rate) AS discount_amount_eur SELECT od.discount_id AS discount_id, SUM(CAST(od.amount / er.rate AS NUMERIC(18,6))) AS discount_amount_eur FROM order_discounts od JOIN orders o ON o.id = od.order_id JOIN discounts d ON d.id = od.discount_id JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = od.currency_code AND er.as_of = CAST(o.placed_at AS date) WHERE o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND d.name <> 'Subscription delivery' GROUP BY od.discount_id),top15 AS ( SELECT discount_id, discount_amount_eur, ROW_NUMBER() OVER (ORDER BY discount_amount_eur DESC, discount_id ASC) AS rn ROW_NUMBER() OVER (ORDER BY discount_amount_eur DESC NULLS FIRST, discount_id ASC NULLS LAST) AS rn FROM discount_eur),discounted_orders AS ( SELECT DISTINCT od.discount_id, o.id AS order_id, o.tenant_id SELECT DISTINCT od.discount_id AS discount_id, o.id AS order_id, o.tenant_id AS tenant_id FROM order_discounts od JOIN orders o ON o.id = od.order_id WHERE od.discount_id IN (SELECT discount_id FROM top15 WHERE rn <= 15) AND o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01'),order_rma_flag AS ( SELECT dco.discount_id, dco.order_id, SELECT dco.discount_id AS discount_id, dco.order_id AS order_id, EXISTS (SELECT 1 FROM order_returns orr WHERE orr.order_id = dco.order_id) AS has_rma FROM discounted_orders dco),discount_summary AS ( SELECT dco.discount_id, SELECT dco.discount_id AS discount_id, COUNT(DISTINCT dco.order_id) AS order_count, COUNT(DISTINCT dco.order_id) FILTER (WHERE f.has_rma) AS orders_with_rma FROM discounted_orders dco JOIN order_rma_flag f ON f.discount_id = dco.discount_id AND f.order_id = dco.order_id GROUP BY dco.discount_id),refunds AS ( SELECT dco.discount_id, SUM(orr.refund_total / er.rate) AS refund_eur SELECT dco.discount_id AS discount_id, SUM(CAST(orr.refund_total / er.rate AS NUMERIC(18,6))) AS refund_eur FROM discounted_orders dco JOIN order_returns orr ON orr.order_id = dco.order_id AND orr.status = 'refunded' JOIN orders o ON o.id = dco.order_id JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = orr.currency_code AND er.as_of = CAST(COALESCE(orr.refunded_at, o.placed_at) AS date) GROUP BY dco.discount_id),baseline AS ( SELECT o.tenant_id, SELECT o.tenant_id AS tenant_id, COUNT(DISTINCT o.id) AS baseline_order_count, COUNT(DISTINCT o.id) FILTER ( WHERE EXISTS (SELECT 1 FROM order_returns orr2 WHERE orr2.order_id = o.id) ) AS baseline_rma_count FROM orders o WHERE o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND NOT EXISTS (SELECT 1 FROM order_discounts od2 WHERE od2.order_id = o.id) GROUP BY o.tenant_id)SELECT d.name AS discount_name, COALESCE(cc.code, 'automatic') AS coupon_code, ds.order_count, ds.orders_with_rma, ROUND(100.0 * ds.orders_with_rma / NULLIF(ds.order_count, 0), 2) AS return_rate_pct, ROUND(COALESCE(r.refund_eur, 0), 2) AS total_refunded_eur, ROUND(100.0 * COALESCE(r.refund_eur, 0) / NULLIF(t.discount_amount_eur, 0), 2) AS refund_pct_of_discount_cost, ROUND(100.0 * b.baseline_rma_count / NULLIF(b.baseline_order_count, 0), 2) AS baseline_return_rate_pct, ROUND( 100.0 * ds.orders_with_rma / NULLIF(ds.order_count, 0) - 100.0 * b.baseline_rma_count / NULLIF(b.baseline_order_count, 0), 2 ) AS return_rate_lift_pp ds.order_count AS order_count, ds.orders_with_rma AS orders_with_rma, 100.0 * ds.orders_with_rma / NULLIF(ds.order_count, 0) AS return_rate_pct, COALESCE(r.refund_eur, 0) AS total_refunded_eur, 100.0 * COALESCE(r.refund_eur, 0) / NULLIF(t.discount_amount_eur, 0) AS refund_pct_of_discount_cost, 100.0 * b.baseline_rma_count / NULLIF(b.baseline_order_count, 0) AS baseline_return_rate_pct, 100.0 * ds.orders_with_rma / NULLIF(ds.order_count, 0) - 100.0 * b.baseline_rma_count / NULLIF(b.baseline_order_count, 0) AS return_rate_lift_ppFROM top15 tJOIN discounts d ON d.id = t.discount_idJOIN discount_summary ds ON ds.discount_id = t.discount_idLEFT JOIN refunds r ON r.discount_id = t.discount_idLEFT JOIN baseline b ON b.tenant_id = d.tenant_idLEFT JOIN LATERAL ( SELECT c.code FROM coupons c WHERE c.discount_id = d.id ORDER BY c.id LIMIT 1 SELECT c.code AS code FROM coupons c WHERE c.discount_id = d.id ORDER BY c.id NULLS LAST {{fetch_first(1)}}) cc ON trueWHERE t.rn <= 15ORDER BY return_rate_lift_pp DESC NULLS LAST, discount_name ASCORDER BY return_rate_lift_pp DESC NULLS LAST, discount_name ASC NULLS LAST, t.discount_id ASC NULLS LAST
WITH discount_eur AS ( SELECT od.discount_id AS discount_id, SUM(CAST(od.amount / er.rate AS NUMERIC(18,6))) AS discount_amount_eur FROM order_discounts od JOIN orders o ON o.id = od.order_id JOIN discounts d ON d.id = od.discount_id JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = od.currency_code AND er.as_of = CAST(o.placed_at AS date) WHERE o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND d.name <> 'Subscription delivery' GROUP BY od.discount_id ), top15 AS ( SELECT discount_id, discount_amount_eur, ROW_NUMBER() OVER (ORDER BY discount_amount_eur DESC NULLS FIRST, discount_id ASC NULLS LAST) AS rn FROM discount_eur ), discounted_orders AS ( SELECT DISTINCT od.discount_id AS discount_id, o.id AS order_id, o.tenant_id AS tenant_id FROM order_discounts od JOIN orders o ON o.id = od.order_id WHERE od.discount_id IN (SELECT discount_id FROM top15 WHERE rn <= 15) AND o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' ), order_rma_flag AS ( SELECT dco.discount_id AS discount_id, dco.order_id AS order_id, EXISTS (SELECT 1 FROM order_returns orr WHERE orr.order_id = dco.order_id) AS has_rma FROM discounted_orders dco ), discount_summary AS ( SELECT dco.discount_id AS discount_id, COUNT(DISTINCT dco.order_id) AS order_count, COUNT(DISTINCT dco.order_id) FILTER (WHERE f.has_rma) AS orders_with_rma FROM discounted_orders dco JOIN order_rma_flag f ON f.discount_id = dco.discount_id AND f.order_id = dco.order_id GROUP BY dco.discount_id ), refunds AS ( SELECT dco.discount_id AS discount_id, SUM(CAST(orr.refund_total / er.rate AS NUMERIC(18,6))) AS refund_eur FROM discounted_orders dco JOIN order_returns orr ON orr.order_id = dco.order_id AND orr.status = 'refunded' JOIN orders o ON o.id = dco.order_id JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = orr.currency_code AND er.as_of = CAST(COALESCE(orr.refunded_at, o.placed_at) AS date) GROUP BY dco.discount_id ), baseline AS ( SELECT o.tenant_id AS tenant_id, COUNT(DISTINCT o.id) AS baseline_order_count, COUNT(DISTINCT o.id) FILTER ( WHERE EXISTS (SELECT 1 FROM order_returns orr2 WHERE orr2.order_id = o.id) ) AS baseline_rma_count FROM orders o WHERE o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND NOT EXISTS (SELECT 1 FROM order_discounts od2 WHERE od2.order_id = o.id) GROUP BY o.tenant_id ) SELECT d.name AS discount_name, COALESCE(cc.code, 'automatic') AS coupon_code, ds.order_count AS order_count, ds.orders_with_rma AS orders_with_rma, 100.0 * ds.orders_with_rma / NULLIF(ds.order_count, 0) AS return_rate_pct, COALESCE(r.refund_eur, 0) AS total_refunded_eur, 100.0 * COALESCE(r.refund_eur, 0) / NULLIF(t.discount_amount_eur, 0) AS refund_pct_of_discount_cost, 100.0 * b.baseline_rma_count / NULLIF(b.baseline_order_count, 0) AS baseline_return_rate_pct, 100.0 * ds.orders_with_rma / NULLIF(ds.order_count, 0) - 100.0 * b.baseline_rma_count / NULLIF(b.baseline_order_count, 0) AS return_rate_lift_pp FROM top15 t JOIN discounts d ON d.id = t.discount_id JOIN discount_summary ds ON ds.discount_id = t.discount_id LEFT JOIN refunds r ON r.discount_id = t.discount_id LEFT JOIN baseline b ON b.tenant_id = d.tenant_id LEFT JOIN LATERAL ( SELECT c.code AS code FROM coupons c WHERE c.discount_id = d.id ORDER BY c.id NULLS LAST FETCH FIRST 1 ROWS ONLY ) cc ON true WHERE t.rn <= 15 ORDER BY return_rate_lift_pp DESC NULLS LAST, discount_name ASC NULLS LAST, t.discount_id ASC NULLS LAST
WITH discount_eur AS ( SELECT od.discount_id AS discount_id, SUM(CAST(od.amount / er.rate AS NUMERIC(18,6))) AS discount_amount_eur FROM order_discounts od JOIN orders o ON o.id = od.order_id JOIN discounts d ON d.id = od.discount_id JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = od.currency_code AND er.as_of = CAST(o.placed_at AS date) WHERE o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND d.name <> 'Subscription delivery' GROUP BY od.discount_id ), top15 AS ( SELECT discount_id, discount_amount_eur, ROW_NUMBER() OVER (ORDER BY discount_amount_eur DESC NULLS FIRST, discount_id ASC NULLS LAST) AS rn FROM discount_eur ), discounted_orders AS ( SELECT DISTINCT od.discount_id AS discount_id, o.id AS order_id, o.tenant_id AS tenant_id FROM order_discounts od JOIN orders o ON o.id = od.order_id WHERE od.discount_id IN (SELECT discount_id FROM top15 WHERE rn <= 15) AND o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' ), order_rma_flag AS ( SELECT dco.discount_id AS discount_id, dco.order_id AS order_id, EXISTS (SELECT 1 FROM order_returns orr WHERE orr.order_id = dco.order_id) AS has_rma FROM discounted_orders dco ), discount_summary AS ( SELECT dco.discount_id AS discount_id, COUNT(DISTINCT dco.order_id) AS order_count, COUNT(DISTINCT dco.order_id) FILTER (WHERE f.has_rma) AS orders_with_rma FROM discounted_orders dco JOIN order_rma_flag f ON f.discount_id = dco.discount_id AND f.order_id = dco.order_id GROUP BY dco.discount_id ), refunds AS ( SELECT dco.discount_id AS discount_id, SUM(CAST(orr.refund_total / er.rate AS NUMERIC(18,6))) AS refund_eur FROM discounted_orders dco JOIN order_returns orr ON orr.order_id = dco.order_id AND orr.status = 'refunded' JOIN orders o ON o.id = dco.order_id JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = orr.currency_code AND er.as_of = CAST(COALESCE(orr.refunded_at, o.placed_at) AS date) GROUP BY dco.discount_id ), baseline AS ( SELECT o.tenant_id AS tenant_id, COUNT(DISTINCT o.id) AS baseline_order_count, COUNT(DISTINCT o.id) FILTER ( WHERE EXISTS (SELECT 1 FROM order_returns orr2 WHERE orr2.order_id = o.id) ) AS baseline_rma_count FROM orders o WHERE o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND NOT EXISTS (SELECT 1 FROM order_discounts od2 WHERE od2.order_id = o.id) GROUP BY o.tenant_id ) SELECT d.name AS discount_name, COALESCE(cc.code, 'automatic') AS coupon_code, ds.order_count AS order_count, ds.orders_with_rma AS orders_with_rma, 100.0 * ds.orders_with_rma / NULLIF(ds.order_count, 0) AS return_rate_pct, COALESCE(r.refund_eur, 0) AS total_refunded_eur, 100.0 * COALESCE(r.refund_eur, 0) / NULLIF(t.discount_amount_eur, 0) AS refund_pct_of_discount_cost, 100.0 * b.baseline_rma_count / NULLIF(b.baseline_order_count, 0) AS baseline_return_rate_pct, 100.0 * ds.orders_with_rma / NULLIF(ds.order_count, 0) - 100.0 * b.baseline_rma_count / NULLIF(b.baseline_order_count, 0) AS return_rate_lift_pp FROM top15 t JOIN discounts d ON d.id = t.discount_id JOIN discount_summary ds ON ds.discount_id = t.discount_id LEFT JOIN refunds r ON r.discount_id = t.discount_id LEFT JOIN baseline b ON b.tenant_id = d.tenant_id LEFT JOIN LATERAL ( SELECT c.code AS code FROM coupons c WHERE c.discount_id = d.id ORDER BY c.id NULLS LAST LIMIT 1 ) cc ON true WHERE t.rn <= 15 ORDER BY return_rate_lift_pp DESC NULLS LAST, discount_name ASC NULLS LAST, t.discount_id 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: Return and refund rates on discounted orders vs. baseline, 2025 Question: Do orders placed under our top discounts return or get refunded at a higher rate than normal, meaning the real cost is higher than the discount line alone suggests? Grain: one row per discount (discounts.id), same top-15 discount set used in the lift analyses (by 2025 discount_amount_eur, including Volume tier, excluding Subscription delivery since those orders have no revenue to erode) Columns: - discount name - coupon code (or 'automatic') - order count - orders with an RMA opened (order_returns exists, any status) - return rate (%) - total refunded amount, status='refunded' (EUR) - refund amount as % of this discount's total cost - baseline return rate for the same tenant's other 2025 orders with no discount (%) - return rate lift (percentage points, discount minus baseline) Details: Source: order_discounts + discounts (restricted to the 15 discounts) + orders (status != 'cancelled', placed_at in 2025, tenant_id, user_id) + order_returns (order_id, status, refund_total, currency_code, refunded_at) for the discounted side. For baseline, same tenant's other 2025 orders with no row in order_discounts, same return-rate calculation. Convert refund_total to EUR via exchange_rates (base_code='EUR', quote_code=currency_code, as_of=refunded_at date, or placed_at if refunded_at is null). 'Refund amount as % of this discount's total cost' = total refunded EUR / discount_amount_eur for that discount (from the night-1 ranking) * 100. Order by return rate lift (percentage points) descending, discount name as tie-break. Note total row count (expect up to 15). Check your query with the warehouse's dry_run tool, then hand it in with submit.
dry_runWITH discount_eur AS ( SELECT od.discount_id, SUM(od.amount / er.rate) AS discount_amount_eur FROM order_discounts od JOIN orders o ON o.id = od.order_id JOIN discounts d ON d.id = od.discount_id JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = od.currency_code AND er.as_of = CAST(o.placed_at AS date) WHERE o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND d.name <> 'Subscription delivery' GROUP BY od.discount_id ), top15 AS ( SELECT discount_id, discount_amount_eur, ROW_NUMBER() OVER (ORDER BY discount_amount_eur DESC, discount_id ASC) AS rn FROM discount_eur ), discounted_orders AS ( SELECT DISTINCT od.discount_id, o.id AS order_id, o.tenant_id FROM order_discounts od JOIN orders o ON o.id = od.order_id WHERE od.discount_id IN (SELECT discount_id FROM top15 WHERE rn <= 15) AND o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' ), order_rma_flag AS ( SELECT do.discount_id, do.order_id, EXISTS (SELECT 1 FROM order_returns orr WHERE orr.order_id = do.order_id) AS has_rma FROM discounted_orders do ), discount_summary AS ( SELECT do.discount_id, COUNT(DISTINCT do.order_id) AS order_count, COUNT(DISTINCT do.order_id) FILTER (WHERE f.has_rma) AS orders_with_rma FROM discounted_orders do JOIN order_rma_flag f ON f.discount_id = do.discount_id AND f.order_id = do.order_id GROUP BY do.discount_id ), refunds AS ( SELECT do.discount_id, SUM(orr.refund_total / er.rate) AS refund_eur FROM discounted_orders do JOIN order_returns orr ON orr.order_id = do.order_id AND orr.status = 'refunded' JOIN orders o ON o.id = do.order_id JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = orr.currency_code AND er.as_of = CAST(COALESCE(orr.refunded_at, o.placed_at) AS date) GROUP BY do.discount_id ), baseline AS ( SELECT o.tenant_id, COUNT(DISTINCT o.id) AS baseline_order_count, COUNT(DISTINCT o.id) FILTER ( WHERE EXISTS (SELECT 1 FROM order_returns orr2 WHERE orr2.order_id = o.id) ) AS baseline_rma_count FROM orders o WHERE o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND NOT EXISTS (SELECT 1 FROM order_discounts od2 WHERE od2.order_id = o.id) GROUP BY o.tenant_id ) SELECT d.name AS discount_name, COALESCE(cc.code, 'automatic') AS coupon_code, ds.order_count, ds.orders_with_rma, ROUND(100.0 * ds.orders_with_rma / NULLIF(ds.order_count, 0), 2) AS return_rate_pct, ROUND(COALESCE(r.refund_eur, 0), 2) AS total_refunded_eur, ROUND(100.0 * COALESCE(r.refund_eur, 0) / NULLIF(t.discount_amount_eur, 0), 2) AS refund_pct_of_discount_cost, ROUND(100.0 * b.baseline_rma_count / NULLIF(b.baseline_order_count, 0), 2) AS baseline_return_rate_pct, ROUND( 100.0 * ds.orders_with_rma / NULLIF(ds.order_count, 0) - 100.0 * b.baseline_rma_count / NULLIF(b.baseline_order_count, 0), 2 ) AS return_rate_lift_pp FROM top15 t JOIN discounts d ON d.id = t.discount_id JOIN discount_summary ds ON ds.discount_id = t.discount_id LEFT JOIN refunds r ON r.discount_id = t.discount_id LEFT JOIN baseline b ON b.tenant_id = d.tenant_id LEFT JOIN LATERAL ( SELECT c.code FROM coupons c WHERE c.discount_id = d.id ORDER BY c.id LIMIT 1 ) cc ON true WHERE t.rn <= 15 ORDER BY return_rate_lift_pp DESC NULLS LAST, discount_name ASC
STATUS: REJECTED - not a single SELECT statement: syntax error at or near "do"
dry_runWITH discount_eur AS ( SELECT od.discount_id, SUM(od.amount / er.rate) AS discount_amount_eur FROM order_discounts od JOIN orders o ON o.id = od.order_id JOIN discounts d ON d.id = od.discount_id JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = od.currency_code AND er.as_of = CAST(o.placed_at AS date) WHERE o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND d.name <> 'Subscription delivery' GROUP BY od.discount_id ), top15 AS ( SELECT discount_id, discount_amount_eur, ROW_NUMBER() OVER (ORDER BY discount_amount_eur DESC, discount_id ASC) AS rn FROM discount_eur ), discounted_orders AS ( SELECT DISTINCT od.discount_id, o.id AS order_id, o.tenant_id FROM order_discounts od JOIN orders o ON o.id = od.order_id WHERE od.discount_id IN (SELECT discount_id FROM top15 WHERE rn <= 15) AND o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' ), order_rma_flag AS ( SELECT dco.discount_id, dco.order_id, EXISTS (SELECT 1 FROM order_returns orr WHERE orr.order_id = dco.order_id) AS has_rma FROM discounted_orders dco ), discount_summary AS ( SELECT dco.discount_id, COUNT(DISTINCT dco.order_id) AS order_count, COUNT(DISTINCT dco.order_id) FILTER (WHERE f.has_rma) AS orders_with_rma FROM discounted_orders dco JOIN order_rma_flag f ON f.discount_id = dco.discount_id AND f.order_id = dco.order_id GROUP BY dco.discount_id ), refunds AS ( SELECT dco.discount_id, SUM(orr.refund_total / er.rate) AS refund_eur FROM discounted_orders dco JOIN order_returns orr ON orr.order_id = dco.order_id AND orr.status = 'refunded' JOIN orders o ON o.id = dco.order_id JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = orr.currency_code AND er.as_of = CAST(COALESCE(orr.refunded_at, o.placed_at) AS date) GROUP BY dco.discount_id ), baseline AS ( SELECT o.tenant_id, COUNT(DISTINCT o.id) AS baseline_order_count, COUNT(DISTINCT o.id) FILTER ( WHERE EXISTS (SELECT 1 FROM order_returns orr2 WHERE orr2.order_id = o.id) ) AS baseline_rma_count FROM orders o WHERE o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND NOT EXISTS (SELECT 1 FROM order_discounts od2 WHERE od2.order_id = o.id) GROUP BY o.tenant_id ) SELECT d.name AS discount_name, COALESCE(cc.code, 'automatic') AS coupon_code, ds.order_count, ds.orders_with_rma, ROUND(100.0 * ds.orders_with_rma / NULLIF(ds.order_count, 0), 2) AS return_rate_pct, ROUND(COALESCE(r.refund_eur, 0), 2) AS total_refunded_eur, ROUND(100.0 * COALESCE(r.refund_eur, 0) / NULLIF(t.discount_amount_eur, 0), 2) AS refund_pct_of_discount_cost, ROUND(100.0 * b.baseline_rma_count / NULLIF(b.baseline_order_count, 0), 2) AS baseline_return_rate_pct, ROUND( 100.0 * ds.orders_with_rma / NULLIF(ds.order_count, 0) - 100.0 * b.baseline_rma_count / NULLIF(b.baseline_order_count, 0), 2 ) AS return_rate_lift_pp FROM top15 t JOIN discounts d ON d.id = t.discount_id JOIN discount_summary ds ON ds.discount_id = t.discount_id LEFT JOIN refunds r ON r.discount_id = t.discount_id LEFT JOIN baseline b ON b.tenant_id = d.tenant_id LEFT JOIN LATERAL ( SELECT c.code FROM coupons c WHERE c.discount_id = d.id ORDER BY c.id LIMIT 1 ) cc ON true WHERE t.rn <= 15 ORDER BY return_rate_lift_pp DESC NULLS LAST, discount_name ASC
STATUS: COMPILES (dry run on the empty schema, no data read) COLUMNS: discount_name, coupon_code, order_count, orders_with_rma, return_rate_pct, total_refunded_eur, refund_pct_of_discount_cost, baseline_return_rate_pct, return_rate_lift_pp
submitWITH discount_eur AS ( SELECT od.discount_id, SUM(od.amount / er.rate) AS discount_amount_eur FROM order_discounts od JOIN orders o ON o.id = od.order_id JOIN discounts d ON d.id = od.discount_id JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = od.currency_code AND er.as_of = CAST(o.placed_at AS date) WHERE o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND o.status <> 'cancelled' AND d.name <> 'Subscription delivery' GROUP BY od.discount_id ), top15 AS ( SELECT discount_id, discount_amount_eur, ROW_NUMBER() OVER (ORDER BY discount_amount_eur DESC, discount_id ASC) AS rn FROM discount_eur ), discounted_orders AS ( SELECT DISTINCT od.discount_id, o.id AS order_id, o.tenant_id FROM order_discounts od JOIN orders o ON o.id = od.order_id WHERE od.discount_id IN (SELECT discount_id FROM top15 WHERE rn <= 15) AND o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' ), order_rma_flag AS ( SELECT dco.discount_id, dco.order_id, EXISTS (SELECT 1 FROM order_returns orr WHERE orr.order_id = dco.order_id) AS has_rma FROM discounted_orders dco ), discount_summary AS ( SELECT dco.discount_id, COUNT(DISTINCT dco.order_id) AS order_count, COUNT(DISTINCT dco.order_id) FILTER (WHERE f.has_rma) AS orders_with_rma FROM discounted_orders dco JOIN order_rma_flag f ON f.discount_id = dco.discount_id AND f.order_id = dco.order_id GROUP BY dco.discount_id ), refunds AS ( SELECT dco.discount_id, SUM(orr.refund_total / er.rate) AS refund_eur FROM discounted_orders dco JOIN order_returns orr ON orr.order_id = dco.order_id AND orr.status = 'refunded' JOIN orders o ON o.id = dco.order_id JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = orr.currency_code AND er.as_of = CAST(COALESCE(orr.refunded_at, o.placed_at) AS date) GROUP BY dco.discount_id ), baseline AS ( SELECT o.tenant_id, COUNT(DISTINCT o.id) AS baseline_order_count, COUNT(DISTINCT o.id) FILTER ( WHERE EXISTS (SELECT 1 FROM order_returns orr2 WHERE orr2.order_id = o.id) ) AS baseline_rma_count FROM orders o WHERE o.status <> 'cancelled' AND o.placed_at >= DATE '2025-01-01' AND o.placed_at < DATE '2026-01-01' AND NOT EXISTS (SELECT 1 FROM order_discounts od2 WHERE od2.order_id = o.id) GROUP BY o.tenant_id ) SELECT d.name AS discount_name, COALESCE(cc.code, 'automatic') AS coupon_code, ds.order_count, ds.orders_with_rma, ROUND(100.0 * ds.orders_with_rma / NULLIF(ds.order_count, 0), 2) AS return_rate_pct, ROUND(COALESCE(r.refund_eur, 0), 2) AS total_refunded_eur, ROUND(100.0 * COALESCE(r.refund_eur, 0) / NULLIF(t.discount_amount_eur, 0), 2) AS refund_pct_of_discount_cost, ROUND(100.0 * b.baseline_rma_count / NULLIF(b.baseline_order_count, 0), 2) AS baseline_return_rate_pct, ROUND( 100.0 * ds.orders_with_rma / NULLIF(ds.order_count, 0) - 100.0 * b.baseline_rma_count / NULLIF(b.baseline_order_count, 0), 2 ) AS return_rate_lift_pp FROM top15 t JOIN discounts d ON d.id = t.discount_id JOIN discount_summary ds ON ds.discount_id = t.discount_id LEFT JOIN refunds r ON r.discount_id = t.discount_id LEFT JOIN baseline b ON b.tenant_id = d.tenant_id LEFT JOIN LATERAL ( SELECT c.code FROM coupons c WHERE c.discount_id = d.id ORDER BY c.id LIMIT 1 ) cc ON true WHERE t.rn <= 15 ORDER BY return_rate_lift_pp DESC NULLS LAST, discount_name ASC
Submitted to tonight's batch. Your work on this request is done.