RFM Customer Segmentation
Scores customers by Recency, Frequency, and Monetary value into actionable segments with SQL.
Prompt
ROLE: You are a CRM analyst building an RFM segmentation for marketing. CONTEXT: Build RFM segments from transactions in [TRANSACTIONS_TABLE] (columns: customer_id, order_timestamp, order_value). Analysis as-of date: [AS_OF_DATE]. Engine: [DATABASE_ENGINE]. Desired number of score buckets: [N_BUCKETS] (typically 5). TASK: 1. Define Recency, Frequency, and Monetary precisely (recency = days since last order as of [AS_OF_DATE]; frequency = order count in window; monetary = total or average value -- state which). 2. Use NTILE([N_BUCKETS]) to score each dimension 1-5 (handle recency so that more recent = higher score). 3. Combine into an RFM code and map code ranges to named segments (Champions, Loyal, At Risk, Hibernating, Lost, New) with the mapping rules stated. 4. Output per-segment customer counts and revenue share. 5. Recommend one marketing action per segment. OUTPUT FORMAT: Definitions -> Scoring ```sql``` -> Segment mapping table -> Segment summary query -> Recommended actions. CONSTRAINTS: Make recency scoring direction explicit (low days = high score). Handle ties and customers with a single order. Exclude refunds/test accounts if present. Keep [AS_OF_DATE] parameterized so the model is reproducible.
How to use this prompt
- 1
Copy the prompt above and paste it into ChatGPT, Claude, or Gemini — or open it in the visual Studio to edit each part on a canvas and run it with your own key.
- 2
Replace any bracketed placeholders with your specifics. The more concrete your context and constraints, the sharper the result — see the 5-part prompt structure.
- 3
Run it, then refine. Ask the model to critique and improve its own answer with self-critique prompting.
Techniques in this prompt
Assigns the model an expert persona so it adopts the right vocabulary, depth, and standards for the task.
Learn this techniqueForces explicit intermediate reasoning instead of jumping to a conclusion, which improves accuracy on hard tasks.
Learn this techniquePins the response to a defined structure so it drops straight into your workflow.
Learn this techniqueRecommended models
Build on this prompt
Open it in the visual Studio to wire it into a full workflow with your own API key — or learn the craft behind prompts like this.
More in Data Analysis & SQL
Translate Business Questions Into SQL
Turns a plain-English stakeholder question into a correct, well-commented SQL query against a known schema.
Optimize A Slow SQL Query
Diagnoses why a query is slow and rewrites it with targeted, explained optimizations and an index plan.
Debug A SQL Query That Returns Wrong Results
Systematically finds the logic error producing incorrect numbers and delivers a corrected, verified query.
Explain An Unfamiliar SQL Query In Plain English
Reverse-engineers a complex inherited query into a clear narrative, business meaning, and risk list.