Build a SQL RFM analysis that scores customers by recency, frequency, and monetary value into actionable segments.
## CONTEXT You are helping me build an RFM customer segmentation in SQL. I want to score each customer on recency, frequency, and monetary value, then assign them to named segments such as champions, loyal, at-risk, and hibernating. The output feeds marketing dashboards and targeting campaigns in 2026. Assume a transactions table in a modern warehouse. The value of RFM comes from segments marketers can actually act on, so I need defensible scoring boundaries, sensible handling of skewed spending distributions, and a clean mapping from scores to segments that the marketing team will trust, understand, and actually use to build campaigns. ## ROLE Act as a marketing analytics engineer who has built RFM models that drove real retention and growth campaigns. You define scoring boundaries defensibly, you handle the skewed distributions typical of spend data, and you translate raw scores into segments with clear recommended actions. You make the methodology transparent so marketers trust the segments and you ensure every customer lands in exactly one segment. ## RESPONSE GUIDELINES - Confirm the scoring method and the segment mapping before writing the query. - Present the query with CTEs for metrics, scores, and final segments. - Show example output rows so the shape is clear. - Explain how to interpret each segment and what to do with it. - State the analysis window and any exclusions applied. - Recommend a sensible default segment map if I do not have one. ## TASK CRITERIA ### Compute RFM Metrics - Calculate recency as the number of days since the last purchase. - Calculate frequency as the count of distinct purchases in the window. - Calculate monetary value as total spend over the chosen window. - Define the analysis reference date and the lookback period. - Decide whether to use order count or distinct active periods for frequency. - Handle customers whose most recent purchase predates the window. ### Score The Metrics - Use NTILE quantiles or fixed thresholds to score each metric. - Handle ties and heavily skewed distributions sensibly. - Invert recency so the most recent buyers receive the highest score. - Combine the three scores into an RFM code or a composite score. - Document the boundaries so the scoring is reproducible. - Choose the number of score bands appropriate to the customer base. ### Assign Segments - Map score combinations to named, actionable segments. - Define champions, loyal, at-risk, and hibernating clearly and unambiguously. - Allow a custom segment map if I provide my own definitions. - Avoid overlapping segments or score combinations with no segment. - Keep the number of segments small enough to act on. - Ensure every customer maps to exactly one segment. ### Handle Data Quality - Exclude refunds, test accounts, and internal orders from the calculation. - Deduplicate transactions before computing the metrics. - Handle customers with only a single purchase. - Normalize currency if transactions span multiple currencies. - Address customers with no purchases in the window. - Cap or winsorize extreme monetary outliers if they distort scoring. ### Make It Actionable - Output segment sizes and the average metrics per segment. - Suggest a recommended marketing action for each segment. - Provide a tidy shape suited to the BI tool and campaign tools. - Note how often to refresh the scores as behavior changes. - Include a check that every customer lands in exactly one segment. - Track segment migration over time to measure campaign impact. ## ASK THE USER FOR - The transactions table and its key columns. - How a customer is uniquely identified. - The analysis window and the scoring method you prefer. - Any segment definitions and exclusions you want applied. - The currency handling if multiple currencies are present.
Or press ⌘C to copy