当前位置:网站首页>Real MySQL interview questions (XXVII) -- Classification of users by RFM analysis method

Real MySQL interview questions (XXVII) -- Classification of users by RFM analysis method

2022-06-23 05:48:00 Socialphobia_ FOGO

RFM Classification of users by analytical method

Here is 2020 year 1 User behavior record of an e-commerce platform in January :
 Insert picture description here
The business problem : The store found that , User operation is too extensive , Failed to achieve user classified operation . The boss wants to carry out targeted marketing for different users next month , Reduce costs and increase revenue , The effect of refined operation . What do I do ?

Involving user classification , It can be used RFM Analysis method .

One . What is? RFM Analysis method ?

RFM The analysis method is based on the user's last consumption interval (Recency)、 Consumption frequency (Frequency)、 Consumption amount (Monetary) To score users , Then users are divided into different types according to scores , Then use different operation strategies for different users , So as to realize auxiliary precision operation .
RFM The analysis method divides users into the following 8 class :
 Insert picture description here
 Insert picture description here
Two . How to use SQL Realization RFM Analysis method ?
1. Define indicators R、F、M
Set the... In the indicator definition “ A span “ Defined as 1 month (31 God ).

The last consumption interval : The user's last consumption is now (1 month 31 Japan ) How long has it been .

Consumption frequency (F): Users for a period of time (1 month ) How many times did you spend .

Consumption amount (M): Users for a period of time (1 month ) Amount of consumption , Corresponds to “ Consumption amount “.

 Insert picture description here

WITH temp1 AS (
SELECT *
FROM  User behavior record 
WHERE  User behavior type =' Buy '
),
temp2 AS (
SELECT  The user id ,MAX( User active date ) AS  Last shopping date 
FROM temp1
GROUP BY  The user id 
),
temp3 AS (--  Find out the user number and the latest consumption time interval R
SELECT  The user id ,DATEDIFF('2020-01-31',temp2. Last shopping date ) AS  The last consumption interval R
FROM temp2
),
temp4 AS (--  Find out the user number and consumption frequency F
SELECT  The user id ,COUNT(*) AS  Consumption frequency F
FROM temp1
WHERE DATEDIFF('2020-01-31', User active date )<=30
GROUP BY  The user id 
),
temp5 AS (--  Find out the user number and consumption amount M
SELECT  The user id ,SUM( Consumption amount ) AS  Total consumption amount M
FROM temp1
WHERE DATEDIFF('2020-01-31', User active date )<=30
GROUP BY  The user id 
),
temp6 AS(--  Summarize user numbers and RFM
SELECT temp3. The user id ,  The last consumption interval R AS R, Consumption frequency F AS F, Total consumption amount M AS M FROM temp3
JOIN temp4 ON temp3. The user id =temp4. The user id 
JOIN temp5 ON temp3. The user id =temp5. The user id 
),
temp7 AS (--  For each user RFM Scoring 
SELECT *,
CASE WHEN R>15 THEN 1
WHEN R>10 AND R<=15 THEN 2
WHEN R>5 AND R<=10 THEN 3
WHEN R>3 AND R<=5 THEN 4
ELSE 5 END AS R branch ,
CASE WHEN F<=1 THEN 1
WHEN F>1 AND F<=3 THEN 2
WHEN F>3 AND F<=5 THEN 3
WHEN F>5 AND F<=7 THEN 4
ELSE 5 END AS F branch ,
CASE WHEN M<=500 THEN 1
WHEN M>500 AND M<=1000 THEN 2
WHEN M>1000 AND M<=1500 THEN 3
WHEN M>1500 AND M<=2000 THEN 4
ELSE 5 END AS M branch 
FROM temp6
),
temp8 AS (--  Find out RFM The average of the scores 
SELECT AVG(R branch ) AS R Average score ,AVG(F branch ) AS F Average score ,AVG(M branch ) AS M Average score 
FROM temp7
),
temp9 AS (--  Put each user's RFM Compare with the corresponding average 
SELECT temp7. The user id ,
CASE WHEN R branch >R Average score  THEN ' high ' ELSE ' low ' END AS R classification ,
CASE WHEN F branch >F Average score  THEN ' high ' ELSE ' low ' END AS F classification ,
CASE WHEN M branch >M Average score  THEN ' high ' ELSE ' low ' END AS M classification 
FROM temp7
JOIN temp8
),
temp10 AS (--  Match user categories 
SELECT  The user id ,
CASE WHEN R classification =' high ' AND F classification =' high ' AND M classification =' high ' THEN ' Important value users '
WHEN R classification =' high ' AND F classification =' low ' AND M classification =' high ' THEN ' Important value users '
WHEN R classification =' low ' AND F classification =' high ' AND M classification =' high ' THEN ' It's important to keep users '
WHEN R classification =' low ' AND F classification =' low ' AND M classification =' high ' THEN ' Important to keep users '
WHEN R classification =' high ' AND F classification =' high ' AND M classification =' low ' THEN ' General value users '
WHEN R classification =' high ' AND F classification =' low ' AND M classification =' low ' THEN ' General development users '
WHEN R classification =' low ' AND F classification =' high ' AND M classification =' low ' THEN ' General user retention '
WHEN R classification =' low ' AND F classification =' low ' AND M classification =' low ' THEN ' Generally retain users '
END AS  The user classification 
FROM temp9
)
SELECT * FROM temp10

give the result as follows :
 Insert picture description here
According to the analysis , You can carry out fine operation for users ~

原网站

版权声明
本文为[Socialphobia_ FOGO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230355226786.html