Queries · q175 · Head of B2B Commerce · week 1, night 2
“Among B2B wholesale opportunities that were actually lost in new-customer acquisition (not renewals), which stage were they sitting in just before they were marked Closed Lost — i.e. where is the real graveyard?”
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 Samuel Whitcombe, Head of Merchant Success
B2B merchants say their sales teams struggle. How do their pipelines convert from lead to won deal, how long does it take, and what predicts winning or losing?
The manager, at the start of week 1
Note to Daniel Kim — B2B pipeline health
Daniel,
Samuel and I need to give the B2B wholesale merchants (and our own leadership) a clear-eyed view of why their sales teams are struggling to close business. This feeds a joint recommendation Samuel and I are making next week on where to invest — better pipeline coaching/tooling for merchants vs. changes to how we structure stages, quotes and terms in the product. So I want the picture to be accurate more than exhaustive — pick your battles across the nights you have.
Here's what I actually want to know, in plain terms:
Priority order is 1, 2, 3 first — those directly shape the recommendation. 4 and 5 are valuable but can slip if you run out of slots this week.
Keep each report to one clean question — I'd rather have four sharp answers than one crowded one. Flag anything that looks like a data quality issue rather than a real trend.
Thanks — looking forward to the Friday memo.
Patrick
Subject: A few questions from the Merchant Success side
Hi Daniel,
Good to see this getting attention this week. A few things I'd love your analysis to help answer, from the merchant-health angle:
Happy to compare notes before you finalize anything for Patrick.
Best, Samuel
The analyst · week 1, night 2, request 1 of 3
Night 1's big finding is a data-model gotcha, not yet a business trend: every wholesale tenant's 'New business' pipeline shows zero opportunities (open or closed) at Prospecting/Qualification/Proposal/Negotiation — closed deals only ever show as the terminal Closed Won/Closed Lost stage. That means current stage_id can't answer 'where do deals die' for lost opportunities; it only tells us the final outcome. It also means Report 3 (won/lost) and Report 2 (cycle time) from night 1 silently pooled two very different populations together: 'New business' (true lead-to-signed-deal acquisition, which is what Patrick and Samuel are actually asking about) and 'Renewals' (existing-customer renew/churn, where 'Renewed' carries a 1.0 default win probability and is a fundamentally easier motion). Tonight I'm spending all three slots fixing this at the root: (1) reconstruct the real graveyard stage from change_history for New-business-only lost opportunities, (2) redo the won/lost characteristics comparison scoped to New business only, (3) redo the cycle-time trend scoped to New business only. This keeps priorities 1-2-3 intact but makes them actually answer the question Patrick asked, rather than a renewals-diluted version of it. I'm holding Samuel's stale-pipeline-risk question and Patrick's #4/#5 for night 3, once I see whether change_history actually has usable coverage for the graveyard question (flagged as a risk in request 1) and whether the split reports change the picture.
The engineer · 1 dry run on the empty schema, then the submission
WITH lost_opps AS ( SELECT o.id, o.tenant_id FROM opportunities o JOIN tenants t ON t.id = o.tenant_id JOIN pipelines p ON p.id = o.pipeline_id WHERE p.name = 'New business' AND o.is_closed = true AND o.is_won = false AND (t.industry ILIKE '%industrial%' OR t.industry ILIKE '%office equipment%' OR t.industry ILIKE '%wholesal%') ), last_stage_change AS ( SELECT DISTINCT ON (ch.entity_id) ch.entity_id, ch.tenant_id, (ch.before_data->>'stage_id') AS before_stage_id FROM change_history ch JOIN lost_opps lo ON lo.id = ch.entity_id AND lo.tenant_id = ch.tenant_id WHERE ch.entity_table = 'opportunities' AND 'stage_id' = ANY(ch.changed_columns) ORDER BY ch.entity_id, ch.occurred_at DESC ), opp_stage AS ( SELECT lo.id AS opportunity_id, os.name AS stage_name, os.sort_position AS stage_order FROM lost_opps lo LEFT JOIN last_stage_change lsc ON lsc.entity_id = lo.id LEFT JOIN opportunity_stages os ON os.id = CAST(lsc.before_stage_id AS bigint) ) SELECT COALESCE(stage_name, 'No stage transition recorded') AS stage_name, stage_order, COUNT(*) AS lost_opportunities_last_seen_here, ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER (), 1) AS pct_of_total_lost_in_new_business, SUM(COUNT(*)) OVER () AS total_lost_opportunities_new_business FROM opp_stage GROUP BY 1, 2 ORDER BY lost_opportunities_last_seen_here DESC, stage_name
“Query compiles cleanly. Submitting.”
“Submitted. The report groups lost New-business opportunities at wholesale tenants by the stage recorded in their last stage_id change in change_history before closing, with a fallback bucket for opportunities lacking any logged stage transition — and I flagged in the note that the analyst should check whether that fallback bucket dominates, since that would mean the audit trail can't really answer the "graveyard stage" question.”
As Daniel Kim saw it, from the small dataset the agents worked on
stage_name | stage_order | lost_opportunities_last_seen_here | pct_of_total_lost_in_new_business | total_lost_opportunities_new_business -----------------------------+-------------+-----------------------------------+-----------------------------------+-------------------------------------- No stage transition recorded | NULL | 30 | 100.0 | 30 (1 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
->> is PostgreSQL's; the standard's JSON_VALUE, which PostgreSQL, MySQL and T-SQL support, returns the same text for a scalar. (docs/RUNNER.md, Dialects)WITH lost_opps AS ( SELECT o.id, o.tenant_id SELECT o.id AS id, o.tenant_id AS tenant_id FROM opportunities o JOIN tenants t ON t.id = o.tenant_id JOIN pipelines p ON p.id = o.pipeline_id WHERE p.name = 'New business' AND o.is_closed = true AND o.is_won = false AND (t.industry ILIKE '%industrial%' OR t.industry ILIKE '%office equipment%' OR t.industry ILIKE '%wholesal%')),last_stage_change AS ( SELECT DISTINCT ON (ch.entity_id) ch.entity_id, ch.tenant_id, (ch.before_data->>'stage_id') AS before_stage_id FROM change_history ch JOIN lost_opps lo ON lo.id = ch.entity_id AND lo.tenant_id = ch.tenant_id WHERE ch.entity_table = 'opportunities' AND 'stage_id' = ANY(ch.changed_columns) ORDER BY ch.entity_id, ch.occurred_at DESC SELECT entity_id, tenant_id, before_stage_id FROM ( SELECT ch.entity_id AS entity_id, ch.tenant_id AS tenant_id, (JSON_VALUE(ch.before_data, '$.stage_id')) AS before_stage_id, ROW_NUMBER() OVER (PARTITION BY ch.entity_id ORDER BY ch.occurred_at DESC NULLS FIRST, ch.id DESC NULLS FIRST) AS rn FROM change_history ch JOIN lost_opps lo ON lo.id = ch.entity_id AND lo.tenant_id = ch.tenant_id WHERE ch.entity_table = 'opportunities' AND {{equals_any('stage_id', ch.changed_columns)}} ) ranked WHERE rn = 1),opp_stage AS ( SELECT lo.id AS opportunity_id, os.name AS stage_name, os.sort_position AS stage_order FROM lost_opps lo LEFT JOIN last_stage_change lsc ON lsc.entity_id = lo.id LEFT JOIN opportunity_stages os ON os.id = CAST(lsc.before_stage_id AS bigint))SELECT COALESCE(stage_name, 'No stage transition recorded') AS stage_name, stage_order, COUNT(*) AS lost_opportunities_last_seen_here, ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER (), 1) AS pct_of_total_lost_in_new_business, 100.0 * COUNT(*) / SUM(COUNT(*)) OVER () AS pct_of_total_lost_in_new_business, SUM(COUNT(*)) OVER () AS total_lost_opportunities_new_businessFROM opp_stageGROUP BY 1, 2ORDER BY lost_opportunities_last_seen_here DESC, stage_nameORDER BY lost_opportunities_last_seen_here DESC NULLS FIRST, stage_name NULLS LAST
WITH lost_opps AS ( SELECT o.id AS id, o.tenant_id AS tenant_id FROM opportunities o JOIN tenants t ON t.id = o.tenant_id JOIN pipelines p ON p.id = o.pipeline_id WHERE p.name = 'New business' AND o.is_closed = true AND o.is_won = false AND (t.industry ILIKE '%industrial%' OR t.industry ILIKE '%office equipment%' OR t.industry ILIKE '%wholesal%') ), last_stage_change AS ( SELECT entity_id, tenant_id, before_stage_id FROM ( SELECT ch.entity_id AS entity_id, ch.tenant_id AS tenant_id, (JSON_VALUE(ch.before_data, '$.stage_id')) AS before_stage_id, ROW_NUMBER() OVER (PARTITION BY ch.entity_id ORDER BY ch.occurred_at DESC NULLS FIRST, ch.id DESC NULLS FIRST) AS rn FROM change_history ch JOIN lost_opps lo ON lo.id = ch.entity_id AND lo.tenant_id = ch.tenant_id WHERE ch.entity_table = 'opportunities' AND 'stage_id' IN (SELECT dbo_unnest.dbo_element FROM UNNEST(ch.changed_columns) AS dbo_unnest(dbo_element)) ) ranked WHERE rn = 1 ), opp_stage AS ( SELECT lo.id AS opportunity_id, os.name AS stage_name, os.sort_position AS stage_order FROM lost_opps lo LEFT JOIN last_stage_change lsc ON lsc.entity_id = lo.id LEFT JOIN opportunity_stages os ON os.id = CAST(lsc.before_stage_id AS bigint) ) SELECT COALESCE(stage_name, 'No stage transition recorded') AS stage_name, stage_order, COUNT(*) AS lost_opportunities_last_seen_here, 100.0 * COUNT(*) / SUM(COUNT(*)) OVER () AS pct_of_total_lost_in_new_business, SUM(COUNT(*)) OVER () AS total_lost_opportunities_new_business FROM opp_stage GROUP BY 1, 2 ORDER BY lost_opportunities_last_seen_here DESC NULLS FIRST, stage_name NULLS LAST
WITH lost_opps AS ( SELECT o.id AS id, o.tenant_id AS tenant_id FROM opportunities o JOIN tenants t ON t.id = o.tenant_id JOIN pipelines p ON p.id = o.pipeline_id WHERE p.name = 'New business' AND o.is_closed = true AND o.is_won = false AND (t.industry ILIKE '%industrial%' OR t.industry ILIKE '%office equipment%' OR t.industry ILIKE '%wholesal%') ), last_stage_change AS ( SELECT entity_id, tenant_id, before_stage_id FROM ( SELECT ch.entity_id AS entity_id, ch.tenant_id AS tenant_id, (JSON_VALUE(ch.before_data, '$.stage_id')) AS before_stage_id, ROW_NUMBER() OVER (PARTITION BY ch.entity_id ORDER BY ch.occurred_at DESC NULLS FIRST, ch.id DESC NULLS FIRST) AS rn FROM change_history ch JOIN lost_opps lo ON lo.id = ch.entity_id AND lo.tenant_id = ch.tenant_id WHERE ch.entity_table = 'opportunities' AND 'stage_id' = ANY(CAST('[' || SUBSTRING(ch.changed_columns FROM 2 FOR CHAR_LENGTH(ch.changed_columns) - 2) || ']' AS text[])) ) ranked WHERE rn = 1 ), opp_stage AS ( SELECT lo.id AS opportunity_id, os.name AS stage_name, os.sort_position AS stage_order FROM lost_opps lo LEFT JOIN last_stage_change lsc ON lsc.entity_id = lo.id LEFT JOIN opportunity_stages os ON os.id = CAST(lsc.before_stage_id AS bigint) ) SELECT COALESCE(stage_name, 'No stage transition recorded') AS stage_name, stage_order, COUNT(*) AS lost_opportunities_last_seen_here, 100.0 * COUNT(*) / SUM(COUNT(*)) OVER () AS pct_of_total_lost_in_new_business, SUM(COUNT(*)) OVER () AS total_lost_opportunities_new_business FROM opp_stage GROUP BY 1, 2 ORDER BY lost_opportunities_last_seen_here DESC NULLS FIRST, stage_name NULLS LAST
WITH lost_opps AS ( SELECT o.id AS id, o.tenant_id AS tenant_id FROM opportunities o JOIN tenants t ON t.id = o.tenant_id JOIN pipelines p ON p.id = o.pipeline_id WHERE p.name = 'New business' AND o.is_closed = true AND o.is_won = false AND (t.industry ILIKE '%industrial%' OR t.industry ILIKE '%office equipment%' OR t.industry ILIKE '%wholesal%') ), last_stage_change AS ( SELECT entity_id, tenant_id, before_stage_id FROM ( SELECT ch.entity_id AS entity_id, ch.tenant_id AS tenant_id, (JSON_VALUE(ch.before_data, '$.stage_id')) AS before_stage_id, ROW_NUMBER() OVER (PARTITION BY ch.entity_id ORDER BY ch.occurred_at DESC NULLS FIRST, ch.id DESC NULLS FIRST) AS rn FROM change_history ch JOIN lost_opps lo ON lo.id = ch.entity_id AND lo.tenant_id = ch.tenant_id WHERE ch.entity_table = 'opportunities' AND CASE WHEN ch.changed_columns IS NULL THEN NULL WHEN empty(ch.changed_columns) THEN false WHEN 'stage_id' IS NULL THEN NULL WHEN has(ch.changed_columns, 'stage_id') THEN true WHEN has(ch.changed_columns, NULL) THEN NULL ELSE false END ) ranked WHERE rn = 1 ), opp_stage AS ( SELECT lo.id AS opportunity_id, os.name AS stage_name, os.sort_position AS stage_order FROM lost_opps lo LEFT JOIN last_stage_change lsc ON lsc.entity_id = lo.id LEFT JOIN opportunity_stages os ON os.id = CAST(lsc.before_stage_id AS bigint) ) SELECT COALESCE(stage_name, 'No stage transition recorded') AS stage_name, stage_order, COUNT(*) AS lost_opportunities_last_seen_here, 100.0 * COUNT(*) / SUM(COUNT(*)) OVER () AS pct_of_total_lost_in_new_business, SUM(COUNT(*)) OVER () AS total_lost_opportunities_new_business FROM opp_stage GROUP BY 1, 2 ORDER BY lost_opportunities_last_seen_here DESC NULLS FIRST, stage_name 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 Daniel Kim (analyst for the Head of B2B Commerce): Title: New Business Pipeline: Stage Where Lost Opportunities Were Last Seen Question: Among B2B wholesale opportunities that were actually lost in new-customer acquisition (not renewals), which stage were they sitting in just before they were marked Closed Lost — i.e. where is the real graveyard? Grain: one row per New-business pipeline stage (plus one row for opportunities with no recorded stage transition) Columns: - stage_name - stage_order - lost_opportunities_last_seen_here (count) - pct_of_total_lost_in_new_business - total_lost_opportunities_new_business (constant, for reference) Details: Night 1's stage report showed that for every wholesale tenant, the 'New business' pipeline has ZERO opportunities currently open or closed at Prospecting/Qualification/Proposal/Negotiation — all closed opportunities show only as 'Closed Won' or 'Closed Lost' (the terminal stages). This means opportunities.stage_id only reflects the FINAL state and does not preserve where a lost deal actually stalled before dying — the earlier 'graveyard stage' question cannot be answered from current stage_id alone. This report reconstructs it from history. Scope: opportunities where tenant_id is a wholesale merchant (tenants.industry ILIKE '%industrial%' OR '%office equipment%' OR '%wholesal%'), pipeline_id = the tenant's 'New business' pipeline (join pipelines on name = 'New business'), is_closed = true, is_won = false. For each such opportunity, look at change_history where entity_table = 'opportunities', entity_id = opportunities.id, changed_columns includes 'stage_id', ordered by occurred_at. Take the most recent such change event and read before_data->>'stage_id' as the stage the opportunity was in immediately before that final change (presumed to be the move into Closed Lost); join that id to opportunity_stages for stage_name/stage_order. If an opportunity has no change_history row with a stage_id change at all, bucket it as 'No stage transition recorded' (it was apparently created or recorded as lost without a logged progression). Group by the resulting stage (or the no-record bucket), count opportunities, and compute each group's share of all lost New-business opportunities for this tenant set. Order by lost_opportunities_last_seen_here descending. Please flag in the report notes if change_history has little or no coverage for these opportunities (e.g., most rows fall into 'No transition recorded'), since that would mean the audit trail can't answer this and we should say so plainly rather than over-interpret a small sample. Check your query with the warehouse's dry_run tool, then hand it in with submit.
dry_runWITH lost_opps AS ( SELECT o.id, o.tenant_id FROM opportunities o JOIN tenants t ON t.id = o.tenant_id JOIN pipelines p ON p.id = o.pipeline_id WHERE p.name = 'New business' AND o.is_closed = true AND o.is_won = false AND (t.industry ILIKE '%industrial%' OR t.industry ILIKE '%office equipment%' OR t.industry ILIKE '%wholesal%') ), last_stage_change AS ( SELECT DISTINCT ON (ch.entity_id) ch.entity_id, ch.tenant_id, (ch.before_data->>'stage_id') AS before_stage_id FROM change_history ch JOIN lost_opps lo ON lo.id = ch.entity_id AND lo.tenant_id = ch.tenant_id WHERE ch.entity_table = 'opportunities' AND 'stage_id' = ANY(ch.changed_columns) ORDER BY ch.entity_id, ch.occurred_at DESC ), opp_stage AS ( SELECT lo.id AS opportunity_id, os.name AS stage_name, os.sort_position AS stage_order FROM lost_opps lo LEFT JOIN last_stage_change lsc ON lsc.entity_id = lo.id LEFT JOIN opportunity_stages os ON os.id = CAST(lsc.before_stage_id AS bigint) ) SELECT COALESCE(stage_name, 'No stage transition recorded') AS stage_name, stage_order, COUNT(*) AS lost_opportunities_last_seen_here, ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER (), 1) AS pct_of_total_lost_in_new_business, SUM(COUNT(*)) OVER () AS total_lost_opportunities_new_business FROM opp_stage GROUP BY 1, 2 ORDER BY lost_opportunities_last_seen_here DESC, stage_name
STATUS: COMPILES (dry run on the empty schema, no data read) COLUMNS: stage_name, stage_order, lost_opportunities_last_seen_here, pct_of_total_lost_in_new_business, total_lost_opportunities_new_business
submitWITH lost_opps AS ( SELECT o.id, o.tenant_id FROM opportunities o JOIN tenants t ON t.id = o.tenant_id JOIN pipelines p ON p.id = o.pipeline_id WHERE p.name = 'New business' AND o.is_closed = true AND o.is_won = false AND (t.industry ILIKE '%industrial%' OR t.industry ILIKE '%office equipment%' OR t.industry ILIKE '%wholesal%') ), last_stage_change AS ( SELECT DISTINCT ON (ch.entity_id) ch.entity_id, ch.tenant_id, (ch.before_data->>'stage_id') AS before_stage_id FROM change_history ch JOIN lost_opps lo ON lo.id = ch.entity_id AND lo.tenant_id = ch.tenant_id WHERE ch.entity_table = 'opportunities' AND 'stage_id' = ANY(ch.changed_columns) ORDER BY ch.entity_id, ch.occurred_at DESC ), opp_stage AS ( SELECT lo.id AS opportunity_id, os.name AS stage_name, os.sort_position AS stage_order FROM lost_opps lo LEFT JOIN last_stage_change lsc ON lsc.entity_id = lo.id LEFT JOIN opportunity_stages os ON os.id = CAST(lsc.before_stage_id AS bigint) ) SELECT COALESCE(stage_name, 'No stage transition recorded') AS stage_name, stage_order, COUNT(*) AS lost_opportunities_last_seen_here, ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER (), 1) AS pct_of_total_lost_in_new_business, SUM(COUNT(*)) OVER () AS total_lost_opportunities_new_business FROM opp_stage GROUP BY 1, 2 ORDER BY lost_opportunities_last_seen_here DESC, stage_name
Submitted to tonight's batch. Your work on this request is done.