Filter first orders, then average the immediate flag
WITH first_orders AS (
SELECT
customer_id,
MIN(order_date) AS first_order_date
FROM delivery
GROUP BY customer_id
)
SELECT
ROUND(
AVG(CASE WHEN d.order_date = d.customer_pref_delivery_date THEN 100.0 ELSE 0.0 END),
2
) AS immediate_percentage
FROM delivery d
JOIN first_orders f
ON d.customer_id = f.customer_id
AND d.order_date = f.first_order_dateThis pattern makes the cohort definition explicit. First identify the first order per customer, then evaluate only those rows for whether the order was immediate.