La rétention par cohorte en SQL
Les clients que vous gagnez reviennent-ils ? Regroupez-les en cohortes selon le mois de leur première commande ; la rétention au mois N est la part d'une cohorte active (ici : qui commande de nouveau) N mois après son premier mois.
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;
Ajoutez une colonne par âge, et divisez-la par customers pour obtenir le taux. Lisez la matrice par colonne pour comparer les
cohortes, et en diagonale pour voir le calendrier : une saison touche toutes les cohortes le même mois.
Le piège. Une cohorte trop jeune pour avoir atteint le mois N affiche 0 %, et le graphique ressemble à un
effondrement : laissez la case vide, CASE WHEN ce mois est terminé THEN … END, sans ELSE. Et cherchez le premier
mois sur tout l'historique avant de filtrer les cohortes affichées : filtrez d'abord les commandes, et chaque ancien
client paraît nouveau.
Sur votre entrepôt. BigQuery écrit DATE_TRUNC(DATE(ordered_at), MONTH). L'âge en mois s'écrit plus court avec
DATEDIFF(month, cohort, order_month) sur Snowflake et DATE_DIFF(order_month, cohort, MONTH) sur 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;
| cohort | customers | m1_pct | m2_pct | m3_pct |
|---|---|---|---|---|
| 2025-06-01 | 5 | 40.0 | 40.0 | 20.0 |
| 2025-07-01 | 11 | 45.5 | 27.3 | 36.4 |
| 2025-08-01 | 7 | 42.9 | 28.6 | 42.9 |
| 2025-09-01 | 13 | 23.1 | 30.8 | NULL |
| 2025-10-01 | 11 | 45.5 | NULL | NULL |
| 2025-11-01 | 14 | NULL | NULL | NULL |
| 2025-12-01 | 3 | NULL | NULL | NULL |