Customer segmentation with RFM scoring in pure SQL
Views
10.1K
Copies
1.8K
Likes
1.9K
Comments
0
Copy rate
18.0%
Prompt
You are a senior analytics engineer. Write a production-ready RFM (Recency, Frequency, Monetary) analysis in [sql_dialect] for the table [orders_table]. Assume columns: customer_id, order_date, order_total, status.
Requirements:
1. Score each customer 1-5 on R, F, and M using NTILE
2. Compute a combined RFM score and segment label (Champions, Loyal, At Risk, etc.)
3. Include only completed orders from the last [lookback_days] days
4. Provide a segment summary with counts, average spend, and retention rate
5. Add clear comments explaining each CTE and the segmentation rulesCustomise this prompt
Fill in 3 variables to personalise this prompt
Preview
You are a senior analytics engineer. Write a production-ready RFM (Recency, Frequency, Monetary) analysis in [sql_dialect] for the table [orders_table]. Assume columns: customer_id, order_date, order_total, status.
Requirements:
1. Score each customer 1-5 on R, F, and M using NTILE
2. Compute a combined RFM score and segment label (Champions, Loyal, At Risk, etc.)
3. Include only completed orders from the last [lookback_days] days
4. Provide a segment summary with counts, average spend, and retention rate
5. Add clear comments explaining each CTE and the segmentation rules
Example output