DB Olympics

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

Delivery time trend, monthly (2024-2025)

“Over the last two years, how long does it take from order placed to delivered, and how much does that vary month to month?”

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 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 1 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.

    Delivery time trend, monthly (2024-2025)
    Question
    Over the last two years, how long does it take from order placed to delivered, and how much does that vary month to month?
    One row per
    calendar month of order placement
    Columns
    • month (YYYY-MM)
    • orders_delivered (count of orders fully delivered)
    • avg_delivery_days (mean, placed_at to final delivered_at)
    • median_delivery_days (p50)
    • p90_delivery_days
    • pct_within_5_days (share of delivered orders where delivery_days <= 5)
  3. Felix 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)
    select cd.month_label as month,
           count(*) as orders_delivered,
           round(avg(dd.delivery_days)::numeric, 1) as avg_delivery_days,
           round(cast(percentile_cont(0.5) within group (order by dd.delivery_days) as numeric), 1) as median_delivery_days,
           round(cast(percentile_cont(0.9) within group (order by dd.delivery_days) as numeric), 1) as p90_delivery_days,
           round(100.0 * sum(case when dd.delivery_days <= 5 then 1 else 0 end) / count(*), 1) as pct_within_5_days
    from (
      select o.id as order_id,
             o.placed_at,
             (cast(
                (select max(s.delivered_at)
                 from fulfillments f
                 join shipments s on s.fulfillment_id = f.id
                 where f.order_id = o.id) as date)
              - cast(o.placed_at as date)) as delivery_days
      from orders o
      where o.status = 'fulfilled'
        and o.placed_at >= timestamp '2024-01-01'
        and o.placed_at < timestamp '2026-01-01'
        and exists (
          select 1 from fulfillments f join shipments s on s.fulfillment_id = f.id where f.order_id = o.id
        )
        and not exists (
          select 1 from fulfillments f join shipments s on s.fulfillment_id = f.id where f.order_id = o.id and s.delivered_at is null
        )
    ) dd
    join calendar_days cd on cd.calendar_date = cast(dd.placed_at as date)
    group by cd.month_label
    order by cd.month_label

    “Query compiles with the expected columns. Submitting.”

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

    “Submitted. The report computes, per calendar month of placed_at (2024-01 through 2025-12), the count of fully-delivered fulfilled orders along with mean, median, p90 delivery days and the share delivered within 5 days — using calendar-day differences between order placement and the latest shipment delivery, restricted to orders where every linked shipment has a delivered_at.”

  4. The report comes back overnight

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

    month   | orders_delivered | avg_delivery_days | median_delivery_days | p90_delivery_days | pct_within_5_days
    --------+------------------+-------------------+----------------------+-------------------+------------------
    2024-01 |             1234 |               3.5 |                  3.0 |               6.0 |              89.6
    2024-02 |             1061 |               3.8 |                  3.0 |               6.0 |              86.1
    2024-03 |             1139 |               3.8 |                  3.0 |               6.0 |              86.9
    2024-04 |             1154 |               4.0 |                  4.0 |               6.0 |              83.4
    2024-05 |             1275 |               4.2 |                  4.0 |               6.0 |              79.1
    2024-06 |             1223 |               4.1 |                  4.0 |               6.0 |              83.7
    2024-07 |             1292 |               3.9 |                  4.0 |               6.0 |              84.9
    2024-08 |             1233 |               4.0 |                  4.0 |               6.0 |              83.4
    2024-09 |             1281 |               3.9 |                  3.0 |               6.0 |              85.0
    2024-10 |             1422 |               4.0 |                  4.0 |               6.0 |              82.6
    2024-11 |             1961 |               3.9 |                  4.0 |               6.0 |              84.8
    2024-12 |             2432 |               4.3 |                  4.0 |               7.0 |              77.9
    2025-01 |             1717 |               3.6 |                  3.0 |               5.0 |              90.3
    2025-02 |             1329 |               3.7 |                  3.0 |               6.0 |              89.4
    2025-03 |             1545 |               3.8 |                  4.0 |               6.0 |              87.1
    2025-04 |             1477 |               3.7 |                  3.0 |               6.0 |              88.8
    2025-05 |             1574 |               4.3 |                  4.0 |               7.0 |              80.6
    2025-06 |             1419 |               3.8 |                  3.0 |               6.0 |              86.3
    2025-07 |             1565 |               3.9 |                  4.0 |               6.0 |              85.2
    2025-08 |             1457 |               3.9 |                  4.0 |               6.0 |              87.2
    2025-09 |             1611 |               4.1 |                  4.0 |               6.0 |              83.2
    2025-10 |             1767 |               4.1 |                  4.0 |               6.0 |              83.1
    2025-11 |             2419 |               4.7 |                  4.0 |               8.0 |              78.7
    2025-12 |             2653 |               4.2 |                  3.0 |               7.0 |              81.1
    (24 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 q025

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

    • spellingcast
    • numericsdocs/NUMERICS.md: rounding removed; results keep full precision; bare NUMERIC cast that only served the rounding removed; AVG over an integer computed as a decimal: AVG(CAST(x AS NUMERIC(18,6)))
    • made explicita selected t.col gets AS col, its standard name made explicit
    • placeholderpercentile cont (2×)
    • placeholderdate diff days
    • 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 cd.month_label as month,       count(*) as orders_delivered,       round(avg(dd.delivery_days)::numeric, 1) as avg_delivery_days,       round(cast(percentile_cont(0.5) within group (order by dd.delivery_days) as numeric), 1) as median_delivery_days,       round(cast(percentile_cont(0.9) within group (order by dd.delivery_days) as numeric), 1) as p90_delivery_days,       round(100.0 * sum(case when dd.delivery_days <= 5 then 1 else 0 end) / count(*), 1) as pct_within_5_days       avg(cast(dd.delivery_days as numeric(18,6))) as avg_delivery_days,       {{percentile_cont(0.5, dd.delivery_days)}} as median_delivery_days,       {{percentile_cont(0.9, dd.delivery_days)}} as p90_delivery_days,       100.0 * sum(case when dd.delivery_days <= 5 then 1 else 0 end) / count(*) as pct_within_5_daysfrom (  select o.id as order_id,         o.placed_at,         (cast(            (select max(s.delivered_at)         o.placed_at AS placed_at,         {{date_diff_days(o.placed_at, (select max(s.delivered_at)             from fulfillments f             join shipments s on s.fulfillment_id = f.id             where f.order_id = o.id) as date)          - cast(o.placed_at as date)) as delivery_days             where f.order_id = o.id))}} as delivery_days  from orders o  where o.status = 'fulfilled'    and o.placed_at >= timestamp '2024-01-01'    and o.placed_at < timestamp '2026-01-01'    and exists (      select 1 from fulfillments f join shipments s on s.fulfillment_id = f.id where f.order_id = o.id    )    and not exists (      select 1 from fulfillments f join shipments s on s.fulfillment_id = f.id where f.order_id = o.id and s.delivered_at is null    )) ddjoin calendar_days cd on cd.calendar_date = cast(dd.placed_at as date)group by cd.month_labelorder by cd.month_labelorder by cd.month_label NULLS LAST
    select cd.month_label as month,
           count(*) as orders_delivered,
           avg(cast(dd.delivery_days as numeric(18,6))) as avg_delivery_days,
           PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY dd.delivery_days) as median_delivery_days,
           PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY dd.delivery_days) as p90_delivery_days,
           100.0 * sum(case when dd.delivery_days <= 5 then 1 else 0 end) / count(*) as pct_within_5_days
    from (
      select o.id as order_id,
             o.placed_at AS placed_at,
             DATE_DIFF('day', o.placed_at, (select max(s.delivered_at)
                 from fulfillments f
                 join shipments s on s.fulfillment_id = f.id
                 where f.order_id = o.id)) as delivery_days
      from orders o
      where o.status = 'fulfilled'
        and o.placed_at >= timestamp '2024-01-01'
        and o.placed_at < timestamp '2026-01-01'
        and exists (
          select 1 from fulfillments f join shipments s on s.fulfillment_id = f.id where f.order_id = o.id
        )
        and not exists (
          select 1 from fulfillments f join shipments s on s.fulfillment_id = f.id where f.order_id = o.id and s.delivered_at is null
        )
    ) dd
    join calendar_days cd on cd.calendar_date = cast(dd.placed_at as date)
    group by cd.month_label
    order by cd.month_label NULLS LAST
    1 s10 sHyper1.92 sDuckDB2.84 sGizmoSQL1.99 sclickhouse_brew2.15 sFirebolt3.21 sDataFusion1.24 sClickHouseerror
    • ClickHouseCode: 48. DB::Exception: Current query is not supported yet, because can't find correlated column '__table5.id' in current header: __table5.placed_at. (NOT_IMPLEMENTED)
  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, 7 turns
    • The request, as Felix received it
      Report request from Lena Hartmann (analyst for the Chief Operating Officer):
      
      Title: Delivery time trend, monthly (2024-2025)
      Question: Over the last two years, how long does it take from order placed to delivered, and how much does that vary month to month?
      Grain: one row per calendar month of order placement
      Columns:
      - month (YYYY-MM)
      - orders_delivered (count of orders fully delivered)
      - avg_delivery_days (mean, placed_at to final delivered_at)
      - median_delivery_days (p50)
      - p90_delivery_days
      - pct_within_5_days (share of delivered orders where delivery_days <= 5)
      Details: Grain: one row per month_label (from calendar_days) of orders.placed_at. Scope: orders with placed_at between 2024-01-01 and 2025-12-31 inclusive, status = 'fulfilled'. For each such order, compute delivery_days = DATEDIFF(day, orders.placed_at, max(shipments.delivered_at) across all shipments reachable via fulfillments.order_id = orders.id and shipments.fulfillment_id = fulfillments.id), only including orders where every shipment linked to that order has a non-null delivered_at (i.e. fully delivered, no partial). Exclude orders with no linked shipments. Round avg/median/p90 to 1 decimal place; pct_within_5_days as a percentage rounded to 1 decimal. Order by month ascending. Expect 24 rows.
      
      Check your query with the warehouse's dry_run tool, then hand it in with submit.
    • Felix calls dry_run
      select cd.month_label as month,
             count(*) as orders_delivered,
             round(avg(dd.delivery_days)::numeric, 1) as avg_delivery_days,
             round(cast(percentile_cont(0.5) within group (order by dd.delivery_days) as numeric), 1) as median_delivery_days,
             round(cast(percentile_cont(0.9) within group (order by dd.delivery_days) as numeric), 1) as p90_delivery_days,
             round(100.0 * sum(case when dd.delivery_days <= 5 then 1 else 0 end) / count(*), 1) as pct_within_5_days
      from (
        select o.id as order_id,
               o.placed_at,
               (cast(
                  (select max(s.delivered_at)
                   from fulfillments f
                   join shipments s on s.fulfillment_id = f.id
                   where f.order_id = o.id) as date)
                - cast(o.placed_at as date)) as delivery_days
        from orders o
        where o.status = 'fulfilled'
          and o.placed_at >= timestamp '2024-01-01'
          and o.placed_at < timestamp '2026-01-01'
          and exists (
            select 1 from fulfillments f join shipments s on s.fulfillment_id = f.id where f.order_id = o.id
          )
          and not exists (
            select 1 from fulfillments f join shipments s on s.fulfillment_id = f.id where f.order_id = o.id and s.delivered_at is null
          )
      ) dd
      join calendar_days cd on cd.calendar_date = cast(dd.placed_at as date)
      group by cd.month_label
      order by cd.month_label
    • The warehouse answers
      STATUS: COMPILES (dry run on the empty schema, no data read)
      COLUMNS: month, orders_delivered, avg_delivery_days, median_delivery_days, p90_delivery_days, pct_within_5_days
    • FelixQuery compiles with the expected columns. Submitting.
    • Felix calls submit
      select cd.month_label as month,
             count(*) as orders_delivered,
             round(avg(dd.delivery_days)::numeric, 1) as avg_delivery_days,
             round(cast(percentile_cont(0.5) within group (order by dd.delivery_days) as numeric), 1) as median_delivery_days,
             round(cast(percentile_cont(0.9) within group (order by dd.delivery_days) as numeric), 1) as p90_delivery_days,
             round(100.0 * sum(case when dd.delivery_days <= 5 then 1 else 0 end) / count(*), 1) as pct_within_5_days
      from (
        select o.id as order_id,
               o.placed_at,
               (cast(
                  (select max(s.delivered_at)
                   from fulfillments f
                   join shipments s on s.fulfillment_id = f.id
                   where f.order_id = o.id) as date)
                - cast(o.placed_at as date)) as delivery_days
        from orders o
        where o.status = 'fulfilled'
          and o.placed_at >= timestamp '2024-01-01'
          and o.placed_at < timestamp '2026-01-01'
          and exists (
            select 1 from fulfillments f join shipments s on s.fulfillment_id = f.id where f.order_id = o.id
          )
          and not exists (
            select 1 from fulfillments f join shipments s on s.fulfillment_id = f.id where f.order_id = o.id and s.delivered_at is null
          )
      ) dd
      join calendar_days cd on cd.calendar_date = cast(dd.placed_at as date)
      group by cd.month_label
      order by cd.month_label
    • The warehouse answers
      Submitted to tonight's batch. Your work on this request is done.
    • FelixSubmitted. The report computes, per calendar month of `placed_at` (2024-01 through 2025-12), the count of fully-delivered fulfilled orders along with mean, median, p90 delivery days and the share delivered within 5 days — using calendar-day differences between order placement and the latest shipment delivery, restricted to orders where every linked shipment has a delivered_at.