奶粉运营,跑数据三个模板。
Posted sakura3
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了奶粉运营,跑数据三个模板。相关的知识,希望对你有一定的参考价值。
#控奶复购:T-37至T-7内购买大于等于2听,且T-14至T-7内购买大于等于1听 #T为1月13日 #删除 DELETE FROM `1208-0106控奶复购`; #写入会员基本信息 INSERT INTO `1208-0106控奶复购` ( 门店ID, 门店, 会员姓名, 手机号码, 控奶首购时间 ) SELECT * FROM ( SELECT a.salesdepart_id AS 门店ID, b.`name` AS 门店, c.`name` AS 会员姓名, c.mobile AS 手机号码, MIN(d.create_date) AS `控奶首购时间` FROM arm_changsha.goods_sales AS a LEFT JOIN arm_changsha.sys_office AS b ON b.id = a.salesdepart_id LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = a.member_id LEFT JOIN arm_changsha.goods_sales_detail AS d ON d.sales_id = a.id LEFT JOIN arm_changsha.goods_base AS e ON e.id = d.goods_id WHERE a.sales_type = ‘1‘ AND c.mobile IS NOT NULL AND d.barcode IN ( SELECT 条码 FROM `tmp_通货` WHERE 商品标签 = ‘非通货‘ AND 大类 LIKE ‘01%‘ ) GROUP BY c.mobile ) AS L WHERE `控奶首购时间` >= ‘2018-12-08 00:00:00‘ AND `控奶首购时间` <= ‘2019-01-06 23:59:59‘; #写入T-37至T-7内购买数量 UPDATE `1208-0106控奶复购` AS a SET a.`T-37至T-7内购买数量` = ( SELECT sum(b.sales_number) AS 购买数量 FROM arm_changsha.goods_sales_detail AS b LEFT JOIN arm_changsha.goods_sales AS d ON d.id = b.sales_id LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = d.member_id WHERE c.mobile = a.手机号码 AND b.create_date >= ‘2018-12-08 00:00:00‘ AND b.create_date <= ‘2019-01-06 23:59:59‘ AND d.sales_type = ‘1‘ AND b.barcode IN ( SELECT 条码 FROM `tmp_通货` WHERE 商品标签 = ‘非通货‘ AND 大类 LIKE ‘01%‘ ) ); #写入T-14至T-7内购买数量 UPDATE `1208-0106控奶复购` AS a SET a.`T-14至T-7内购买数量` = ( SELECT sum(b.sales_number) AS 购买数量 FROM arm_changsha.goods_sales_detail AS b LEFT JOIN arm_changsha.goods_sales AS d ON d.id = b.sales_id LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = d.member_id WHERE c.mobile = a.手机号码 AND b.create_date >= ‘2018-12-31 00:00:00‘ AND b.create_date <= ‘2019-01-06 23:59:59‘ AND d.sales_type = ‘1‘ AND b.barcode IN ( SELECT 条码 FROM `tmp_通货` WHERE 商品标签 = ‘非通货‘ AND 大类 LIKE ‘01%‘ ) ); #删除T-14至T-7期间未购用户 DELETE FROM `1208-0106控奶复购` WHERE `T-14至T-7内购买数量`IS NULL; #写入T+7控货奶粉购买数量 UPDATE `1208-0106控奶复购` AS a SET a.`T+7控奶购买数量` = ( SELECT sum(b.sales_number) AS 购买数量 FROM arm_changsha.goods_sales_detail AS b LEFT JOIN arm_changsha.goods_sales AS d ON d.id = b.sales_id LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = d.member_id WHERE c.mobile = a.手机号码 AND b.create_date >= ‘2019-01-14 00:00:00‘ AND b.create_date <= ‘2019-01-20 23:59:59‘ AND d.sales_type = ‘1‘ AND b.barcode IN ( SELECT 条码 FROM `tmp_通货` WHERE 商品标签 = ‘非通货‘ AND 大类 LIKE ‘01%‘ ) ); #写入T+7复购金额 UPDATE `1208-0106控奶复购` AS a SET a.`T+7复购金额` = ( SELECT ROUND(SUM(b.real_pay) / 10000, 2) AS 累计消费金额 FROM arm_changsha.goods_sales AS b LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = b.member_id WHERE c.mobile = a.手机号码 AND b.create_date >= ‘2019-01-14 00:00:00‘ AND b.create_date <= ‘2019-01-20 23:59:59‘ AND b.sales_type = ‘1‘ GROUP BY b.member_id ); #写入T+7购买次数 UPDATE `1208-0106控奶复购` AS a SET a.`T+7小票数` = ( SELECT COUNT(DISTINCT b.sales_id) FROM arm_changsha.goods_sales_detail AS b LEFT JOIN arm_changsha.goods_sales AS d ON d.id = b.sales_id LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = d.member_id WHERE c.mobile = a.手机号码 AND b.create_date >= ‘2019-01-14 00:00:00‘ AND b.create_date <= ‘2019-01-20 23:59:59‘ AND d.sales_type = ‘1‘ ); #写入近90日充值金额 UPDATE `1208-0106控奶复购` AS a SET a.`近90日充值金额` = ( SELECT ROUND( sum(b.use_recharge_balance) / 10000, 2 ) AS 充值金额 FROM arm_changsha.ms_member_card_log AS b LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = b.member_id LEFT JOIN arm_changsha.ms_acct_book_def AS d ON d.member_id = b.member_id WHERE c.mobile = a.手机号码 AND d.acct_id IS NOT NULL AND b.oper_type IN (‘4‘, ‘7‘, ‘8‘) AND b.create_date >= ‘2018-10-16 00:00:00‘ AND b.create_date <= ‘2019-01-13 23:59:59‘ ); #写入近90天控货奶粉购买数量 UPDATE `1208-0106控奶复购` AS a SET a.`近90日控奶购买数量` = ( SELECT sum(b.sales_number) AS 购买数量 FROM arm_changsha.goods_sales_detail AS b LEFT JOIN arm_changsha.goods_sales AS d ON d.id = b.sales_id LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = d.member_id WHERE c.mobile = a.手机号码 AND b.create_date >= ‘2018-10-16 00:00:00‘ AND b.create_date <= ‘2019-01-13 23:59:59‘ AND d.sales_type = ‘1‘ AND b.barcode IN ( SELECT 条码 FROM `tmp_通货` WHERE 商品标签 = ‘非通货‘ AND 大类 LIKE ‘01%‘ ) ); #判断是否运营门店 UPDATE `1208-0106控奶复购` AS a SET a.`是否运营门店` = CASE WHEN a.门店ID IN ( ‘1008‘, ‘1016‘, ‘1025‘, ‘1026‘ ) THEN ‘是‘ ELSE ‘否‘ END; #判断是否稳定 UPDATE `1208-0106控奶复购` AS a SET a.`是否稳定` = CASE WHEN a.近90日控奶购买数量 >= ‘4‘ OR a.近90日充值金额 >= ‘500‘ THEN ‘是‘ ELSE ‘否‘ END;
控奶新购
#控奶新客:T-14至T-7内购买大于等于1,且T-14至T-194(最远到7月)未购控奶 #T为1月13日; DELETE FROM `1231-0106控奶新客`; #写入会员基本信息 INSERT INTO `1231-0106控奶新客` ( 门店ID, 门店, 会员姓名, 手机号码, 控奶首购时间 ) SELECT * FROM ( SELECT a.salesdepart_id AS 门店ID, b.`name` AS 门店, c.`name` AS 会员姓名, c.mobile AS 手机号码, MIN(d.create_date) AS `控奶首购时间` FROM arm_changsha.goods_sales AS a LEFT JOIN arm_changsha.sys_office AS b ON b.id = a.salesdepart_id LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = a.member_id LEFT JOIN arm_changsha.goods_sales_detail AS d ON d.sales_id = a.id LEFT JOIN arm_changsha.goods_base AS e ON e.id = d.goods_id WHERE a.sales_type = ‘1‘ AND d.barcode IN ( SELECT 条码 FROM `tmp_通货` WHERE 商品标签 = ‘非通货‘ AND 大类 LIKE ‘01%‘ ) AND c.mobile NOT IN ( SELECT mobile FROM `7~9月英克系统消费客户统计` ) AND c.mobile IS NOT NULL GROUP BY c.mobile ) AS L WHERE `控奶首购时间` >= ‘2018-12-31 00:00:00‘ AND `控奶首购时间` <= ‘2019-01-06 23:59:59‘; #写入首购购买数量 UPDATE `1231-0106控奶新客` AS a SET a.`控奶首购数量` = ( SELECT sum(b.sales_number) AS 购买数量 FROM arm_changsha.goods_sales_detail AS b LEFT JOIN arm_changsha.goods_sales AS d ON d.id = b.sales_id LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = d.member_id LEFT JOIN arm_changsha.goods_base AS e ON e.id = b.goods_id WHERE c.mobile = a.手机号码 AND b.create_date = a.控奶首购时间 AND e.kind_id LIKE ‘01%‘ AND d.sales_type = ‘1‘ AND b.barcode IN ( SELECT 条码 FROM `tmp_通货` WHERE 商品标签 = ‘非通货‘ AND 大类 LIKE ‘01%‘ ) ); #写入T+7控货奶粉购买数量 UPDATE `1231-0106控奶新客` AS a SET a.`T+7控奶购买数量` = ( SELECT sum(b.sales_number) AS 购买数量 FROM arm_changsha.goods_sales_detail AS b LEFT JOIN arm_changsha.goods_sales AS d ON d.id = b.sales_id LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = d.member_id WHERE c.mobile = a.手机号码 AND b.create_date >= ‘2019-01-14 00:00:00‘ AND b.create_date <= ‘2019-01-20 23:59:59‘ AND d.sales_type = ‘1‘ AND b.barcode IN ( SELECT 条码 FROM `tmp_通货` WHERE 商品标签 = ‘非通货‘ AND 大类 LIKE ‘01%‘ ) ); #写入T+7复购金额 UPDATE `1231-0106控奶新客` AS a SET a.`T+7复购金额` = ( SELECT ROUND(SUM(b.real_pay) / 10000, 2) AS 累计消费金额 FROM arm_changsha.goods_sales AS b LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = b.member_id WHERE c.mobile = a.手机号码 AND b.create_date >= ‘2019-01-14 00:00:00‘ AND b.create_date <= ‘2019-01-20 23:59:59‘ AND b.sales_type = ‘1‘ GROUP BY b.member_id ); #写入T+7购买次数 UPDATE `1231-0106控奶新客` AS a SET a.`T+7小票数` = ( SELECT COUNT(DISTINCT b.sales_id) FROM arm_changsha.goods_sales_detail AS b LEFT JOIN arm_changsha.goods_sales AS d ON d.id = b.sales_id LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = d.member_id WHERE c.mobile = a.手机号码 AND b.create_date >= ‘2019-01-14 00:00:00‘ AND b.create_date <= ‘2019-01-20 23:59:59‘ AND d.sales_type = ‘1‘ ); #写入近90日充值金额 UPDATE `1231-0106控奶新客` AS a SET a.`近90日充值金额` = ( SELECT ROUND( sum(b.use_recharge_balance) / 10000, 2 ) AS 充值金额 FROM arm_changsha.ms_member_card_log AS b LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = b.member_id LEFT JOIN arm_changsha.ms_acct_book_def AS d ON d.member_id = b.member_id WHERE c.mobile = a.手机号码 AND d.acct_id IS NOT NULL AND b.oper_type IN (‘4‘, ‘7‘, ‘8‘) AND b.create_date >= ‘2018-10-16 00:00:00‘ AND b.create_date <= ‘2019-01-13 23:59:59‘ ); #写入近90天控货奶粉购买数量 UPDATE `1231-0106控奶新客` AS a SET a.`近90日控奶购买数量` = ( SELECT sum(b.sales_number) AS 购买数量 FROM arm_changsha.goods_sales_detail AS b LEFT JOIN arm_changsha.goods_sales AS d ON d.id = b.sales_id LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = d.member_id WHERE c.mobile = a.手机号码 AND b.create_date >= ‘2018-10-16 00:00:00‘ AND b.create_date <= ‘2019-01-13 23:59:59‘ AND d.sales_type = ‘1‘ AND b.barcode IN ( SELECT 条码 FROM `tmp_通货` WHERE 商品标签 = ‘非通货‘ AND 大类 LIKE ‘01%‘ ) ); #判断是否运营门店 UPDATE `1231-0106控奶新客` AS a SET a.`是否运营门店` = CASE WHEN a.门店ID IN ( ‘1008‘, ‘1016‘, ‘1025‘, ‘1026‘ ) THEN ‘是‘ ELSE ‘否‘ END; #判断是否稳定 UPDATE `1231-0106控奶新客` AS a SET a.`是否稳定` = CASE WHEN a.近90日控奶购买数量 >= ‘4‘ OR a.近90日充值金额 >= ‘500‘ THEN ‘是‘ ELSE ‘否‘ END;
大润发测试:
DELETE FROM `大润发2月第二周`; #写入会员基本信息 INSERT INTO `大润发2月第二周` ( 门店ID, 会员姓名, 手机号码, 注册日期, 消费金额, 消费次数 ) SELECT a.salesdepart_id AS 门店ID, b.`name` AS 会员姓名, b.mobile AS 手机号码, b.create_date AS 注册日期, ROUND(a.real_pay / 10000, 2) AS 消费金额, COUNT(a.id) AS 消费次数 FROM arm_changsha.goods_sales AS a LEFT JOIN arm_changsha.ms_member_def AS b ON b.id = a.member_id WHERE b.create_date >= ‘2019-01-31 00:00:00‘ AND b.create_date <= ‘2019-02-13 23:59:59‘ AND a.salesdepart_id = ‘1008‘ AND a.sales_type = ‘1‘ GROUP BY b.mobile; /*随机一半人发券 UPDATE `大润发2月第二周` AS a INNER JOIN( SELECT * FROM `大润发2月第二周_copy` ORDER BY RAND(手机号码) LIMIT 0,49)b SET a.`是否发券` = ‘是‘ WHERE b.手机号码 = a.手机号码;*/ #补充发券日期 UPDATE `大润发2月第二周` AS a SET a.`发券日期` = ‘2019-01-14‘ WHERE a.`是否发券` = ‘是‘; #写入T+7复购金额 UPDATE `大润发2月第二周` AS a SET a.`T+7消费金额` = ( SELECT ROUND(SUM(b.real_pay) / 10000, 2) AS 累计消费金额 FROM arm_changsha.goods_sales AS b LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = b.member_id WHERE c.mobile = a.手机号码 AND b.create_date >= ‘2019-02-14 00:00:00‘ AND b.create_date <= ‘2019-02-20 23:59:59‘ AND b.sales_type = ‘1‘ GROUP BY b.member_id ); #写入T+7购买次数 UPDATE `大润发2月第二周` AS a SET a.`T+7消费次数` = ( SELECT COUNT(DISTINCT b.sales_id) FROM arm_changsha.goods_sales_detail AS b LEFT JOIN arm_changsha.goods_sales AS d ON d.id = b.sales_id LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = d.member_id WHERE c.mobile = a.手机号码 AND b.create_date >= ‘2019-02-14 00:00:00‘ AND b.create_date <= ‘2019-02-20 23:59:59‘ AND d.sales_type = ‘1‘ ); #写入T+14复购金额 UPDATE `大润发2月第二周` AS a SET a.`T+14消费金额` = ( SELECT ROUND(SUM(b.real_pay) / 10000, 2) AS 累计消费金额 FROM arm_changsha.goods_sales AS b LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = b.member_id WHERE c.mobile = a.手机号码 AND b.create_date >= ‘2019-02-14 00:00:00‘ AND b.create_date <= ‘2019-02-27 23:59:59‘ AND b.sales_type = ‘1‘ GROUP BY b.member_id ); #写入T+14购买次数 UPDATE `大润发2月第二周` AS a SET a.`T+14消费次数` = ( SELECT COUNT(DISTINCT b.sales_id) FROM arm_changsha.goods_sales_detail AS b LEFT JOIN arm_changsha.goods_sales AS d ON d.id = b.sales_id LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = d.member_id WHERE c.mobile = a.手机号码 AND b.create_date >= ‘2019-02-14 00:00:00‘ AND b.create_date <= ‘2019-02-27 23:59:59‘ AND d.sales_type = ‘1‘ ); #写入T+30复购金额 UPDATE `大润发2月第二周` AS a SET a.`T+30消费金额` = ( SELECT ROUND(SUM(b.real_pay) / 10000, 2) AS 累计消费金额 FROM arm_changsha.goods_sales AS b LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = b.member_id WHERE c.mobile = a.手机号码 AND b.create_date >= ‘2019-02-14 00:00:00‘ AND b.create_date <= ‘2019-03-15 23:59:59‘ AND b.sales_type = ‘1‘ GROUP BY b.member_id ); #写入T+30购买次数 UPDATE `大润发2月第二周` AS a SET a.`T+30消费次数` = ( SELECT COUNT(DISTINCT b.sales_id) FROM arm_changsha.goods_sales_detail AS b LEFT JOIN arm_changsha.goods_sales AS d ON d.id = b.sales_id LEFT JOIN arm_changsha.ms_member_def AS c ON c.id = d.member_id WHERE c.mobile = a.手机号码 AND b.create_date >= ‘2019-02-14 00:00:00‘ AND b.create_date <= ‘2019-03-15 23:59:59‘ AND d.sales_type = ‘1‘ );
以上是关于奶粉运营,跑数据三个模板。的主要内容,如果未能解决你的问题,请参考以下文章