90-Day Churn by First-Order Cohort and Acquisition Channel

PostgreSQL · read replica · for the VP of Growth · fictional retailer

Summary

This analysis measures the share of new customers who place no second completed order within 90 days of their first, by first-order month and acquisition channel. The query runs as one CTE pipeline on the read replica, scans orders once with an index-friendly filter, and only reports cohorts with a full 90-day window. All results depend on your data — [validate] against the reconciliation checks below before sharing.

Definitions and grain

TermDefinition
Completed orderorders.status not in ('cancelled', 'refunded') [validate the status list]
First orderEarliest completed ordered_at per customer (UTC)
Cohortdate_trunc('month', first_order_at) in UTC
ChurnedNo completed order where first_order_at < ordered_at <= first_order_at + interval '90 days'
Eligible cohortfirst_order_at <= now() - interval '90 days'
Grain of outputOne row per cohort month × acquisition channel

Data quality checks

sql
-- 1) Orders with no matching customer (should be 0)
SELECT count(*) AS orphan_orders
FROM orders o
LEFT JOIN customers c ON c.customer_id = o.customer_id
WHERE c.customer_id IS NULL;

-- 2) Unknown or null acquisition channels
SELECT coalesce(acquisition_channel, '(null)') AS channel, count(*)
FROM customers
GROUP BY 1
ORDER BY 2 DESC;

-- 3) Status values actually present (confirm the exclusion list)
SELECT status, count(*) FROM orders GROUP BY status ORDER BY 2 DESC;

-- 4) Duplicate order ids (should be 0)
SELECT count(*) - count(DISTINCT order_id) AS duplicate_orders FROM orders;

Main analysis

sql
-- 90-day churn by first-order cohort and channel (UTC throughout).
WITH completed AS (
  -- Filter once; status is low-cardinality so this uses the
  -- (customer_id, ordered_at) index and a filter, not a separate scan.
  SELECT customer_id, ordered_at
  FROM orders
  WHERE status NOT IN ('cancelled', 'refunded')
),
first_orders AS (
  SELECT customer_id, min(ordered_at) AS first_order_at
  FROM completed
  GROUP BY customer_id
),
eligible AS (
  -- Only customers whose 90-day window has fully elapsed.
  SELECT f.customer_id,
         f.first_order_at,
         date_trunc('month', f.first_order_at AT TIME ZONE 'UTC') AS cohort_month,
         coalesce(c.acquisition_channel, 'unknown') AS channel
  FROM first_orders f
  JOIN customers c USING (customer_id)
  WHERE f.first_order_at <= now() - interval '90 days'
),
repeaters AS (
  -- EXISTS-style semi-join: stops at the first qualifying repeat order.
  SELECT e.customer_id
  FROM eligible e
  WHERE EXISTS (
    SELECT 1
    FROM completed o
    WHERE o.customer_id = e.customer_id
      AND o.ordered_at >  e.first_order_at
      AND o.ordered_at <= e.first_order_at + interval '90 days'
  )
)
SELECT
  e.cohort_month::date                                   AS cohort_month,
  e.channel,
  count(*)                                               AS new_customers,
  count(*) - count(r.customer_id)                        AS churned_customers,
  round(100.0 * (count(*) - count(r.customer_id)) / count(*), 1) AS churn_rate_pct
FROM eligible e
LEFT JOIN repeaters r USING (customer_id)
GROUP BY 1, 2
HAVING count(*) >= 50          -- suppress tiny groups (noise and privacy)
ORDER BY 1, 2;

Performance notes

Validation

sql
-- A) New customers in the output must equal eligible customers overall.
SELECT count(*) FROM first_orders WHERE first_order_at <= now() - interval '90 days';
-- compare with: SELECT sum(new_customers) FROM (<main query without HAVING>) q;

-- B) Spot-check one customer by hand.
SELECT customer_id, ordered_at, status
FROM orders
WHERE customer_id = :sample_customer_id   -- pick one churned and one repeater
ORDER BY ordered_at;

Edge cases handled: same-timestamp duplicate first orders (the repeat must be strictly later); null channels (unknown); refunds after a repeat order (refunded orders never count as repeats); cohorts younger than 90 days (excluded).

Output spec for the VP of Growth

ElementSpec
Main viewHeatmap: rows = cohort month, columns = channel, cell = churn rate %; darker = higher churn
CompanionLine chart of churn rate % by cohort month, one line per top-4 channel
LabelsShow new_customers on hover; hide cells with fewer than 50 customers
Headline"[Channel] cohorts churn [x] points more than [channel] over the last [n] months" — fill only from validated results

Interpretation template (fill after validation)

Risks and next steps

Next steps I can help with: I can write the dashboard spec or a scheduled dbt version. Can you share the row count from validation A so we can confirm the totals reconcile?

Sample output · ChatterMate.Ai Prompt Library · fictional data — your result will differ.