DB Olympics

Queries · q027 · Chief Operating Officer · week 1, night 1

Peak season weekly fulfillment performance (Oct-Dec 2025)

“Did delivery speed and reliability hold up during the most recent peak season, and did any backlog build up or clear by year-end?”

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 SQLInesThinks in window functions: ranks, running totals, LAG/LEAD for comparisons, percent-of-total.

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 1, request 3 of 3

    Priya's top two priorities are (1) end-to-end delivery speed and its consistency/trend, and (2) how the last peak season held up. I'm spending all 3 slots tonight on those, leaving carriers/warehouses, inventory and returns for later nights once I've seen where the baseline numbers point. I'm defining "delivered" as order status = fulfilled with all linked shipments having a non-null delivered_at, and delivery time as placed_at -> last shipment delivered_at. Report 1 gives the 24-month trend + variability (median/p90/SLA-hit-rate) so I can see direction of travel and spread in one page. Report 2 cuts the same metrics by merchant for 2025 to see who is reliably fast vs. a coin toss. Report 3 looks at the most recent peak window (Oct-Dec 2025) weekly to see if speed degraded under volume and whether a backlog built up and cleared by year-end. I'll sanity-check all three against each other (e.g. does the peak-season weekly average line up with the Nov-Dec rows in the monthly trend) before going further.

    Peak season weekly fulfillment performance (Oct-Dec 2025)
    Question
    Did delivery speed and reliability hold up during the most recent peak season, and did any backlog build up or clear by year-end?
    One row per
    ISO week (week_start), based on order placement week
    Columns
    • week_start
    • orders_placed (all orders placed that week, any status)
    • orders_delivered (of those, count fully delivered by 2025-12-31)
    • avg_delivery_days (for the fully-delivered subset)
    • pct_within_5_days (share of fully-delivered orders in that week where delivery_days <= 5)
    • orders_still_undelivered (placed that week, status not 'fulfilled' or missing a delivered_at as of 2025-12-31 cutoff)
  3. Ines 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 base as (
      select
        o.id as order_id,
        o.placed_at,
        o.status,
        cd.week_start as week_start
      from orders o
      join calendar_days cd on cd.calendar_date = o.placed_at::date
      where o.placed_at >= timestamp '2025-10-01'
        and o.placed_at < timestamp '2026-01-01'
    ),
    fulfillment_agg as (
      select
        f.order_id,
        count(s.id) as shipment_count,
        count(s.delivered_at) as delivered_shipment_count,
        max(s.delivered_at) as max_delivered_at
      from fulfillments f
      join shipments s on s.fulfillment_id = f.id
      group by f.order_id
    ),
    combined as (
      select
        b.order_id,
        b.placed_at,
        b.status,
        b.week_start,
        case
          when fa.shipment_count is not null and fa.shipment_count > 0
               and fa.shipment_count = fa.delivered_shipment_count
          then true else false
        end as is_fully_delivered,
        case
          when fa.shipment_count is not null and fa.shipment_count > 0
               and fa.shipment_count = fa.delivered_shipment_count
          then extract(epoch from (fa.max_delivered_at - b.placed_at)) / 86400.0
          else null
        end as delivery_days
      from base b
      left join fulfillment_agg fa on fa.order_id = b.order_id
    )
    select
      week_start,
      count(*) as orders_placed,
      count(*) filter (where is_fully_delivered) as orders_delivered,
      round(avg(delivery_days) filter (where is_fully_delivered), 2) as avg_delivery_days,
      round(
        100.0 * count(*) filter (where is_fully_delivered and delivery_days <= 5)
        / nullif(count(*) filter (where is_fully_delivered), 0),
        1
      ) as pct_within_5_days,
      count(*) filter (where status <> 'fulfilled' or not is_fully_delivered) as orders_still_undelivered
    from combined
    group by week_start
    order by week_start asc

    “Looks good — compiles cleanly with the expected 6 columns. 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 the report does: - Grain: one row per ISO week (week_start), based on order placement week, for orders placed 2025-10-01 through 2025-12-31 (~13 rows expected). - orders_placed: all orders placed that week, any status. - orders_delivered / avg_delivery_days / pct_within_5_days: computed over the subset where every shipment linked to the order (via fulfillments) has a delivered_at, with delivery_days = (max shipment delivered_at − placed_at) in days. - orders_still_undelivered: orders placed that week where status ≠ fulfilled, or status is fulfilled but delivery couldn't be fully resolved by the 2025-12-31 cutoff — this will mechanically trend upward in the last couple of weeks due to transit-time right-censoring, as flagged in the note to the analyst. Two things worth knowing: the report pools all tenants together (no merchant filter, since the question is about overall ops, not specific merchants), and orders_still_undelivered is intentionally literal per the spec, so it can flag orders that are already physically delivered but not yet status-flagged fulfilled.”

  4. The report comes back overnight

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

    week_start | orders_placed | orders_delivered | avg_delivery_days | pct_within_5_days | orders_still_undelivered
    -----------+---------------+------------------+-------------------+-------------------+-------------------------
    2025-09-29 |           279 |              271 |               4.5 |              72.7 |                       19
    2025-10-06 |           397 |              383 |              3.94 |              79.4 |                       25
    2025-10-13 |           423 |              414 |              3.92 |              80.7 |                       25
    2025-10-20 |           448 |              440 |              4.31 |              73.9 |                       21
    2025-10-27 |           441 |              427 |              3.91 |              82.4 |                       27
    2025-11-03 |           480 |              466 |              3.92 |              80.5 |                       41
    2025-11-10 |           470 |              452 |              4.73 |              64.8 |                       35
    2025-11-17 |           576 |              559 |              4.29 |              73.3 |                       41
    2025-11-24 |          1010 |              966 |              5.26 |              73.5 |                      104
    2025-12-01 |           969 |              928 |              5.73 |              56.1 |                       87
    2025-12-08 |           635 |              611 |              3.96 |              80.7 |                       34
    2025-12-15 |           660 |              628 |              3.43 |              87.9 |                       38
    2025-12-22 |           577 |              510 |               3.1 |              92.5 |                       67
    2025-12-29 |           271 |               38 |              1.87 |             100.0 |                      233
    (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:

  6. It becomes benchmark query q027

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

    • spellingcast
    • numericsdocs/NUMERICS.md: rounding removed; results keep full precision
    • 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 (7×)
    • 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
    The benchmark text, against the submitted SQL
    with base as (  select    o.id as order_id,    o.placed_at,    o.status,    o.placed_at AS placed_at,    o.status AS status,    cd.week_start as week_start  from orders o  join calendar_days cd on cd.calendar_date = o.placed_at::date  join calendar_days cd on cd.calendar_date = CAST(o.placed_at AS date)  where o.placed_at >= timestamp '2025-10-01'    and o.placed_at < timestamp '2026-01-01'),fulfillment_agg as (  select    f.order_id,    f.order_id AS order_id,    count(s.id) as shipment_count,    count(s.delivered_at) as delivered_shipment_count,    max(s.delivered_at) as max_delivered_at  from fulfillments f  join shipments s on s.fulfillment_id = f.id  group by f.order_id),combined as (  select    b.order_id,    b.placed_at,    b.status,    b.week_start,    b.order_id AS order_id,    b.placed_at AS placed_at,    b.status AS status,    b.week_start AS week_start,    case      when fa.shipment_count is not null and fa.shipment_count > 0           and fa.shipment_count = fa.delivered_shipment_count      then true else false    end as is_fully_delivered,    case      when fa.shipment_count is not null and fa.shipment_count > 0           and fa.shipment_count = fa.delivered_shipment_count      then extract(epoch from (fa.max_delivered_at - b.placed_at)) / 86400.0      then {{seconds_between(b.placed_at, fa.max_delivered_at)}} / 86400.0      else null    end as delivery_days  from base b  left join fulfillment_agg fa on fa.order_id = b.order_id)select  week_start,  count(*) as orders_placed,  count(*) filter (where is_fully_delivered) as orders_delivered,  round(avg(delivery_days) filter (where is_fully_delivered), 2) as avg_delivery_days,  round(    100.0 * count(*) filter (where is_fully_delivered and delivery_days <= 5)    / nullif(count(*) filter (where is_fully_delivered), 0),    1  ) as pct_within_5_days,  avg(delivery_days) filter (where is_fully_delivered) as avg_delivery_days,  100.0 * count(*) filter (where is_fully_delivered and delivery_days <= 5)  / nullif(count(*) filter (where is_fully_delivered), 0) as pct_within_5_days,  count(*) filter (where status <> 'fulfilled' or not is_fully_delivered) as orders_still_undeliveredfrom combinedgroup by week_startorder by week_start ascorder by week_start asc NULLS LAST
    with base as (
      select
        o.id as order_id,
        o.placed_at AS placed_at,
        o.status AS status,
        cd.week_start as week_start
      from orders o
      join calendar_days cd on cd.calendar_date = CAST(o.placed_at AS date)
      where o.placed_at >= timestamp '2025-10-01'
        and o.placed_at < timestamp '2026-01-01'
    ),
    fulfillment_agg as (
      select
        f.order_id AS order_id,
        count(s.id) as shipment_count,
        count(s.delivered_at) as delivered_shipment_count,
        max(s.delivered_at) as max_delivered_at
      from fulfillments f
      join shipments s on s.fulfillment_id = f.id
      group by f.order_id
    ),
    combined as (
      select
        b.order_id AS order_id,
        b.placed_at AS placed_at,
        b.status AS status,
        b.week_start AS week_start,
        case
          when fa.shipment_count is not null and fa.shipment_count > 0
               and fa.shipment_count = fa.delivered_shipment_count
          then true else false
        end as is_fully_delivered,
        case
          when fa.shipment_count is not null and fa.shipment_count > 0
               and fa.shipment_count = fa.delivered_shipment_count
          then CAST(EXTRACT(EPOCH FROM (fa.max_delivered_at - b.placed_at)) AS NUMERIC(18,6)) / 86400.0
          else null
        end as delivery_days
      from base b
      left join fulfillment_agg fa on fa.order_id = b.order_id
    )
    select
      week_start,
      count(*) as orders_placed,
      count(*) filter (where is_fully_delivered) as orders_delivered,
      avg(delivery_days) filter (where is_fully_delivered) as avg_delivery_days,
      100.0 * count(*) filter (where is_fully_delivered and delivery_days <= 5)
      / nullif(count(*) filter (where is_fully_delivered), 0) as pct_within_5_days,
      count(*) filter (where status <> 'fulfilled' or not is_fully_delivered) as orders_still_undelivered
    from combined
    group by week_start
    order by week_start asc NULLS LAST
    100 ms1 s10 sHyper633 msDuckDB509 msGizmoSQL403 msclickhouse_brew1.19 sFirebolt581 msDataFusion632 msClickHouse1.15 s
  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

    Ines's whole session, 7 turns
    • The request, as Ines received it
      Report request from Lena Hartmann (analyst for the Chief Operating Officer):
      
      Title: Peak season weekly fulfillment performance (Oct-Dec 2025)
      Question: Did delivery speed and reliability hold up during the most recent peak season, and did any backlog build up or clear by year-end?
      Grain: one row per ISO week (week_start), based on order placement week
      Columns:
      - week_start
      - orders_placed (all orders placed that week, any status)
      - orders_delivered (of those, count fully delivered by 2025-12-31)
      - avg_delivery_days (for the fully-delivered subset)
      - pct_within_5_days (share of fully-delivered orders in that week where delivery_days <= 5)
      - orders_still_undelivered (placed that week, status not 'fulfilled' or missing a delivered_at as of 2025-12-31 cutoff)
      Details: Grain: one row per week_start (Monday, from calendar_days.week_start) for orders.placed_at between 2025-10-01 and 2025-12-31 inclusive. orders_placed = count of all orders placed in that week regardless of status. delivery_days defined exactly as in the monthly trend report (placed_at to max linked shipment.delivered_at via fulfillments, only counted when all shipments for that order are delivered). orders_still_undelivered = orders placed in that week whose status is not 'fulfilled', or status is 'fulfilled' but no delivered_at could be resolved by the 2025-12-31 data cutoff -- this flags backlog/slippage, understanding the most recent weeks will naturally show more undelivered due to right-censoring (transit time), which is expected and should just be noted in the report, not treated as a defect. Order by week_start ascending. Expect about 13 rows.
      
      Check your query with the warehouse's dry_run tool, then hand it in with submit.
    • Ines calls dry_run
      with base as (
        select
          o.id as order_id,
          o.placed_at,
          o.status,
          cd.week_start as week_start
        from orders o
        join calendar_days cd on cd.calendar_date = o.placed_at::date
        where o.placed_at >= timestamp '2025-10-01'
          and o.placed_at < timestamp '2026-01-01'
      ),
      fulfillment_agg as (
        select
          f.order_id,
          count(s.id) as shipment_count,
          count(s.delivered_at) as delivered_shipment_count,
          max(s.delivered_at) as max_delivered_at
        from fulfillments f
        join shipments s on s.fulfillment_id = f.id
        group by f.order_id
      ),
      combined as (
        select
          b.order_id,
          b.placed_at,
          b.status,
          b.week_start,
          case
            when fa.shipment_count is not null and fa.shipment_count > 0
                 and fa.shipment_count = fa.delivered_shipment_count
            then true else false
          end as is_fully_delivered,
          case
            when fa.shipment_count is not null and fa.shipment_count > 0
                 and fa.shipment_count = fa.delivered_shipment_count
            then extract(epoch from (fa.max_delivered_at - b.placed_at)) / 86400.0
            else null
          end as delivery_days
        from base b
        left join fulfillment_agg fa on fa.order_id = b.order_id
      )
      select
        week_start,
        count(*) as orders_placed,
        count(*) filter (where is_fully_delivered) as orders_delivered,
        round(avg(delivery_days) filter (where is_fully_delivered), 2) as avg_delivery_days,
        round(
          100.0 * count(*) filter (where is_fully_delivered and delivery_days <= 5)
          / nullif(count(*) filter (where is_fully_delivered), 0),
          1
        ) as pct_within_5_days,
        count(*) filter (where status <> 'fulfilled' or not is_fully_delivered) as orders_still_undelivered
      from combined
      group by week_start
      order by week_start asc
    • The warehouse answers
      STATUS: COMPILES (dry run on the empty schema, no data read)
      COLUMNS: week_start, orders_placed, orders_delivered, avg_delivery_days, pct_within_5_days, orders_still_undelivered
    • InesLooks good — compiles cleanly with the expected 6 columns. Submitting.
    • Ines calls submit
      with base as (
        select
          o.id as order_id,
          o.placed_at,
          o.status,
          cd.week_start as week_start
        from orders o
        join calendar_days cd on cd.calendar_date = o.placed_at::date
        where o.placed_at >= timestamp '2025-10-01'
          and o.placed_at < timestamp '2026-01-01'
      ),
      fulfillment_agg as (
        select
          f.order_id,
          count(s.id) as shipment_count,
          count(s.delivered_at) as delivered_shipment_count,
          max(s.delivered_at) as max_delivered_at
        from fulfillments f
        join shipments s on s.fulfillment_id = f.id
        group by f.order_id
      ),
      combined as (
        select
          b.order_id,
          b.placed_at,
          b.status,
          b.week_start,
          case
            when fa.shipment_count is not null and fa.shipment_count > 0
                 and fa.shipment_count = fa.delivered_shipment_count
            then true else false
          end as is_fully_delivered,
          case
            when fa.shipment_count is not null and fa.shipment_count > 0
                 and fa.shipment_count = fa.delivered_shipment_count
            then extract(epoch from (fa.max_delivered_at - b.placed_at)) / 86400.0
            else null
          end as delivery_days
        from base b
        left join fulfillment_agg fa on fa.order_id = b.order_id
      )
      select
        week_start,
        count(*) as orders_placed,
        count(*) filter (where is_fully_delivered) as orders_delivered,
        round(avg(delivery_days) filter (where is_fully_delivered), 2) as avg_delivery_days,
        round(
          100.0 * count(*) filter (where is_fully_delivered and delivery_days <= 5)
          / nullif(count(*) filter (where is_fully_delivered), 0),
          1
        ) as pct_within_5_days,
        count(*) filter (where status <> 'fulfilled' or not is_fully_delivered) as orders_still_undelivered
      from combined
      group by week_start
      order by week_start asc
    • The warehouse answers
      Submitted to tonight's batch. Your work on this request is done.
    • InesSubmitted. Summary of what the report does: - **Grain:** one row per ISO week (`week_start`), based on order placement week, for orders placed 2025-10-01 through 2025-12-31 (~13 rows expected). - **orders_placed:** all orders placed that week, any status. - **orders_delivered / avg_delivery_days / pct_within_5_days:** computed over the subset where every shipment linked to the order (via fulfillments) has a `delivered_at`, with `delivery_days` = (max shipment `delivered_at` − `placed_at`) in days. - **orders_still_undelivered:** orders placed that week where status ≠ `fulfilled`, or status is `fulfilled` but delivery couldn't be fully resolved by the 2025-12-31 cutoff — this will mechanically trend upward in the last couple of weeks due to transit-time right-censoring, as flagged in the note to the analyst. Two things worth knowing: the report pools all tenants together (no merchant filter, since the question is about overall ops, not specific merchants), and `orders_still_undelivered` is intentionally literal per the spec, so it can flag orders that are already physically delivered but not yet status-flagged `fulfilled`.