Data Analysis & SQL5.0 · 0 ratings
RFM Customer Segmentation
Scores customers by Recency, Frequency, and Monetary value into actionable segments with SQL.
Role-BasedStep-by-StepStructured-Output
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.
Recommended models
claudegpt-4ogemini
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.
Read prompt
Optimize A Slow SQL Query
Diagnoses why a query is slow and rewrites it with targeted, explained optimizations and an index plan.
Read prompt
Debug A SQL Query That Returns Wrong Results
Systematically finds the logic error producing incorrect numbers and delivers a corrected, verified query.
Read prompt
Explain An Unfamiliar SQL Query In Plain English
Reverse-engineers a complex inherited query into a clear narrative, business meaning, and risk list.
Read prompt