MySQL计算销售员昨日各指标综合得分_20161206

时间:2022-03-28 19:26:49

MySQL计算销售员昨日各指标综合得分_20161206

数据需求如上,新增重激活以15天未下单为界定

SELECT d.销售员,(日销售额得分*0.6+日新增客户数得分*0.15+日客单价得分*0.1+日客户平均产品数得分*0.15)AS 综合得分,日销售额,日客单价,日新增客户数 AS 日新增客户数,日客户平均产品数
FROM (#添加得分指标
SELECT c.*
,CASE WHEN 日销售额>=10000 THEN 100 WHEN 日销售额>=4000 AND 日销售额<10000 THEN 60+40/6000*(日销售额-4000) WHEN 日销售额<4000 THEN 0+60/4000* (4000-日销售额) ELSE NULL END AS 日销售额得分
,CASE WHEN 日客单价>=600 THEN 100 WHEN 日客单价>=450 AND 日客单价<600 THEN 60+40/150*(日客单价-450) WHEN 日客单价>=360 AND 日客单价<450 THEN 0+60/90*(450-日客单价) ELSE 0 END AS 日客单价得分
,CASE WHEN 日客户平均产品数>=6 THEN 100 WHEN 日客户平均产品数>=3 AND 日客户平均产品数<6 THEN 60+40/3*(日客户平均产品数-3) WHEN 日客户平均产品数>=1 AND 日客户平均产品数<3 THEN 0+60/2*(3-日客户平均产品数) ELSE 0 END AS 日客户平均产品数得分
,CASE WHEN 日新增客户数>=8 THEN 100 WHEN 日新增客户数>=2 AND 日新增客户数<8 THEN 60+40/6*(日新增客户数-2) WHEN 日新增客户数<2 THEN 0+60/2*(2-日新增客户数) ELSE 0 END AS 日新增客户数得分
FROM (#明细销售员日销售额 新增激活客户数 客单价 日客户平均产品数
SELECT a.*,IFNULL(b.日新增客户数,0) AS 日新增客户数
FROM (
SELECT 销售员,SUM(金额) AS 日销售额,SUM(金额)/COUNT(用户ID) AS 日客单价,AVG(产品数) AS 日客户平均产品数
FROM (
SELECT 销售员,用户ID,COUNT(产品ID) AS 产品数,SUM(金额) AS 金额
FROM (
SELECT 销售员,用户ID,产品ID,SUM(金额) AS 金额
FROM `a003_order`
WHERE 城市='郑州' AND DATE(订单日期)=DATE_ADD(CURRENT_DATE,INTERVAL -1 DAY) AND 金额>0
GROUP BY 销售员,用户ID,产品ID
) AS a1
GROUP BY 销售员,用户ID
) AS a2
GROUP BY 销售员
ORDER BY SUM(金额) DESC
) AS a
LEFT JOIN (
SELECT 销售员,COUNT(用户ID) AS 日新增客户数
FROM (
SELECT b1.用户ID,b1.用户名称,昨日以前15天之前金额,昨日以前15天金额,昨日金额,b2.销售员,CASE WHEN 昨日以前15天之前金额 IS NULL AND 昨日以前15天金额 IS NULL THEN "新增" WHEN 昨日以前15天之前金额 IS NOT NULL AND 昨日以前15天金额 IS NULL THEN "重激活" WHEN 昨日以前15天金额 IS NOT NULL THEN "留存" ELSE NULL END 留存情况
FROM (
SELECT 用户ID,用户名称,SUM(IF(DATE(订单日期)=DATE_ADD(CURRENT_DATE,INTERVAL -1 DAY),金额,NULL)) AS 昨日金额,
SUM(IF(DATE(订单日期)>=DATE_ADD(CURRENT_DATE,INTERVAL -16 DAY) AND DATE(订单日期)<DATE_ADD(CURRENT_DATE,INTERVAL -1 DAY),金额,NULL)) AS 昨日以前15天金额,
SUM(IF(DATE(订单日期)<DATE_ADD(CURRENT_DATE,INTERVAL -16 DAY),金额,NULL)) AS 昨日以前15天之前金额
FROM a003_order
WHERE 城市="郑州" AND 金额>0 AND 用户ID IN (SELECT 用户ID FROM a003_order AS a WHERE 城市="郑州" AND DATE(订单日期)=DATE_ADD(CURRENT_DATE,INTERVAL -1 DAY) AND 金额>0 GROUP BY 用户ID)
GROUP BY 用户ID
) AS b1
LEFT JOIN a003b_order_item AS b2 ON b1.用户ID=b2.用户ID
)AS b3
WHERE 留存情况<>'留存'
GROUP BY 销售员
) AS b ON a.销售员=b.销售员
) AS c
) AS d