Producing the RFM Report
Producing the RFM Report
Like most reports, an RFM report can be created using a series of SQL statements. This section presents two SQL Server stored procedures that produce RFM scores. Figure 13-20 shows the five tables that are used.
The CUSTOMER_SALES table contains the raw data that are used in the RFM calculations. CUSTOMER_RFM contains CustomerID and the final R, F, and M scores. The
Figure 13-20
SQL Server Tables for RFM Analysis
Chapter 13 Database Processing for Business Intelligence Systems
remaining three tables—CUSTOMER_R, CUSTOMER_F, and CUSTOMER_M—are used to store intermediate results. Note that all CustomerID columns are NOT NULL.
The stored procedure shown in Figure 13-21 is used to calculate and store the R, F, and M scores. It begins by deleting the results from any prior analysis from the CUSTOMER_R, CUSTOMER_F, and CUSTOMER_M tables. It then calls three procedures for computing the R, F, and M scores. Finally, it stores the R, F, and M scores in the CUSTOMER_RFM table. Only this table is needed for reporting purposes.
Figure 13-21
The RFM_Analysis Stored Procedure
Part 5 Database Access Standards
The Calculate_R stored procedure shown in Figure 13-22 illustrates how the R score is calculated. This procedure first places the date of each customer’s most recent order into the MostRecentOrderDate column. Then, it uses the SQL TOP {PercentageNumber} PERCENT syntax of SQL SELECT statements to set the R_Score values. The first UPDATE statement sets the value of R_Score to 1 for the top 20 percent of customers (after they have been sorted in descending order according to MostRecentOrderDate). Then, it sets the R_Score to 2 for the top 25 percent of customers who have a null value for R_Score in descending order of MostRe- centOrderDate. The procedure continues to set the R values for all customers. The Calculate_F and Calculate_M procedures are similar and will be left to you as Project Question 13.58.
Figure 13-22
The Calculate_R Stored Procedure
Chapter 13 Database Processing for Business Intelligence Systems
The SQL query for RFM Results
The RFM tables The RFM stored
procedures The SQL RFM query
results sorted by NumberOfCustomers in descending order
Figure 13-23
Example RFM Results
Figure 13-23 shows how the CUSTOMER_RFM table is used for reporting purposes. Figure 13-23 shows a SELECT on CUSTOMER_RFM that was prepared using a database of more than 5,000 customers and over 1 million transactions. The preparation of the CUSTOMER_RFM table required less than 30 seconds on a moderately powered personal computer.
The results in Figure 13-23 are interesting, but unless this information is delivered to the correct users it will be of no ultimate value to the organization. For example, the 2nd row in this figure shows that 202 customers have an RFM score of {4 1 1}. These customers order frequently, they order items of high monetary value, but they have not ordered recently. The company may be in danger of losing them. Somehow this report and the customers who have these scores (see Review Question 13.32) need to be made available to the appropriate sales personnel. To understand the modern means for accomplishing this, we will consider the components of a reporting system.
Parts
» This page intentionally left blank
» Reporting and Data Mining Database Applications
» Database Applications and SQL
» Reading Specified Columns and Rows from a Single Table
» “Does Not Work with Microsoft Access ANSI-89 SQL”
» Processing SQL Statements in Microsoft Access 2010
» Using SQL in Microsoft SQL Server 2008 R2
» Using SQL in Oracle Database 11g
» Using SQL in Oracle MySQL 5.5
» Wildcards in SQL WHERE Clauses
» Using SQL Built-in Functions
» SQL Expressions in SQL SELECT Statements
» Querying Multiple Tables with Subqueries
» Querying Multiple Tables with Joins
» Comparing Subqueries and Joins
» Finding Functional Dependencies
» Eliminating Anomalies from Multivalued Dependencies
» The Multivalue, Multicolumn Problem
» The General-Purpose Remarks Column
» R Diagrams Using the IE Crow’s Foot Model
» The Multivalued Attribute Pattern
» The Archetype/Instance Pattern
» The Student Acceptance Letter
» X This is a warning, no further action is required.
» 1:1 Relationships Between Strong Entities
» M Relationships Between Strong Entities
» Relationships in Mixed Entity Designs
» Representing Ternary and Higher-Order Relationships
» Relational Representation of the Highline University Data Model
» Surrogate Key Database Design
» Column Properties for the View Ridge Database Design Tables
» Variations in SQL Data Types
» Implementing Data Constraints
» Populating the View Ridge Database Tables
» Using Triggers to Provide Default Values
» The WORK_AddWorkTransaction Stored Procedure
» • If a PROJECT row is deleted, then the project has been canceled, and it is unneces-
» Reducing Cardinalities (with Data Loss)
» Optimistic Versus Pessimistic Locking
» Declaring Lock Characteristics
» Processing Rights and Responsibilities
» Recovery via Rollback/Rollforward
» Maintaining the Data Repository
» Types of Distributed Databases
» • Express Edition. This free, feature-limited version is available for download. It
» SQL Server 2008 R2 SQL Statements and SQL Scripts
» Creating the View Ridge Database Table Structure
» Populating the VRG Tables with Data
» The Stored Procedure InsertCustomerAndInterests
» The Stored Procedure InsertCustomerWithTransaction
» A Trigger for Setting Default Values
» A Trigger for Enforcing a Data Constraint
» A Trigger for Enforcing a Required Child Constraint
» Creating an ODBC Data Source Name
» Materializing XML Documents with XSLT
» Using the SQL SELECT . . . FOR XML Statement
» Multitable SELECT with FOR XML
» A Schema with Two Multivalued Paths
» Problems with Operational Data
» Using SQL for Market Basket Analysis
Show more