SQL Dojo
Indicatore

Retention per coorti in SQL

Raggruppare i clienti per mese del primo ordine e misurare la quota ancora attiva 1, 2, 3 mesi dopo, lasciando vuote le coorti troppo recenti.

I clienti che acquisisci continuano a tornare? Raggruppali in coorti in base al mese del loro primo ordine; la retention al mese N è la quota di una coorte ancora attiva (qui: che ordina di nuovo) N mesi dopo il mese di partenza.

WITH firsts AS (
  SELECT customer_id,
         DATE_TRUNC('month', ordered_at) AS order_month,
         MIN(DATE_TRUNC('month', ordered_at)) OVER (PARTITION BY customer_id) AS cohort
  FROM orders
  WHERE status <> 'cancelled'
),
aged AS (
  SELECT customer_id, cohort,
         (EXTRACT(YEAR FROM order_month) - EXTRACT(YEAR FROM cohort)) * 12
           + EXTRACT(MONTH FROM order_month) - EXTRACT(MONTH FROM cohort) AS age
  FROM firsts
)
SELECT cohort,
       COUNT(DISTINCT customer_id) AS customers,
       COUNT(DISTINCT CASE WHEN age = 1 THEN customer_id END) AS active_m1
FROM aged
GROUP BY cohort
ORDER BY cohort;

Aggiungi una colonna per ogni età e dividi ciascuna per customers per ottenere il tasso. Leggi la matrice per colonna per confrontare le coorti, e lungo le diagonali per vedere l'effetto del calendario: una stagione colpisce tutte le coorti nello stesso mese.

La trappola. Una coorte troppo recente per aver raggiunto il mese N mostra 0 %, e il grafico sembra segnalare un crollo: lascia vuota la cella, CASE WHEN quel mese è finito THEN … END, senza ELSE. E calcola il primo mese su tutto lo storico prima di filtrare le coorti da mostrare: se filtri prima gli ordini, ogni cliente di lunga data sembra nuovo.

Sul tuo data warehouse. BigQuery scrive DATE_TRUNC(DATE(ordered_at), MONTH). L'età in mesi si scrive in modo più breve con DATEDIFF(month, cohort, order_month) su Snowflake e DATE_DIFF(order_month, cohort, MONTH) su BigQuery.

-- The data stops on 27 December 2025: a cell counts only if its month ended before December.
WITH firsts AS (
  SELECT customer_id,
         CAST(DATE_TRUNC('month', ordered_at) AS DATE) AS order_month,
         CAST(MIN(DATE_TRUNC('month', ordered_at)) OVER (PARTITION BY customer_id) AS DATE) AS cohort
  FROM orders
  WHERE status <> 'cancelled'
)
SELECT cohort,
       COUNT(DISTINCT customer_id) AS customers,
       CASE WHEN cohort + INTERVAL 1 MONTH < DATE '2025-12-01'
            THEN ROUND(100.0 * COUNT(DISTINCT CASE WHEN order_month = cohort + INTERVAL 1 MONTH THEN customer_id END)
                       / COUNT(DISTINCT customer_id), 1) END AS m1_pct,
       CASE WHEN cohort + INTERVAL 2 MONTH < DATE '2025-12-01'
            THEN ROUND(100.0 * COUNT(DISTINCT CASE WHEN order_month = cohort + INTERVAL 2 MONTH THEN customer_id END)
                       / COUNT(DISTINCT customer_id), 1) END AS m2_pct,
       CASE WHEN cohort + INTERVAL 3 MONTH < DATE '2025-12-01'
            THEN ROUND(100.0 * COUNT(DISTINCT CASE WHEN order_month = cohort + INTERVAL 3 MONTH THEN customer_id END)
                       / COUNT(DISTINCT customer_id), 1) END AS m3_pct
FROM firsts
WHERE cohort >= DATE '2025-06-01'
GROUP BY cohort
ORDER BY cohort;
cohortcustomersm1_pctm2_pctm3_pct
2025-06-01540.040.020.0
2025-07-011145.527.336.4
2025-08-01742.928.642.9
2025-09-011323.130.8NULL
2025-10-011145.5NULLNULL
2025-11-0114NULLNULLNULL
2025-12-013NULLNULLNULL
Provalo in Randori