DB Olympics

Queries · q236 · Chief Operating Officer · week 2, night 2

Purchase order replenishment performance by quarter, 2025 (mature POs only, non-censored)

“Once we only look at purchase orders old enough to have fully played out, are we genuinely behind on restocking, or was the Q4 backlog just a year-end timing artifact?”

Manager · asked the questionPriya RamanChief Operating Officer. Came from a third-party logistics provider. Wants to know how reliable the delivery promise is that merchants make on Tidewick.
Analyst · planned the reportLena HartmannSkeptical: checks definitions and sanity totals before trusting a trend.
Engineer · wrote the SQLFelixJunior engineer who nests subqueries in FROM and WHERE (IN, EXISTS, correlated subqueries) rather than CTEs.

Fictional people at a fictional company, each played by a language-model agent. Who they are and what they are told

  1. Priya Raman writes to Lena Hartmann

    The manager, at the start of week 2

    Subject: Carrier picture confirms it — now I want to know what we can actually fix

    Lena,

    This is exactly the clarity I was hoping for — "it's the carriers, not the warehouses" is a much more actionable story than "fulfillment is inconsistent." A few things jumped out, including from what Daniel's and Tomasz's teams posted this week, and I want to chase them down before we draft anything for merchants or carrier partners.

    What surprised me: Albion Parcel going from solid to broken in November and staying broken through December is different from Landfall and Lowlands, which sound like long-term weak performers we've just tolerated. One is a relationship we may need to end; the other looks like it could be an acute problem (capacity, a route, a depot) that might actually be fixable if we understood the trigger. I don't want to lump all three into one "bad carriers" conversation when they may need completely different responses.

    I also noticed Daniel's team's finding that "where's my order" tickets spike in lockstep with late deliveries each November/December — that's a direct line from the carrier problem to support cost and customer satisfaction, and I'd like to be able to say that explicitly in my own numbers rather than borrowing his.

    Questions for this week:

    1. Of the three weak carriers, which ones are tied to specific regions, lanes, or merchants, versus broadly weak everywhere? I want to know if Albion's November collapse was localized (a single hub, country, or set of merchants) or general — that tells us whether this is a "fix one bad link" problem or a "this carrier can't be trusted" problem.
    2. If we could bring our weak carriers up to the standard of our good ones, how much of our delivery unreliability would actually go away? I want a sense of the ceiling — is this 80% of our problem, or just the long tail?
    3. Can you give me a clean, non-censored view of whether we're genuinely behind on restocking from suppliers, or if that was a year-end timing artifact? You flagged this yourself — I'd like it resolved before I mention supplier reliability to anyone.
    4. Is there any sign that the picking discrepancies Tomasz's team flagged (concentrated in apparel/footwear) are contributing to delivery delays or failed promises — e.g., orders stalling or being corrected after the fact — or is that purely a shrinkage/accounting issue with no customer-facing effect? I want to know if it belongs in the same story as delivery reliability or is a separate track entirely.

    If it's a tight week, prioritize #1 and #2 — I want the carrier narrative airtight before I take it anywhere. Returns/restock cycle time can wait one more week if needed.

    Thanks, Priya

  2. Lena Hartmann plans the night

    The analyst · week 2, night 2, request 2 of 3

    Night 1 resolved priority #1 cleanly, and the answer is more nuanced than "weak carrier = weak carrier": (a) Albion Parcel's collapse is NOT localized -- it hit both countries it serves (UK and Ireland) and literally every origin warehouse/merchant it ships for, uniformly, the moment November started (pct-within-3-days fell from ~70-93% to ~0-40% everywhere at once, transit days jumping from ~2-2.5 to 8-17 days across the board). That rules out "one bad depot" or "one bad merchant" and points to a systemic capacity failure at Albion itself -- useful to know before the carrier conversation, but it's a harder story (a carrier that broke under load everywhere), not an easy single-fix. (b) Landfall Freight is uniformly bad across all 18 countries it serves (14-38% within 3 days everywhere, no bright spots) -- genuinely a "this carrier can't be trusted" case. (c) Lowlands Express is the interesting exception: it's actually solid on its home turf (Netherlands 79.9%, Belgium 69.3%, France 63.0% within 3 days -- 64.5% of its volume) but poor everywhere else (15-36% within 3 days on the remaining 35.5%). That's a lane-reassignment fix, not a carrier breakup. Tonight I'm covering priority #2 (the reliability ceiling, using a clean full-year carrier table so I can compute the weighted improvement by hand) and starting on #3 (a non-censored PO view, restricting to purchase orders old enough to have fully played out) and #4 (whether the apparel/footwear picking discrepancies Fatima's team flagged show up in delivery shortfalls, not just inventory accounting).

    Purchase order replenishment performance by quarter, 2025 (mature POs only, non-censored)
    Question
    Once we only look at purchase orders old enough to have fully played out, are we genuinely behind on restocking, or was the Q4 backlog just a year-end timing artifact?
    One row per
    calendar quarter of ordered_at, 2025
    Columns
    • quarter (e.g. 2025-Q1)
    • pos_ordered_total (all POs with ordered_at in that quarter, for reference)
    • pos_matured (subset of pos_ordered_total with ordered_at <= 2025-11-01, i.e. at least 60 days elapsed before the 2025-12-31 cutoff)
    • pos_matured_received (matured subset with received_at not null)
    • pct_matured_still_open (matured subset with received_at null and status in ('submitted','partially_received') -- a true backlog signal, not a recency artifact)
    • avg_actual_lead_days (DATEDIFF(day, ordered_at, received_at), matured+received subset only)
    • pct_received_late (matured subset with both expected_at and received_at set, where received_at > expected_at)
    • avg_days_late (matured, late subset only)
  3. Felix writes the SQL

    The engineer · 3 dry runs on the empty schema, then the submission

    Dry run 1 · STATUS: REJECTED
    SELECT
      EXTRACT(QUARTER FROM ordered_at) AS q,
      COUNT(*) AS pos_ordered_total,
      COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01') AS pos_matured,
      AVG(CASE WHEN received_at IS NOT NULL THEN DATEDIFF(day, ordered_at, received_at) END) AS avg_lead
    FROM purchase_orders
    WHERE ordered_at >= DATE '2025-01-01' AND ordered_at < DATE '2026-01-01'
    GROUP BY 1
    ORDER BY 1
    Dry run 2 · 1 change from the dry run before · STATUS: COMPILES (dry run on the empty schema, no data read)
    SELECT  EXTRACT(QUARTER FROM ordered_at) AS q,  COUNT(*) AS pos_ordered_total,  COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01') AS pos_matured,  AVG(CASE WHEN received_at IS NOT NULL THEN DATEDIFF(day, ordered_at, received_at) END) AS avg_lead  AVG(CASE WHEN received_at IS NOT NULL THEN DATE_DIFF('day', ordered_at, received_at) END) AS avg_leadFROM purchase_ordersWHERE ordered_at >= DATE '2025-01-01' AND ordered_at < DATE '2026-01-01'GROUP BY 1ORDER BY 1
    Dry run 3 · 3 changes from the dry run before · STATUS: COMPILES (dry run on the empty schema, no data read)
    SELECT  EXTRACT(QUARTER FROM ordered_at) AS q,  '2025-Q' || CAST(EXTRACT(QUARTER FROM ordered_at) AS INTEGER) AS quarter,  COUNT(*) AS pos_ordered_total,  COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01') AS pos_matured,  AVG(CASE WHEN received_at IS NOT NULL THEN DATE_DIFF('day', ordered_at, received_at) END) AS avg_lead  COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01' AND received_at IS NOT NULL) AS pos_matured_received,  ROUND(100.0 * COUNT(*) FILTER (      WHERE ordered_at <= DATE '2025-11-01'        AND received_at IS NULL        AND status IN ('submitted','partially_received')    ) / NULLIF(COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01'), 0), 1) AS pct_matured_still_open,  ROUND(AVG(CASE      WHEN ordered_at <= DATE '2025-11-01' AND received_at IS NOT NULL      THEN DATE_DIFF('day', ordered_at, received_at)    END), 1) AS avg_actual_lead_days,  ROUND(100.0 * COUNT(*) FILTER (      WHERE ordered_at <= DATE '2025-11-01'        AND expected_at IS NOT NULL        AND received_at IS NOT NULL        AND received_at > expected_at    ) / NULLIF(COUNT(*) FILTER (      WHERE ordered_at <= DATE '2025-11-01'        AND expected_at IS NOT NULL        AND received_at IS NOT NULL    ), 0), 1) AS pct_received_late,  ROUND(AVG(CASE      WHEN ordered_at <= DATE '2025-11-01'        AND expected_at IS NOT NULL        AND received_at IS NOT NULL        AND received_at > expected_at      THEN DATE_DIFF('day', expected_at, received_at)    END), 1) AS avg_days_lateFROM purchase_ordersWHERE ordered_at >= DATE '2025-01-01' AND ordered_at < DATE '2026-01-01'GROUP BY 1ORDER BY 1GROUP BY EXTRACT(QUARTER FROM ordered_at)ORDER BY quarter ASC

    “This matches the requested grain (one row per quarter), has 8 columns within the width limit, and the query compiles cleanly. Submitting now.”

    Submitted · the SQL of the last dry run, unchanged · Submitted to tonight's batch. Your work on this request is done.

    “Submitted. The report gives one row per 2025 quarter with the requested PO maturity and backlog metrics, using a 60-day maturity cutoff (ordered_at <= 2025-11-01) as specified, so Q4's matured figures intentionally reflect only October-ordered POs.”

  4. The report comes back overnight

    As Lena Hartmann saw it, from the small dataset the agents worked on

    quarter | pos_ordered_total | pos_matured | pos_matured_received | pct_matured_still_open | avg_actual_lead_days | pct_received_late | avg_days_late
    --------+-------------------+-------------+----------------------+------------------------+----------------------+-------------------+--------------
    2025-Q1 |              1183 |        1183 |                 1183 |                    0.0 |                 22.4 |              52.9 |           6.1
    2025-Q2 |              1209 |        1209 |                 1209 |                    0.0 |                 20.0 |              47.4 |           3.8
    2025-Q3 |              1238 |        1238 |                 1238 |                    0.0 |                 20.8 |              54.0 |           4.8
    2025-Q4 |              1588 |         464 |                  460 |                    0.9 |                 20.7 |              59.3 |           4.5
    (4 rows)
  5. What came of it

    The memo and the queries that followed; links found by title, so inferred

    It builds on:

  6. It becomes benchmark query q236

    Changed only where the benchmark needs the same result on every system

    • placeholderdate diff days (2×)
    • numericsdocs/NUMERICS.md: rounding removed; results keep full precision; AVG over an integer computed as a decimal: AVG(CAST(x AS NUMERIC(18,6)))
    • determinismthe standard leaves the place of NULLs in a sort to the implementation: NULLS LAST ascending, NULLS FIRST descending, as in the reference results
    The benchmark text, against the submitted SQL
    SELECT  '2025-Q' || CAST(EXTRACT(QUARTER FROM ordered_at) AS INTEGER) AS quarter,  COUNT(*) AS pos_ordered_total,  COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01') AS pos_matured,  COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01' AND received_at IS NOT NULL) AS pos_matured_received,  ROUND(100.0 * COUNT(*) FILTER (      WHERE ordered_at <= DATE '2025-11-01'        AND received_at IS NULL        AND status IN ('submitted','partially_received')    ) / NULLIF(COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01'), 0), 1) AS pct_matured_still_open,  ROUND(AVG(CASE      WHEN ordered_at <= DATE '2025-11-01' AND received_at IS NOT NULL      THEN DATE_DIFF('day', ordered_at, received_at)    END), 1) AS avg_actual_lead_days,  ROUND(100.0 * COUNT(*) FILTER (      WHERE ordered_at <= DATE '2025-11-01'        AND expected_at IS NOT NULL        AND received_at IS NOT NULL        AND received_at > expected_at    ) / NULLIF(COUNT(*) FILTER (      WHERE ordered_at <= DATE '2025-11-01'        AND expected_at IS NOT NULL        AND received_at IS NOT NULL    ), 0), 1) AS pct_received_late,  ROUND(AVG(CASE      WHEN ordered_at <= DATE '2025-11-01'        AND expected_at IS NOT NULL        AND received_at IS NOT NULL        AND received_at > expected_at      THEN DATE_DIFF('day', expected_at, received_at)    END), 1) AS avg_days_late  100.0 * COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01'  AND received_at IS NULL  AND status IN ('submitted','partially_received')) / NULLIF(COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01'), 0) AS pct_matured_still_open,  AVG(CAST(CASEWHEN ordered_at <= DATE '2025-11-01' AND received_at IS NOT NULLTHEN {{date_diff_days(ordered_at, received_at)}}END AS NUMERIC(18,6))) AS avg_actual_lead_days,  100.0 * COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01'  AND expected_at IS NOT NULL  AND received_at IS NOT NULL  AND received_at > expected_at) / NULLIF(COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01'  AND expected_at IS NOT NULL  AND received_at IS NOT NULL), 0) AS pct_received_late,  AVG(CAST(CASEWHEN ordered_at <= DATE '2025-11-01'  AND expected_at IS NOT NULL  AND received_at IS NOT NULL  AND received_at > expected_atTHEN {{date_diff_days(expected_at, received_at)}}END AS NUMERIC(18,6))) AS avg_days_lateFROM purchase_ordersWHERE ordered_at >= DATE '2025-01-01' AND ordered_at < DATE '2026-01-01'GROUP BY EXTRACT(QUARTER FROM ordered_at)ORDER BY quarter ASCORDER BY quarter ASC NULLS LAST
    SELECT
      '2025-Q' || CAST(EXTRACT(QUARTER FROM ordered_at) AS INTEGER) AS quarter,
      COUNT(*) AS pos_ordered_total,
      COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01') AS pos_matured,
      COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01' AND received_at IS NOT NULL) AS pos_matured_received,
      100.0 * COUNT(*) FILTER (
    WHERE ordered_at <= DATE '2025-11-01'
      AND received_at IS NULL
      AND status IN ('submitted','partially_received')
    ) / NULLIF(COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01'), 0) AS pct_matured_still_open,
      AVG(CAST(CASE
    WHEN ordered_at <= DATE '2025-11-01' AND received_at IS NOT NULL
    THEN DATE_DIFF('day', ordered_at, received_at)
    END AS NUMERIC(18,6))) AS avg_actual_lead_days,
      100.0 * COUNT(*) FILTER (
    WHERE ordered_at <= DATE '2025-11-01'
      AND expected_at IS NOT NULL
      AND received_at IS NOT NULL
      AND received_at > expected_at
    ) / NULLIF(COUNT(*) FILTER (
    WHERE ordered_at <= DATE '2025-11-01'
      AND expected_at IS NOT NULL
      AND received_at IS NOT NULL
    ), 0) AS pct_received_late,
      AVG(CAST(CASE
    WHEN ordered_at <= DATE '2025-11-01'
      AND expected_at IS NOT NULL
      AND received_at IS NOT NULL
      AND received_at > expected_at
    THEN DATE_DIFF('day', expected_at, received_at)
    END AS NUMERIC(18,6))) AS avg_days_late
    FROM purchase_orders
    WHERE ordered_at >= DATE '2025-01-01' AND ordered_at < DATE '2026-01-01'
    GROUP BY EXTRACT(QUARTER FROM ordered_at)
    ORDER BY quarter ASC NULLS LAST
    1 ms10 ms100 msHyper4.5 msDuckDB4.8 msGizmoSQL9.0 msclickhouse_brew13.9 msFirebolt16.1 msDataFusion36.0 msClickHouse10.4 ms
  7. Everything, as recorded

    The agents' own records, as the role play wrote them; only the personal data of the person who ran it is redacted

    Felix's whole session, 11 turns
    • The request, as Felix received it
      Report request from Lena Hartmann (analyst for the Chief Operating Officer):
      
      Title: Purchase order replenishment performance by quarter, 2025 (mature POs only, non-censored)
      Question: Once we only look at purchase orders old enough to have fully played out, are we genuinely behind on restocking, or was the Q4 backlog just a year-end timing artifact?
      Grain: one row per calendar quarter of ordered_at, 2025
      Columns:
      - quarter (e.g. 2025-Q1)
      - pos_ordered_total (all POs with ordered_at in that quarter, for reference)
      - pos_matured (subset of pos_ordered_total with ordered_at <= 2025-11-01, i.e. at least 60 days elapsed before the 2025-12-31 cutoff)
      - pos_matured_received (matured subset with received_at not null)
      - pct_matured_still_open (matured subset with received_at null and status in ('submitted','partially_received') -- a true backlog signal, not a recency artifact)
      - avg_actual_lead_days (DATEDIFF(day, ordered_at, received_at), matured+received subset only)
      - pct_received_late (matured subset with both expected_at and received_at set, where received_at > expected_at)
      - avg_days_late (matured, late subset only)
      Details: Table: purchase_orders. Scope: ordered_at between 2025-01-01 and 2025-12-31, grouped by calendar quarter of ordered_at. The 60-day maturity threshold (ordered_at <= 2025-11-01) is chosen because the longest contracted supplier lead time we've seen is ~43 days, so 60 days gives real buffer for a PO to have resolved one way or the other by the data cutoff; note in the report that Q4's matured subset will only reflect POs ordered in October (since November/December POs aren't yet mature), which is expected and intentional. Round day metrics to 1 decimal, pct columns to 1 decimal. Order by quarter ascending. Expect 4 rows.
      
      Check your query with the warehouse's dry_run tool, then hand it in with submit.
    • Felix calls dry_run
      SELECT
        EXTRACT(QUARTER FROM ordered_at) AS q,
        COUNT(*) AS pos_ordered_total,
        COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01') AS pos_matured,
        AVG(CASE WHEN received_at IS NOT NULL THEN DATEDIFF(day, ordered_at, received_at) END) AS avg_lead
      FROM purchase_orders
      WHERE ordered_at >= DATE '2025-01-01' AND ordered_at < DATE '2026-01-01'
      GROUP BY 1
      ORDER BY 1
    • The warehouse answers
      STATUS: REJECTED
        - does not compile: Binder Error: Referenced column "day" not found in FROM clause!
    • Felix calls dry_run
      SELECT
        EXTRACT(QUARTER FROM ordered_at) AS q,
        COUNT(*) AS pos_ordered_total,
        COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01') AS pos_matured,
        AVG(CASE WHEN received_at IS NOT NULL THEN DATE_DIFF('day', ordered_at, received_at) END) AS avg_lead
      FROM purchase_orders
      WHERE ordered_at >= DATE '2025-01-01' AND ordered_at < DATE '2026-01-01'
      GROUP BY 1
      ORDER BY 1
    • The warehouse answers
      STATUS: COMPILES (dry run on the empty schema, no data read)
      COLUMNS: q, pos_ordered_total, pos_matured, avg_lead
    • Felix calls dry_run
      SELECT
        '2025-Q' || CAST(EXTRACT(QUARTER FROM ordered_at) AS INTEGER) AS quarter,
        COUNT(*) AS pos_ordered_total,
        COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01') AS pos_matured,
        COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01' AND received_at IS NOT NULL) AS pos_matured_received,
        ROUND(100.0 * COUNT(*) FILTER (
            WHERE ordered_at <= DATE '2025-11-01'
              AND received_at IS NULL
              AND status IN ('submitted','partially_received')
          ) / NULLIF(COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01'), 0), 1) AS pct_matured_still_open,
        ROUND(AVG(CASE
            WHEN ordered_at <= DATE '2025-11-01' AND received_at IS NOT NULL
            THEN DATE_DIFF('day', ordered_at, received_at)
          END), 1) AS avg_actual_lead_days,
        ROUND(100.0 * COUNT(*) FILTER (
            WHERE ordered_at <= DATE '2025-11-01'
              AND expected_at IS NOT NULL
              AND received_at IS NOT NULL
              AND received_at > expected_at
          ) / NULLIF(COUNT(*) FILTER (
            WHERE ordered_at <= DATE '2025-11-01'
              AND expected_at IS NOT NULL
              AND received_at IS NOT NULL
          ), 0), 1) AS pct_received_late,
        ROUND(AVG(CASE
            WHEN ordered_at <= DATE '2025-11-01'
              AND expected_at IS NOT NULL
              AND received_at IS NOT NULL
              AND received_at > expected_at
            THEN DATE_DIFF('day', expected_at, received_at)
          END), 1) AS avg_days_late
      FROM purchase_orders
      WHERE ordered_at >= DATE '2025-01-01' AND ordered_at < DATE '2026-01-01'
      GROUP BY EXTRACT(QUARTER FROM ordered_at)
      ORDER BY quarter ASC
    • The warehouse answers
      STATUS: COMPILES (dry run on the empty schema, no data read)
      COLUMNS: quarter, pos_ordered_total, pos_matured, pos_matured_received, pct_matured_still_open, avg_actual_lead_days, pct_received_late, avg_days_late
    • FelixThis matches the requested grain (one row per quarter), has 8 columns within the width limit, and the query compiles cleanly. Submitting now.
    • Felix calls submit
      SELECT
        '2025-Q' || CAST(EXTRACT(QUARTER FROM ordered_at) AS INTEGER) AS quarter,
        COUNT(*) AS pos_ordered_total,
        COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01') AS pos_matured,
        COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01' AND received_at IS NOT NULL) AS pos_matured_received,
        ROUND(100.0 * COUNT(*) FILTER (
            WHERE ordered_at <= DATE '2025-11-01'
              AND received_at IS NULL
              AND status IN ('submitted','partially_received')
          ) / NULLIF(COUNT(*) FILTER (WHERE ordered_at <= DATE '2025-11-01'), 0), 1) AS pct_matured_still_open,
        ROUND(AVG(CASE
            WHEN ordered_at <= DATE '2025-11-01' AND received_at IS NOT NULL
            THEN DATE_DIFF('day', ordered_at, received_at)
          END), 1) AS avg_actual_lead_days,
        ROUND(100.0 * COUNT(*) FILTER (
            WHERE ordered_at <= DATE '2025-11-01'
              AND expected_at IS NOT NULL
              AND received_at IS NOT NULL
              AND received_at > expected_at
          ) / NULLIF(COUNT(*) FILTER (
            WHERE ordered_at <= DATE '2025-11-01'
              AND expected_at IS NOT NULL
              AND received_at IS NOT NULL
          ), 0), 1) AS pct_received_late,
        ROUND(AVG(CASE
            WHEN ordered_at <= DATE '2025-11-01'
              AND expected_at IS NOT NULL
              AND received_at IS NOT NULL
              AND received_at > expected_at
            THEN DATE_DIFF('day', expected_at, received_at)
          END), 1) AS avg_days_late
      FROM purchase_orders
      WHERE ordered_at >= DATE '2025-01-01' AND ordered_at < DATE '2026-01-01'
      GROUP BY EXTRACT(QUARTER FROM ordered_at)
      ORDER BY quarter ASC
    • The warehouse answers
      Submitted to tonight's batch. Your work on this request is done.
    • FelixSubmitted. The report gives one row per 2025 quarter with the requested PO maturity and backlog metrics, using a 60-day maturity cutoff (`ordered_at <= 2025-11-01`) as specified, so Q4's matured figures intentionally reflect only October-ordered POs.