DB Olympics

Queries · q243 · Chief Operating Officer · week 2, night 4

Order-level delivery reliability: Lowlands Express by destination lane, full year 2025 (unified re-check)

“Confirmed and reconciled: how much does Lowlands Express's order-level reliability differ between its home lanes (Netherlands/Belgium/France) and everywhere else, versus the rest of our carriers combined?”

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

    Before I write anything, I cross-checked the three night-3 order-level reports against each other by hand, and they don't reconcile: Report 1 says the three weak carriers blend to 66.8% within 5 days; Report 2's Albion baseline/collapse figures imply Albion alone blends to ~80.1%, which would force Landfall+Lowlands down to ~58.8% to match Report 1's total. But Report 3's "all other carriers" (82.0%, 18,088 orders) combined with Report 1's "good eleven" (93.4%, 13,696 orders) and Report 2's Albion (~80.1%, 2,564 orders) overshoots the reported total on its own -- it would require Landfall's own rate to be negative, which is impossible. So at least one of these three reports has an inconsistency (likely a scoping or computation difference across the three separate queries), and I'm not willing to hand Priya a "ceiling" number or a "fix Lowlands' lanes" recommendation until it's resolved -- this is exactly the kind of number I'd expect her or Daniel's/Tomasz's teams to poke at. Tonight (last night) I'm spending all 3 slots getting a single, internally-consistent source of truth in one query each, rather than stitching across reports: (1) one full-year carrier-level breakdown to anchor the ceiling calculation, (2) one carrier-group-by-quarter breakdown (Albion / Landfall / Lowlands / everyone else) to nail down the timing story cleanly, and (3) one Lowlands-by-lane breakdown done the same consistent way, so the lane-reassignment number is trustworthy. I'll reconcile against this week's earlier reports once these are back and flag any remaining discrepancy explicitly in the memo rather than paper over it.

    Order-level delivery reliability: Lowlands Express by destination lane, full year 2025 (unified re-check)
    Question
    Confirmed and reconciled: how much does Lowlands Express's order-level reliability differ between its home lanes (Netherlands/Belgium/France) and everywhere else, versus the rest of our carriers combined?
    One row per
    lane_group: 'Lowlands core (NL/BE/FR)', 'Lowlands non-core (other countries)', 'All other carriers combined'
    Columns
    • lane_group
    • orders_fully_delivered
    • avg_delivery_days
    • pct_within_5_days
    • share_of_orders_pct
  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 order_fulfillment_counts AS (
      SELECT order_id, COUNT(*) AS fulfillment_count
      FROM fulfillments
      GROUP BY order_id
    ),
    fulfillment_shipment_counts AS (
      SELECT fulfillment_id, COUNT(*) AS shipment_count
      FROM shipments
      GROUP BY fulfillment_id
    ),
    eligible_orders AS (
      SELECT
        o.id AS order_id,
        o.placed_at,
        o.shipping_address_id,
        f.id AS fulfillment_id
      FROM orders o
      JOIN order_fulfillment_counts ofc
        ON ofc.order_id = o.id AND ofc.fulfillment_count = 1
      JOIN fulfillments f
        ON f.order_id = o.id
      WHERE o.status = 'fulfilled'
        AND o.placed_at >= DATE '2025-01-01'
        AND o.placed_at < DATE '2026-01-01'
    ),
    eligible_with_shipment AS (
      SELECT
        eo.order_id,
        eo.placed_at,
        eo.shipping_address_id,
        s.carrier,
        s.delivered_at
      FROM eligible_orders eo
      JOIN fulfillment_shipment_counts fsc
        ON fsc.fulfillment_id = eo.fulfillment_id AND fsc.shipment_count = 1
      JOIN shipments s
        ON s.fulfillment_id = eo.fulfillment_id
      WHERE s.delivered_at IS NOT NULL
    ),
    order_lane AS (
      SELECT
        ews.order_id,
        EXTRACT(EPOCH FROM (ews.delivered_at - ews.placed_at)) / 86400.0 AS delivery_days,
        CASE
          WHEN ews.carrier = 'Lowlands Express' AND c.name IN ('Netherlands', 'Belgium', 'France')
            THEN 'Lowlands core (NL/BE/FR)'
          WHEN ews.carrier = 'Lowlands Express'
            THEN 'Lowlands non-core (other countries)'
          ELSE 'All other carriers combined'
        END AS lane_group
      FROM eligible_with_shipment ews
      LEFT JOIN addresses a ON a.id = ews.shipping_address_id
      LEFT JOIN countries c ON c.id = a.country_id
    ),
    lane_groups AS (
      SELECT 'Lowlands core (NL/BE/FR)' AS lane_group, 1 AS sort_order
      UNION ALL
      SELECT 'Lowlands non-core (other countries)', 2
      UNION ALL
      SELECT 'All other carriers combined', 3
    ),
    lane_stats AS (
      SELECT
        lane_group,
        COUNT(*) AS orders_fully_delivered,
        AVG(delivery_days) AS avg_delivery_days,
        AVG(CASE WHEN delivery_days <= 5 THEN 1.0 ELSE 0.0 END) * 100 AS pct_within_5_days
      FROM order_lane
      GROUP BY lane_group
    ),
    totals AS (
      SELECT SUM(orders_fully_delivered) AS total_orders FROM lane_stats
    )
    SELECT
      lg.lane_group,
      COALESCE(ls.orders_fully_delivered, 0) AS orders_fully_delivered,
      ROUND(CAST(ls.avg_delivery_days AS numeric), 2) AS avg_delivery_days,
      ROUND(CAST(ls.pct_within_5_days AS numeric), 1) AS pct_within_5_days,
      ROUND(CAST(COALESCE(ls.orders_fully_delivered, 0) AS numeric) / NULLIF(t.total_orders, 0) * 100, 1) AS share_of_orders_pct
    FROM lane_groups lg
    LEFT JOIN lane_stats ls ON ls.lane_group = lg.lane_group
    CROSS JOIN totals t
    ORDER BY lg.sort_order

    “Query compiles and returns 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 splits the fully-delivered, single-shipment 2025 order population into the three requested lane groups (Lowlands core, Lowlands non-core, all other carriers), with counts, average delivery days, % within 5 days, and share of total — using the identical eligibility filter as the other reports in this batch so the totals should reconcile.”

  4. The report comes back overnight

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

    lane_group                          | orders_fully_delivered | avg_delivery_days | pct_within_5_days | share_of_orders_pct
    ------------------------------------+------------------------+-------------------+-------------------+--------------------
    Lowlands core (NL/BE/FR)            |                   1581 |              4.34 |              72.9 |                 7.7
    Lowlands non-core (other countries) |                    864 |              5.96 |              34.1 |                 4.2
    All other carriers combined         |                  18088 |              3.92 |              82.0 |                88.1
    (3 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 q243

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

    • numericsdocs/NUMERICS.md: rounding removed; results keep full precision; bare NUMERIC cast that only served the rounding removed; intended fraction: integer numerator cast to NUMERIC(18,6) before the division
    • 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 (9×)
    • 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 order_fulfillment_counts AS (  SELECT order_id, COUNT(*) AS fulfillment_count  FROM fulfillments  GROUP BY order_id),fulfillment_shipment_counts AS (  SELECT fulfillment_id, COUNT(*) AS shipment_count  FROM shipments  GROUP BY fulfillment_id),eligible_orders AS (  SELECT    o.id AS order_id,    o.placed_at,    o.shipping_address_id,    o.placed_at AS placed_at,    o.shipping_address_id AS shipping_address_id,    f.id AS fulfillment_id  FROM orders o  JOIN order_fulfillment_counts ofc    ON ofc.order_id = o.id AND ofc.fulfillment_count = 1  JOIN fulfillments f    ON f.order_id = o.id  WHERE o.status = 'fulfilled'    AND o.placed_at >= DATE '2025-01-01'    AND o.placed_at < DATE '2026-01-01'),eligible_with_shipment AS (  SELECT    eo.order_id,    eo.placed_at,    eo.shipping_address_id,    s.carrier,    s.delivered_at    eo.order_id AS order_id,    eo.placed_at AS placed_at,    eo.shipping_address_id AS shipping_address_id,    s.carrier AS carrier,    s.delivered_at AS delivered_at  FROM eligible_orders eo  JOIN fulfillment_shipment_counts fsc    ON fsc.fulfillment_id = eo.fulfillment_id AND fsc.shipment_count = 1  JOIN shipments s    ON s.fulfillment_id = eo.fulfillment_id  WHERE s.delivered_at IS NOT NULL),order_lane AS (  SELECT    ews.order_id,    EXTRACT(EPOCH FROM (ews.delivered_at - ews.placed_at)) / 86400.0 AS delivery_days,    ews.order_id AS order_id,    {{seconds_between(ews.placed_at, ews.delivered_at)}} / 86400.0 AS delivery_days,    CASE      WHEN ews.carrier = 'Lowlands Express' AND c.name IN ('Netherlands', 'Belgium', 'France')        THEN 'Lowlands core (NL/BE/FR)'      WHEN ews.carrier = 'Lowlands Express'        THEN 'Lowlands non-core (other countries)'      ELSE 'All other carriers combined'    END AS lane_group  FROM eligible_with_shipment ews  LEFT JOIN addresses a ON a.id = ews.shipping_address_id  LEFT JOIN countries c ON c.id = a.country_id),lane_groups AS (  SELECT 'Lowlands core (NL/BE/FR)' AS lane_group, 1 AS sort_order  UNION ALL  SELECT 'Lowlands non-core (other countries)', 2  UNION ALL  SELECT 'All other carriers combined', 3),lane_stats AS (  SELECT    lane_group,    COUNT(*) AS orders_fully_delivered,    AVG(delivery_days) AS avg_delivery_days,    AVG(CASE WHEN delivery_days <= 5 THEN 1.0 ELSE 0.0 END) * 100 AS pct_within_5_days  FROM order_lane  GROUP BY lane_group),totals AS (  SELECT SUM(orders_fully_delivered) AS total_orders FROM lane_stats)SELECT  lg.lane_group,  lg.lane_group AS lane_group,  COALESCE(ls.orders_fully_delivered, 0) AS orders_fully_delivered,  ROUND(CAST(ls.avg_delivery_days AS numeric), 2) AS avg_delivery_days,  ROUND(CAST(ls.pct_within_5_days AS numeric), 1) AS pct_within_5_days,  ROUND(CAST(COALESCE(ls.orders_fully_delivered, 0) AS numeric) / NULLIF(t.total_orders, 0) * 100, 1) AS share_of_orders_pct  ls.avg_delivery_days AS avg_delivery_days,  ls.pct_within_5_days AS pct_within_5_days,  CAST(COALESCE(ls.orders_fully_delivered, 0) AS numeric(18,6)) / NULLIF(t.total_orders, 0) * 100 AS share_of_orders_pctFROM lane_groups lgLEFT JOIN lane_stats ls ON ls.lane_group = lg.lane_groupCROSS JOIN totals tORDER BY lg.sort_orderORDER BY lg.sort_order NULLS LAST
    WITH order_fulfillment_counts AS (
      SELECT order_id, COUNT(*) AS fulfillment_count
      FROM fulfillments
      GROUP BY order_id
    ),
    fulfillment_shipment_counts AS (
      SELECT fulfillment_id, COUNT(*) AS shipment_count
      FROM shipments
      GROUP BY fulfillment_id
    ),
    eligible_orders AS (
      SELECT
        o.id AS order_id,
        o.placed_at AS placed_at,
        o.shipping_address_id AS shipping_address_id,
        f.id AS fulfillment_id
      FROM orders o
      JOIN order_fulfillment_counts ofc
        ON ofc.order_id = o.id AND ofc.fulfillment_count = 1
      JOIN fulfillments f
        ON f.order_id = o.id
      WHERE o.status = 'fulfilled'
        AND o.placed_at >= DATE '2025-01-01'
        AND o.placed_at < DATE '2026-01-01'
    ),
    eligible_with_shipment AS (
      SELECT
        eo.order_id AS order_id,
        eo.placed_at AS placed_at,
        eo.shipping_address_id AS shipping_address_id,
        s.carrier AS carrier,
        s.delivered_at AS delivered_at
      FROM eligible_orders eo
      JOIN fulfillment_shipment_counts fsc
        ON fsc.fulfillment_id = eo.fulfillment_id AND fsc.shipment_count = 1
      JOIN shipments s
        ON s.fulfillment_id = eo.fulfillment_id
      WHERE s.delivered_at IS NOT NULL
    ),
    order_lane AS (
      SELECT
        ews.order_id AS order_id,
        CAST(EXTRACT(EPOCH FROM (ews.delivered_at - ews.placed_at)) AS NUMERIC(18,6)) / 86400.0 AS delivery_days,
        CASE
          WHEN ews.carrier = 'Lowlands Express' AND c.name IN ('Netherlands', 'Belgium', 'France')
            THEN 'Lowlands core (NL/BE/FR)'
          WHEN ews.carrier = 'Lowlands Express'
            THEN 'Lowlands non-core (other countries)'
          ELSE 'All other carriers combined'
        END AS lane_group
      FROM eligible_with_shipment ews
      LEFT JOIN addresses a ON a.id = ews.shipping_address_id
      LEFT JOIN countries c ON c.id = a.country_id
    ),
    lane_groups AS (
      SELECT 'Lowlands core (NL/BE/FR)' AS lane_group, 1 AS sort_order
      UNION ALL
      SELECT 'Lowlands non-core (other countries)', 2
      UNION ALL
      SELECT 'All other carriers combined', 3
    ),
    lane_stats AS (
      SELECT
        lane_group,
        COUNT(*) AS orders_fully_delivered,
        AVG(delivery_days) AS avg_delivery_days,
        AVG(CASE WHEN delivery_days <= 5 THEN 1.0 ELSE 0.0 END) * 100 AS pct_within_5_days
      FROM order_lane
      GROUP BY lane_group
    ),
    totals AS (
      SELECT SUM(orders_fully_delivered) AS total_orders FROM lane_stats
    )
    SELECT
      lg.lane_group AS lane_group,
      COALESCE(ls.orders_fully_delivered, 0) AS orders_fully_delivered,
      ls.avg_delivery_days AS avg_delivery_days,
      ls.pct_within_5_days AS pct_within_5_days,
      CAST(COALESCE(ls.orders_fully_delivered, 0) AS numeric(18,6)) / NULLIF(t.total_orders, 0) * 100 AS share_of_orders_pct
    FROM lane_groups lg
    LEFT JOIN lane_stats ls ON ls.lane_group = lg.lane_group
    CROSS JOIN totals t
    ORDER BY lg.sort_order NULLS LAST
    1 s10 sHyper1.14 sDuckDB1.61 sGizmoSQL1.27 sclickhouse_brew2.08 sFirebolterrorDataFusion4.70 sClickHouse4.31 s
    • Firebolt{ "errors": [ { "description": "Line 79, Column 65: Operations between decimals with different precision and scale is not supported at the moment. Explicitly cast one of the decimals to other decimal precision and scale." } ], "query": { "query_id": "017a02b0-83fe-4eff-bee9-bd1065e03ecb", "query_lab
  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: Order-level delivery reliability: Lowlands Express by destination lane, full year 2025 (unified re-check)
      Question: Confirmed and reconciled: how much does Lowlands Express's order-level reliability differ between its home lanes (Netherlands/Belgium/France) and everywhere else, versus the rest of our carriers combined?
      Grain: one row per lane_group: 'Lowlands core (NL/BE/FR)', 'Lowlands non-core (other countries)', 'All other carriers combined'
      Columns:
      - lane_group
      - orders_fully_delivered
      - avg_delivery_days
      - pct_within_5_days
      - share_of_orders_pct
      Details: Same scoping and single-carrier attribution rule as the other two reports in this set (orders placed_at in 2025, status='fulfilled', exactly one fulfillment with exactly one shipment, delivered_at not null). For orders where shipments.carrier = 'Lowlands Express', split by destination country (orders.shipping_address_id -> addresses -> countries.name): 'Lowlands core (NL/BE/FR)' if country in ('Netherlands','Belgium','France'), else 'Lowlands non-core (other countries)'. All remaining orders (any other carrier) go to 'All other carriers combined'. Please also report the raw count of orders in each group and confirm the three groups sum to the same total order count as the full-year carrier report in this batch, so I can verify consistency across the two reports. Round avg_delivery_days to 2 decimals, pct columns to 1 decimal. Order: Lowlands core first, then Lowlands non-core, then All other carriers combined. Expect 3 rows.
      
      Check your query with the warehouse's dry_run tool, then hand it in with submit.
    • Grace calls dry_run
      WITH order_fulfillment_counts AS (
        SELECT order_id, COUNT(*) AS fulfillment_count
        FROM fulfillments
        GROUP BY order_id
      ),
      fulfillment_shipment_counts AS (
        SELECT fulfillment_id, COUNT(*) AS shipment_count
        FROM shipments
        GROUP BY fulfillment_id
      ),
      eligible_orders AS (
        SELECT
          o.id AS order_id,
          o.placed_at,
          o.shipping_address_id,
          f.id AS fulfillment_id
        FROM orders o
        JOIN order_fulfillment_counts ofc
          ON ofc.order_id = o.id AND ofc.fulfillment_count = 1
        JOIN fulfillments f
          ON f.order_id = o.id
        WHERE o.status = 'fulfilled'
          AND o.placed_at >= DATE '2025-01-01'
          AND o.placed_at < DATE '2026-01-01'
      ),
      eligible_with_shipment AS (
        SELECT
          eo.order_id,
          eo.placed_at,
          eo.shipping_address_id,
          s.carrier,
          s.delivered_at
        FROM eligible_orders eo
        JOIN fulfillment_shipment_counts fsc
          ON fsc.fulfillment_id = eo.fulfillment_id AND fsc.shipment_count = 1
        JOIN shipments s
          ON s.fulfillment_id = eo.fulfillment_id
        WHERE s.delivered_at IS NOT NULL
      ),
      order_lane AS (
        SELECT
          ews.order_id,
          EXTRACT(EPOCH FROM (ews.delivered_at - ews.placed_at)) / 86400.0 AS delivery_days,
          CASE
            WHEN ews.carrier = 'Lowlands Express' AND c.name IN ('Netherlands', 'Belgium', 'France')
              THEN 'Lowlands core (NL/BE/FR)'
            WHEN ews.carrier = 'Lowlands Express'
              THEN 'Lowlands non-core (other countries)'
            ELSE 'All other carriers combined'
          END AS lane_group
        FROM eligible_with_shipment ews
        LEFT JOIN addresses a ON a.id = ews.shipping_address_id
        LEFT JOIN countries c ON c.id = a.country_id
      ),
      lane_groups AS (
        SELECT 'Lowlands core (NL/BE/FR)' AS lane_group, 1 AS sort_order
        UNION ALL
        SELECT 'Lowlands non-core (other countries)', 2
        UNION ALL
        SELECT 'All other carriers combined', 3
      ),
      lane_stats AS (
        SELECT
          lane_group,
          COUNT(*) AS orders_fully_delivered,
          AVG(delivery_days) AS avg_delivery_days,
          AVG(CASE WHEN delivery_days <= 5 THEN 1.0 ELSE 0.0 END) * 100 AS pct_within_5_days
        FROM order_lane
        GROUP BY lane_group
      ),
      totals AS (
        SELECT SUM(orders_fully_delivered) AS total_orders FROM lane_stats
      )
      SELECT
        lg.lane_group,
        COALESCE(ls.orders_fully_delivered, 0) AS orders_fully_delivered,
        ROUND(CAST(ls.avg_delivery_days AS numeric), 2) AS avg_delivery_days,
        ROUND(CAST(ls.pct_within_5_days AS numeric), 1) AS pct_within_5_days,
        ROUND(CAST(COALESCE(ls.orders_fully_delivered, 0) AS numeric) / NULLIF(t.total_orders, 0) * 100, 1) AS share_of_orders_pct
      FROM lane_groups lg
      LEFT JOIN lane_stats ls ON ls.lane_group = lg.lane_group
      CROSS JOIN totals t
      ORDER BY lg.sort_order
    • The warehouse answers
      STATUS: COMPILES (dry run on the empty schema, no data read)
      COLUMNS: lane_group, orders_fully_delivered, avg_delivery_days, pct_within_5_days, share_of_orders_pct
    • GraceQuery compiles and returns the expected columns. Submitting.
    • Grace calls submit
      WITH order_fulfillment_counts AS (
        SELECT order_id, COUNT(*) AS fulfillment_count
        FROM fulfillments
        GROUP BY order_id
      ),
      fulfillment_shipment_counts AS (
        SELECT fulfillment_id, COUNT(*) AS shipment_count
        FROM shipments
        GROUP BY fulfillment_id
      ),
      eligible_orders AS (
        SELECT
          o.id AS order_id,
          o.placed_at,
          o.shipping_address_id,
          f.id AS fulfillment_id
        FROM orders o
        JOIN order_fulfillment_counts ofc
          ON ofc.order_id = o.id AND ofc.fulfillment_count = 1
        JOIN fulfillments f
          ON f.order_id = o.id
        WHERE o.status = 'fulfilled'
          AND o.placed_at >= DATE '2025-01-01'
          AND o.placed_at < DATE '2026-01-01'
      ),
      eligible_with_shipment AS (
        SELECT
          eo.order_id,
          eo.placed_at,
          eo.shipping_address_id,
          s.carrier,
          s.delivered_at
        FROM eligible_orders eo
        JOIN fulfillment_shipment_counts fsc
          ON fsc.fulfillment_id = eo.fulfillment_id AND fsc.shipment_count = 1
        JOIN shipments s
          ON s.fulfillment_id = eo.fulfillment_id
        WHERE s.delivered_at IS NOT NULL
      ),
      order_lane AS (
        SELECT
          ews.order_id,
          EXTRACT(EPOCH FROM (ews.delivered_at - ews.placed_at)) / 86400.0 AS delivery_days,
          CASE
            WHEN ews.carrier = 'Lowlands Express' AND c.name IN ('Netherlands', 'Belgium', 'France')
              THEN 'Lowlands core (NL/BE/FR)'
            WHEN ews.carrier = 'Lowlands Express'
              THEN 'Lowlands non-core (other countries)'
            ELSE 'All other carriers combined'
          END AS lane_group
        FROM eligible_with_shipment ews
        LEFT JOIN addresses a ON a.id = ews.shipping_address_id
        LEFT JOIN countries c ON c.id = a.country_id
      ),
      lane_groups AS (
        SELECT 'Lowlands core (NL/BE/FR)' AS lane_group, 1 AS sort_order
        UNION ALL
        SELECT 'Lowlands non-core (other countries)', 2
        UNION ALL
        SELECT 'All other carriers combined', 3
      ),
      lane_stats AS (
        SELECT
          lane_group,
          COUNT(*) AS orders_fully_delivered,
          AVG(delivery_days) AS avg_delivery_days,
          AVG(CASE WHEN delivery_days <= 5 THEN 1.0 ELSE 0.0 END) * 100 AS pct_within_5_days
        FROM order_lane
        GROUP BY lane_group
      ),
      totals AS (
        SELECT SUM(orders_fully_delivered) AS total_orders FROM lane_stats
      )
      SELECT
        lg.lane_group,
        COALESCE(ls.orders_fully_delivered, 0) AS orders_fully_delivered,
        ROUND(CAST(ls.avg_delivery_days AS numeric), 2) AS avg_delivery_days,
        ROUND(CAST(ls.pct_within_5_days AS numeric), 1) AS pct_within_5_days,
        ROUND(CAST(COALESCE(ls.orders_fully_delivered, 0) AS numeric) / NULLIF(t.total_orders, 0) * 100, 1) AS share_of_orders_pct
      FROM lane_groups lg
      LEFT JOIN lane_stats ls ON ls.lane_group = lg.lane_group
      CROSS JOIN totals t
      ORDER BY lg.sort_order
    • The warehouse answers
      Submitted to tonight's batch. Your work on this request is done.
    • GraceSubmitted. The report splits the fully-delivered, single-shipment 2025 order population into the three requested lane groups (Lowlands core, Lowlands non-core, all other carriers), with counts, average delivery days, % within 5 days, and share of total — using the identical eligibility filter as the other reports in this batch so the totals should reconcile.