DB Olympics

Queries · q031 · Chief Operating Officer · week 1, night 3

Carrier transit performance, Q4 2025 (complete)

“Across every carrier, how did transit time and reliability hold up in the peak quarter, and how much volume does each carrier actually carry?”

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 SQLGraceWrites tidy, layered CTEs with descriptive names, one step per CTE, then a final select.

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 1

    Subject: Getting my bearings on fulfillment — questions for this week

    Hi Lena,

    Thanks for taking these on. I'm new here and coming from the logistics side, so my first priority is understanding whether the delivery promise we make to shoppers actually holds up — that's the lens for most of what's below. Given the slot budget, here's what matters most, roughly in priority order:

    1. How long does it actually take us to get an order into a customer's hands, and how much does that vary? I want to understand the real end-to-end time from order placed to delivered, and whether that's gotten better, worse, or stayed flat over the past year or so. I'm especially interested in how consistent we are — are there merchants or categories where delivery is reliably fast, and others where it's a coin toss?
    2. How did we do during the last peak/busy season? I'd like to know whether fulfillment speed and reliability held up when volume spiked, or whether we saw things slip — delays, backlogs, stockouts — and how quickly we recovered afterward. This matters because peak is the real stress test of the whole operation.
    3. Where are the weak links — specific carriers, warehouses, or regions? I want to know if there are particular carriers or fulfillment sites that are consistently underperforming on delivery time or dependability, so we know where to focus attention or have a harder conversation with a partner.
    4. How well do we manage stock so we're not promising things we can't deliver? I'm curious about how often we run into stockouts or have to delay orders for lack of inventory, and whether our replenishment from suppliers (purchase orders) is keeping pace — are we chronically waiting too long on restocks?
    5. (If there's room) What does the returns process look like from a logistics standpoint? Specifically, how long it takes for returned goods to get back into our system and become sellable again — that's a cost and customer-experience lever I want to keep an eye on.

    What I'll use this for: I'm forming my first impressions of operational health in this area and want to walk into conversations with merchants and carrier partners armed with facts, not assumptions. I'd rather have a smaller number of really clear answers than a lot of surface-level ones — so if you need to cut scope to fit your slots this week, prioritize #1 and #2, and we can pick up the rest next week.

    Thanks — looking forward to Friday's memo.

    Priya

  2. Lena Hartmann plans the night

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

    Night 2 findings, read critically: (1) Report 1 only printed 50 of 56 carrier-quarter rows -- it's missing the last few carriers' Q3/Q4 figures, so I can't yet see the full carrier picture for peak; I'm fixing that tonight by re-running Q4 only (one quarter -> all ~14 carriers fit on one page) and adding a volume-share column so I can judge materiality, not just rates. (2) Within what I could see, two carriers (Landfall Freight, Lowlands Express) are chronically mediocre all year -- not a peak story, a structural one. But Albion Parcel is the standout: rock-solid in Q1-Q3 (88-97% within 3 days) then collapses in Q4 (avg transit 6.54 days, only 50.7% within 3 days) -- a sharp, carrier-specific peak failure that deserves its own drill-down by month to see exactly when it broke and whether it recovered by year-end. (3) Report 3 (stage breakdown) is a clean, important result: warehouse processing time stayed flat (~1.1-1.2 days) straight through peak even as volume nearly doubled, while carrier transit time is what stretched (2.5-2.9 days baseline -> 3.46 in Nov) -- so the peak slowdown from Night 1 is a carrier/transit problem, not a warehouse-capacity problem. That reframes priority #3: warehouses look fine; carriers are the lever. (4) The warehouse report's worst performers (Thistlecombe, Purrfect Pantry, Chocolaterie Mireille, Kayak Corner, etc.) all have unremarkable processing times, so their poor delivery reliability must come from somewhere in the transit leg -- I want to test whether they're simply the merchants who happen to ship with the weak carriers. Tonight: complete the Q4 carrier picture, isolate Albion Parcel's break, and test the carrier-mix hypothesis for the best/worst merchants.

    Carrier transit performance, Q4 2025 (complete)
    Question
    Across every carrier, how did transit time and reliability hold up in the peak quarter, and how much volume does each carrier actually carry?
    One row per
    carrier, for shipments shipped in Q4 2025
    Columns
    • carrier
    • shipments_shipped (shipped_at between 2025-10-01 and 2025-12-31)
    • shipments_delivered (of those, delivered_at not null as of the 2025-12-31 cutoff)
    • avg_transit_days
    • median_transit_days
    • p90_transit_days
    • pct_within_3_days
    • pct_delivered
    • share_of_q4_shipments_pct (this carrier's shipments_shipped / total Q4 shipments across all carriers, x100)
  3. Grace writes the SQL

    The engineer · 1 dry run on the empty schema, then the submission

    Dry run 1 · STATUS: COMPILES (dry run on the empty schema, no data read)
    WITH q4_shipments AS (
        SELECT
            carrier,
            shipped_at,
            delivered_at,
            CASE WHEN delivered_at IS NOT NULL
                 THEN EXTRACT(EPOCH FROM (delivered_at - shipped_at)) / 86400.0
            END AS transit_days
        FROM shipments
        WHERE shipped_at >= DATE '2025-10-01'
          AND shipped_at < DATE '2026-01-01'
    ),
    carrier_stats AS (
        SELECT
            carrier,
            COUNT(*) AS shipments_shipped,
            COUNT(delivered_at) AS shipments_delivered,
            AVG(transit_days) AS avg_transit_days,
            PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY transit_days) AS median_transit_days,
            PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY transit_days) AS p90_transit_days,
            COUNT(CASE WHEN transit_days <= 3 THEN 1 END) AS delivered_within_3_days
        FROM q4_shipments
        GROUP BY carrier
    ),
    totals AS (
        SELECT SUM(shipments_shipped) AS total_q4_shipments
        FROM carrier_stats
    )
    SELECT
        cs.carrier,
        cs.shipments_shipped,
        cs.shipments_delivered,
        ROUND(cs.avg_transit_days::numeric, 2) AS avg_transit_days,
        ROUND(cs.median_transit_days::numeric, 2) AS median_transit_days,
        ROUND(cs.p90_transit_days::numeric, 2) AS p90_transit_days,
        ROUND(100.0 * cs.delivered_within_3_days / NULLIF(cs.shipments_delivered, 0), 1) AS pct_within_3_days,
        ROUND(100.0 * cs.shipments_delivered / NULLIF(cs.shipments_shipped, 0), 1) AS pct_delivered,
        ROUND(100.0 * cs.shipments_shipped / NULLIF(t.total_q4_shipments, 0), 1) AS share_of_q4_shipments_pct
    FROM carrier_stats cs
    CROSS JOIN totals t
    ORDER BY cs.shipments_shipped DESC, cs.carrier ASC

    “Query compiles and returns exactly the requested columns at the right grain. Submitting.”

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

    “Submitted. Summary of what was delivered: - Grain: one row per carrier, for shipments with shipped_at in Q4 2025 (2025-10-01 through 2025-12-31 inclusive). - Metrics: shipments_shipped, shipments_delivered, avg/median/p90 transit days (fractional-day precision, rounded to 2 decimals), pct_within_3_days and pct_delivered (rounded to 1 decimal, % scale), and each carrier's share of total Q4 shipment volume. - Ordering: shipments_shipped descending, carrier ascending as tie-break — no row cap applied, so all ~14 carriers will print. - Caveat flagged in the note: late-December shipments may still be in transit as of the 2025-12-31 cutoff, so pct_delivered/pct_within_3_days can understate carriers with heavy late-month volume (right-censoring), not necessarily worse service.”

  4. The report comes back overnight

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

    carrier              | shipments_shipped | shipments_delivered | avg_transit_days | median_transit_days | p90_transit_days | pct_within_3_days | pct_delivered | share_of_q4_shipments_pct
    ---------------------+-------------------+---------------------+------------------+---------------------+------------------+-------------------+---------------+--------------------------
    Strombogen Kurier    |               905 |                 877 |             2.14 |                2.04 |             3.23 |              80.7 |          96.9 |                      12.3
    Liberty Ground       |               854 |                 830 |             2.29 |                2.08 |             3.76 |              78.2 |          97.2 |                      11.6
    Lowlands Express     |               853 |                 816 |             3.70 |                3.16 |             6.18 |              44.6 |          95.7 |                      11.6
    Albion Parcel        |               794 |                 779 |             6.57 |                3.30 |            17.10 |              43.4 |          98.1 |                      10.8
    Trailhead Express    |               706 |                 687 |             2.41 |                2.09 |             3.92 |              73.2 |          97.3 |                       9.6
    Landfall Freight     |               676 |                 648 |             5.15 |                4.90 |             8.01 |              13.6 |          95.9 |                       9.2
    Alpenpaket           |               628 |                 620 |             2.19 |                2.09 |             3.25 |              79.4 |          98.7 |                       8.5
    Kennet Freight       |               453 |                 442 |             2.04 |                2.01 |             3.10 |              86.9 |          97.6 |                       6.2
    Skerrylane Logistics |               401 |                 393 |             2.67 |                2.22 |             4.34 |              66.7 |          98.0 |                       5.5
    Polder Post          |               320 |                 310 |             2.51 |                2.17 |             4.21 |              71.6 |          96.9 |                       4.3
    Prairie Line Express |               291 |                 284 |             2.57 |                2.28 |             3.99 |              67.6 |          97.6 |                       4.0
    Regent Parcels       |               277 |                 268 |             2.20 |                2.08 |             3.29 |              81.0 |          96.8 |                       3.8
    Gallia Colis         |               109 |                 105 |             2.80 |                2.09 |             5.12 |              68.6 |          96.3 |                       1.5
    Liffey Couriers      |                90 |                  89 |             2.48 |                2.11 |             4.10 |              66.3 |          98.9 |                       1.2
    (14 rows)
  5. What came of it

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

    Later queries that build on this report:

    It builds on:

  6. It becomes benchmark query q031

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

    • spellingcast (3×)
    • numericsdocs/NUMERICS.md: rounding removed; results keep full precision; bare NUMERIC cast that only served the rounding removed
    • numericsdocs/NUMERICS.md: EXTRACT(EPOCH FROM (b - a)) cast to NUMERIC(18,6)
    • made explicita selected t.col gets AS col, its standard name made explicit (3×)
    • placeholderpercentile cont (2×)
    • placeholderseconds between
    • determinismthe standard leaves the place of NULLs in a sort to the implementation: NULLS LAST ascending, NULLS FIRST descending, as in the reference results (2×)
    The benchmark text, against the submitted SQL
    WITH q4_shipments AS (    SELECT        carrier,        shipped_at,        delivered_at,        CASE WHEN delivered_at IS NOT NULL             THEN EXTRACT(EPOCH FROM (delivered_at - shipped_at)) / 86400.0             THEN {{seconds_between(shipped_at, delivered_at)}} / 86400.0        END AS transit_days    FROM shipments    WHERE shipped_at >= DATE '2025-10-01'      AND shipped_at < DATE '2026-01-01'),carrier_stats AS (    SELECT        carrier,        COUNT(*) AS shipments_shipped,        COUNT(delivered_at) AS shipments_delivered,        AVG(transit_days) AS avg_transit_days,        PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY transit_days) AS median_transit_days,        PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY transit_days) AS p90_transit_days,        {{percentile_cont(0.5, transit_days)}} AS median_transit_days,        {{percentile_cont(0.9, transit_days)}} AS p90_transit_days,        COUNT(CASE WHEN transit_days <= 3 THEN 1 END) AS delivered_within_3_days    FROM q4_shipments    GROUP BY carrier),totals AS (    SELECT SUM(shipments_shipped) AS total_q4_shipments    FROM carrier_stats)SELECT    cs.carrier,    cs.shipments_shipped,    cs.shipments_delivered,    ROUND(cs.avg_transit_days::numeric, 2) AS avg_transit_days,    ROUND(cs.median_transit_days::numeric, 2) AS median_transit_days,    ROUND(cs.p90_transit_days::numeric, 2) AS p90_transit_days,    ROUND(100.0 * cs.delivered_within_3_days / NULLIF(cs.shipments_delivered, 0), 1) AS pct_within_3_days,    ROUND(100.0 * cs.shipments_delivered / NULLIF(cs.shipments_shipped, 0), 1) AS pct_delivered,    ROUND(100.0 * cs.shipments_shipped / NULLIF(t.total_q4_shipments, 0), 1) AS share_of_q4_shipments_pct    cs.carrier AS carrier,    cs.shipments_shipped AS shipments_shipped,    cs.shipments_delivered AS shipments_delivered,    cs.avg_transit_days AS avg_transit_days,    cs.median_transit_days AS median_transit_days,    cs.p90_transit_days AS p90_transit_days,    100.0 * cs.delivered_within_3_days / NULLIF(cs.shipments_delivered, 0) AS pct_within_3_days,    100.0 * cs.shipments_delivered / NULLIF(cs.shipments_shipped, 0) AS pct_delivered,    100.0 * cs.shipments_shipped / NULLIF(t.total_q4_shipments, 0) AS share_of_q4_shipments_pctFROM carrier_stats csCROSS JOIN totals tORDER BY cs.shipments_shipped DESC, cs.carrier ASCORDER BY cs.shipments_shipped DESC NULLS FIRST, cs.carrier ASC NULLS LAST
    WITH q4_shipments AS (
        SELECT
            carrier,
            shipped_at,
            delivered_at,
            CASE WHEN delivered_at IS NOT NULL
                 THEN CAST(EXTRACT(EPOCH FROM (delivered_at - shipped_at)) AS NUMERIC(18,6)) / 86400.0
            END AS transit_days
        FROM shipments
        WHERE shipped_at >= DATE '2025-10-01'
          AND shipped_at < DATE '2026-01-01'
    ),
    carrier_stats AS (
        SELECT
            carrier,
            COUNT(*) AS shipments_shipped,
            COUNT(delivered_at) AS shipments_delivered,
            AVG(transit_days) AS avg_transit_days,
            PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY transit_days) AS median_transit_days,
            PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY transit_days) AS p90_transit_days,
            COUNT(CASE WHEN transit_days <= 3 THEN 1 END) AS delivered_within_3_days
        FROM q4_shipments
        GROUP BY carrier
    ),
    totals AS (
        SELECT SUM(shipments_shipped) AS total_q4_shipments
        FROM carrier_stats
    )
    SELECT
        cs.carrier AS carrier,
        cs.shipments_shipped AS shipments_shipped,
        cs.shipments_delivered AS shipments_delivered,
        cs.avg_transit_days AS avg_transit_days,
        cs.median_transit_days AS median_transit_days,
        cs.p90_transit_days AS p90_transit_days,
        100.0 * cs.delivered_within_3_days / NULLIF(cs.shipments_delivered, 0) AS pct_within_3_days,
        100.0 * cs.shipments_delivered / NULLIF(cs.shipments_shipped, 0) AS pct_delivered,
        100.0 * cs.shipments_shipped / NULLIF(t.total_q4_shipments, 0) AS share_of_q4_shipments_pct
    FROM carrier_stats cs
    CROSS JOIN totals t
    ORDER BY cs.shipments_shipped DESC NULLS FIRST, cs.carrier ASC NULLS LAST
    10 ms100 ms1 sHyper90.7 msDuckDB144 msGizmoSQL120 msclickhouse_brew119 msFirebolt125 msDataFusion117 msClickHouse523 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

    Grace's whole session, 7 turns
    • The request, as Grace received it
      Report request from Lena Hartmann (analyst for the Chief Operating Officer):
      
      Title: Carrier transit performance, Q4 2025 (complete)
      Question: Across every carrier, how did transit time and reliability hold up in the peak quarter, and how much volume does each carrier actually carry?
      Grain: one row per carrier, for shipments shipped in Q4 2025
      Columns:
      - carrier
      - shipments_shipped (shipped_at between 2025-10-01 and 2025-12-31)
      - shipments_delivered (of those, delivered_at not null as of the 2025-12-31 cutoff)
      - avg_transit_days
      - median_transit_days
      - p90_transit_days
      - pct_within_3_days
      - pct_delivered
      - share_of_q4_shipments_pct (this carrier's shipments_shipped / total Q4 shipments across all carriers, x100)
      Details: Table: shipments (carrier, shipped_at, delivered_at). Scope: shipped_at >= 2025-10-01 and <= 2025-12-31, all carriers (no row cap needed -- expect ~14 distinct carriers based on last night's report, so please return every carrier, do not silently truncate). transit_days = DATEDIFF(day, shipped_at, delivered_at), computed only where delivered_at is not null. Round day metrics to 2 decimals, pct columns to 1 decimal (as %). As before, flag that pct_delivered/pct_within_3_days for shipments shipped very late in December may be understated due to the 2025-12-31 cutoff (right-censoring), not necessarily a carrier failure. Order by shipments_shipped descending, tie-break by carrier ascending.
      
      Check your query with the warehouse's dry_run tool, then hand it in with submit.
    • Grace calls dry_run
      WITH q4_shipments AS (
          SELECT
              carrier,
              shipped_at,
              delivered_at,
              CASE WHEN delivered_at IS NOT NULL
                   THEN EXTRACT(EPOCH FROM (delivered_at - shipped_at)) / 86400.0
              END AS transit_days
          FROM shipments
          WHERE shipped_at >= DATE '2025-10-01'
            AND shipped_at < DATE '2026-01-01'
      ),
      carrier_stats AS (
          SELECT
              carrier,
              COUNT(*) AS shipments_shipped,
              COUNT(delivered_at) AS shipments_delivered,
              AVG(transit_days) AS avg_transit_days,
              PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY transit_days) AS median_transit_days,
              PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY transit_days) AS p90_transit_days,
              COUNT(CASE WHEN transit_days <= 3 THEN 1 END) AS delivered_within_3_days
          FROM q4_shipments
          GROUP BY carrier
      ),
      totals AS (
          SELECT SUM(shipments_shipped) AS total_q4_shipments
          FROM carrier_stats
      )
      SELECT
          cs.carrier,
          cs.shipments_shipped,
          cs.shipments_delivered,
          ROUND(cs.avg_transit_days::numeric, 2) AS avg_transit_days,
          ROUND(cs.median_transit_days::numeric, 2) AS median_transit_days,
          ROUND(cs.p90_transit_days::numeric, 2) AS p90_transit_days,
          ROUND(100.0 * cs.delivered_within_3_days / NULLIF(cs.shipments_delivered, 0), 1) AS pct_within_3_days,
          ROUND(100.0 * cs.shipments_delivered / NULLIF(cs.shipments_shipped, 0), 1) AS pct_delivered,
          ROUND(100.0 * cs.shipments_shipped / NULLIF(t.total_q4_shipments, 0), 1) AS share_of_q4_shipments_pct
      FROM carrier_stats cs
      CROSS JOIN totals t
      ORDER BY cs.shipments_shipped DESC, cs.carrier ASC
    • The warehouse answers
      STATUS: COMPILES (dry run on the empty schema, no data read)
      COLUMNS: carrier, shipments_shipped, shipments_delivered, avg_transit_days, median_transit_days, p90_transit_days, pct_within_3_days, pct_delivered, share_of_q4_shipments_pct
    • GraceQuery compiles and returns exactly the requested columns at the right grain. Submitting.
    • Grace calls submit
      WITH q4_shipments AS (
          SELECT
              carrier,
              shipped_at,
              delivered_at,
              CASE WHEN delivered_at IS NOT NULL
                   THEN EXTRACT(EPOCH FROM (delivered_at - shipped_at)) / 86400.0
              END AS transit_days
          FROM shipments
          WHERE shipped_at >= DATE '2025-10-01'
            AND shipped_at < DATE '2026-01-01'
      ),
      carrier_stats AS (
          SELECT
              carrier,
              COUNT(*) AS shipments_shipped,
              COUNT(delivered_at) AS shipments_delivered,
              AVG(transit_days) AS avg_transit_days,
              PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY transit_days) AS median_transit_days,
              PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY transit_days) AS p90_transit_days,
              COUNT(CASE WHEN transit_days <= 3 THEN 1 END) AS delivered_within_3_days
          FROM q4_shipments
          GROUP BY carrier
      ),
      totals AS (
          SELECT SUM(shipments_shipped) AS total_q4_shipments
          FROM carrier_stats
      )
      SELECT
          cs.carrier,
          cs.shipments_shipped,
          cs.shipments_delivered,
          ROUND(cs.avg_transit_days::numeric, 2) AS avg_transit_days,
          ROUND(cs.median_transit_days::numeric, 2) AS median_transit_days,
          ROUND(cs.p90_transit_days::numeric, 2) AS p90_transit_days,
          ROUND(100.0 * cs.delivered_within_3_days / NULLIF(cs.shipments_delivered, 0), 1) AS pct_within_3_days,
          ROUND(100.0 * cs.shipments_delivered / NULLIF(cs.shipments_shipped, 0), 1) AS pct_delivered,
          ROUND(100.0 * cs.shipments_shipped / NULLIF(t.total_q4_shipments, 0), 1) AS share_of_q4_shipments_pct
      FROM carrier_stats cs
      CROSS JOIN totals t
      ORDER BY cs.shipments_shipped DESC, cs.carrier ASC
    • The warehouse answers
      Submitted to tonight's batch. Your work on this request is done.
    • GraceSubmitted. Summary of what was delivered: - **Grain:** one row per carrier, for shipments with `shipped_at` in Q4 2025 (2025-10-01 through 2025-12-31 inclusive). - **Metrics:** shipments_shipped, shipments_delivered, avg/median/p90 transit days (fractional-day precision, rounded to 2 decimals), pct_within_3_days and pct_delivered (rounded to 1 decimal, % scale), and each carrier's share of total Q4 shipment volume. - **Ordering:** shipments_shipped descending, carrier ascending as tie-break — no row cap applied, so all ~14 carriers will print. - **Caveat flagged in the note:** late-December shipments may still be in transit as of the 2025-12-31 cutoff, so pct_delivered/pct_within_3_days can understate carriers with heavy late-month volume (right-censoring), not necessarily worse service.