#付费预估表
DELIMITER//
DROP PROCEDURE IF EXISTS `付费用户预估`;
CREATE PROCEDURE `付费用户预估`(IN date1 DATE,IN date2 DATE)
BEGIN
-- 当月订购来源明细、当月启动明细
DECLARE month_str1 VARCHAR(10);
DECLARE table_order VARCHAR(20);
DECLARE table_operate VARCHAR(20);
set @month_str1 = DATE_FORMAT(date2,'%y_%m');
set @table_order = '_订购明细';
set @table_operate = '_启动明细';
set @table_order = CONCAT(@month_str1,@table_order);
set @table_operate = CONCAT(@month_str1,@table_operate);
set @current_month_order0 = CONCAT('DROP TABLE IF EXISTS ',@table_order,';');
set @current_month_order = CONCAT('create table ',@table_order,' SELECT `订购日期`,`用户ID`,`订购时间`,`订购来源`,`所属CP`,`订购产品`,`历史订购成功次数` FROM `订购来源明细` WHERE `订购日期`>="',date1,'" AND `订购日期`<="',date2,'";');
set @current_month_operate0 = CONCAT('DROP TABLE IF EXISTS ',@table_operate,';');
set @current_month_operate = CONCAT('create table ',@table_operate,' SELECT `时间`,`帐号ID`,`游戏ID`,`游戏名称`,`所属CP`,`游戏时长(秒)`,`启动次数`,`订购产品`,`游戏是否收费` FROM `启动明细` WHERE `时间`>="',date1,'" AND `时间`<="',date2,'";');
set @od_index = CONCAT("create index dt1 on ",@table_operate,'(`帐号ID`);');
set @op_index = CONCAT("create index id1 on ",@table_operate,'(`时间`);');
PREPARE sql1 FROM @current_month_order;
PREPARE sql2 FROM @current_month_operate;
PREPARE sql1_0 FROM @current_month_order0;
PREPARE sql2_0 FROM @current_month_operate0;
PREPARE sql3 FROM @od_index;
PREPARE sql4 FROM @op_index;
EXECUTE sql1_0;
EXECUTE sql2_0;
EXECUTE sql1;
EXECUTE sql2;
EXECUTE sql3;
EXECUTE sql4;
DEALLOCATE prepare sql1_0;
DEALLOCATE prepare sql2_0;
DEALLOCATE prepare sql1;
DEALLOCATE prepare sql2;
DEALLOCATE prepare sql3;
DEALLOCATE prepare sql4;
-- 当月活跃用户ID,创建当月活跃用户ID订购记录表
set @a_1 = '_活跃用户订购记录';
set @biao_1 = CONCAT(@month_str1,@a_1);
set @current_active_userid0 = CONCAT('DROP TABLE IF EXISTS ',@biao_1,';');
set @current_active_userid = CONCAT('create table ',@biao_1,' as SELECT `订购日期`,`用户ID`,`订购时间`,`订购来源`,`所属CP`,`订购产品`,`历史订购成功次数` FROM `订购来源明细` WHERE `用户ID` IN (SELECT DISTINCT `帐号ID` FROM ',@table_operate,');');
PREPARE sql3_0 FROM @current_active_userid0;
PREPARE sql3 FROM @current_active_userid;
EXECUTE sql3_0;
EXECUTE sql3;
DEALLOCATE PREPARE sql3_0;
DEALLOCATE PREPARE sql3;
-- 当月付费预估表
set @a_2 = '_cp分成预估1';
set @biao_2 = CONCAT(@month_str1,@a_2);
set @order_source1_0 = CONCAT('DROP TABLE IF EXISTS ',@biao_2,';');
set @order_source1 = CONCAT('CREATE TABLE ',@biao_2,' AS SELECT a.*,a.`总订购次数`-f.`当月订购次数` AS "当月前订购次数",f.`当月订购次数`,f.`订购来源`,f.`当月最后订购日期` FROM
(SELECT g.`用户ID`,g.`订购产品`,count(1) "总订购次数" FROM (SELECT * FROM ',@biao_1,' WHERE `订购日期`<="',date2,'") g GROUP BY g.`用户ID`,g.`订购产品` ) a
LEFT JOIN
(SELECT b.`用户ID`,b.`订购产品`,b.`当月订购次数`,c.`所属CP` "订购来源",b.`当月最后订购日期` FROM
(SELECT `用户ID`,`订购产品`,COUNT(1) "当月订购次数",MAX(`订购日期`) "当月最后订购日期" FROM ',@table_order,' GROUP BY `用户ID`,`订购产品`)b
LEFT JOIN ',@table_order,' c
ON b.`用户ID`=c.`用户ID` AND b.`订购产品`=c.`订购产品` AND b.`当月最后订购日期`=c.`订购日期`) f
ON a.`用户ID`=f.`用户ID` AND a.`订购产品`=f.`订购产品`;');
PREPARE sql4_0 FROM @order_source1_0;
PREPARE sql4 FROM @order_source1;
EXECUTE sql4_0;
EXECUTE sql4;
DEALLOCATE PREPARE sql4_0;
DEALLOCATE PREPARE sql4;
SET @update_历史订购次数 = CONCAT("UPDATE ",@biao_2,' SET `当月前订购次数` = `总订购次数`-`当月订购次数` WHERE ISNULL(`当月前订购次数`);');
PREPARE sql5 FROM @update_历史订购次数;
EXECUTE sql5;
DEALLOCATE PREPARE sql5;
END//
DELIMITER;
#2、用户启动及时长计算
DELIMITER//
DROP PROCEDURE IF EXISTS `用户启动及时长计算`;
CREATE PROCEDURE `用户启动及时长计算`(IN date1 DATE,IN date2 DATE)
BEGIN
DECLARE month_str1 VARCHAR(10);
set @year_str = YEAR(date2);
set @month_str1 = DATE_FORMAT(date2,'%y_%m');
set @month_str2 = MONTH(date2);
set @biao1_0 = '_活跃用户ID';
set @CP_sj_0 = '_CP1用户启动及时长表';
set @CP_yh_0 = '_CP2用户启动及时长表';
set @CP_dp_0 = '_CP2用户启动及时长表';
set @CP_qdsc_0 = '_用户启动及时长计算';
set @biao1 = CONCAT(@month_str1,@biao1_0);
set @CP_sj = CONCAT(@month_str1,@CP_sj_0);
set @CP_yh = CONCAT(@month_str1,@CP_yh_0);
set @CP_dp = CONCAT(@month_str1,@CP_dp_0);
set @CP_qdsc = CONCAT(@month_str1,@CP_qdsc_0);
#当月活跃用户ID
set @sql1_0 = CONCAT('DROP TABLE IF EXISTS ',@biao1,';');
set @sql1 = CONCAT('CREATE TABLE ',@biao1,' SELECT a.`帐号ID` "账号ID" FROM (SELECT distinct `帐号ID` FROM `启动明细_月防刷筛选` WHERE `年`=YEAR("',date1,'") AND `月`>=MONTH("',date1,'") AND `月`<=MONTH("',date2,'"))a;');
#CP1表
set @sql2_0 = CONCAT('DROP TABLE IF EXISTS ',@CP_sj,';');
set @sql2 = CONCAT('CREATE TABLE ',@CP_sj,' SELECT c.`帐号ID` "帐号ID1",SUM(c.`启动次数`) "CP1启动次数",SUM(c.`访问时间`) "CP1启动时长" FROM (SELECT * FROM `启动明细_月防刷筛选` WHERE `年`=YEAR("',date1,'") AND `月`>=MONTH("',date1,'") AND `月`<=MONTH("',date2,'") AND `所属CP`="XJ GAMES")c GROUP BY c.`帐号ID`;');
#CP2表
set @sql3_0 = CONCAT('DROP TABLE IF EXISTS ',@CP_yh,';');
set @sql3 = CONCAT('CREATE TABLE ',@CP_yh,' SELECT c.`帐号ID` "帐号ID2",SUM(c.`启动次数`) "CP2启动次数",SUM(c.`访问时间`) "CP2启动时长" FROM (SELECT * FROM`启动明细_月防刷筛选` WHERE `年`=YEAR("',date1,'") AND `月`>=MONTH("',date1,'") AND `月`<=MONTH("',date2,'") AND `所属CP`="CP2")c GROUP BY c.`帐号ID`;');
#CP2表
set @sql4_0 = CONCAT('DROP TABLE IF EXISTS ',@CP_dp,';');
set @sql4 = CONCAT('CREATE TABLE ',@CP_dp,' SELECT c.`帐号ID` "帐号ID3",SUM(c.`启动次数`) "CP2启动次数",SUM(c.`访问时间`) "CP2启动时长" FROM (SELECT * FROM`启动明细_月防刷筛选` WHERE `年`=YEAR("',date1,'") AND `月`>=MONTH("',date1,'") AND `月`<=MONTH("',date2,'") AND `所属CP`="CP2互动")c GROUP BY c.`帐号ID`;');
PREPARE sql1_0 FROM @sql1_0;
PREPARE sql1 FROM @sql1;
PREPARE sql2_0 FROM @sql2_0;
PREPARE sql2 FROM @sql2;
PREPARE sql3_0 FROM @sql3_0;
PREPARE sql3 FROM @sql3;
PREPARE sql4_0 FROM @sql4_0;
PREPARE sql4 FROM @sql4;
EXECUTE sql1_0;
EXECUTE sql1;
EXECUTE sql2_0;
EXECUTE sql2;
EXECUTE sql3_0;
EXECUTE sql3;
EXECUTE sql4_0;
EXECUTE sql4;
DEALLOCATE PREPARE sql1_0;
DEALLOCATE PREPARE sql1;
DEALLOCATE PREPARE sql2_0;
DEALLOCATE PREPARE sql2;
DEALLOCATE PREPARE sql3_0;
DEALLOCATE PREPARE sql3;
DEALLOCATE PREPARE sql4_0;
DEALLOCATE PREPARE sql4;
#表连接
set @sql5_0 = CONCAT('DROP TABLE IF EXISTS ',@CP_qdsc,';');
set @sql5 = CONCAT('CREATE TABLE ',@CP_qdsc,' SELECT format(e.`账号ID`,0) "账号ID",e.`CP1启动次数`,e.`CP2启动次数`,f.`CP2启动次数`,e.`CP1启动时长`,e.`CP2启动时长`,f.`CP2启动时长` FROM
(SELECT c.*,d.* FROM (SELECT a.`账号ID`,b.* FROM ',@biao1,' a LEFT JOIN ',@CP_sj,' b ON a.`账号ID`=b.`帐号ID1`)c
LEFT JOIN ',@CP_yh,' d ON c.`账号ID`=d.`帐号ID2`)e
LEFT JOIN ',@CP_dp,' f ON e.`账号ID`=f.`帐号ID3`;');
PREPARE sql5_0 FROM @sql5_0;
PREPARE sql5 FROM @sql5;
EXECUTE sql5_0;
EXECUTE sql5;
DEALLOCATE PREPARE sql5_0;
DEALLOCATE PREPARE sql5;
END//
DELIMITER;