MySQL 使用存储过程实现留存率

11 篇文章 0 订阅
1 篇文章 0 订阅
DELIMITER //


USE `resourcemanage-dev`//


DROP PROCEDURE IF EXISTS `stat_remain_player`//


CREATE DEFINER=`root`@`localhost` PROCEDURE `stat_remain_player`()
BEGIN
-- 今天的日期
DECLARE today DATE DEFAULT CURDATE();
DECLARE yesterday DATE DEFAULT DATE_SUB(today, INTERVAL 1 DAY);
DECLARE days_ago_2 DATE DEFAULT DATE_SUB(today, INTERVAL 2 DAY);
DECLARE days_ago_3 DATE DEFAULT DATE_SUB(today, INTERVAL 3 DAY);
DECLARE days_ago_4 DATE DEFAULT DATE_SUB(today, INTERVAL 4 DAY);
DECLARE days_ago_6 DATE DEFAULT DATE_SUB(today, INTERVAL 6 DAY);
DECLARE days_ago_7 DATE DEFAULT DATE_SUB(today, INTERVAL 7 DAY);
DECLARE days_ago_13 DATE DEFAULT DATE_SUB(today, INTERVAL 13 DAY);
DECLARE days_ago_14 DATE DEFAULT DATE_SUB(today, INTERVAL 14 DAY);
DECLARE days_ago_29 DATE DEFAULT DATE_SUB(today, INTERVAL 29 DAY);
DECLARE days_ago_30 DATE DEFAULT DATE_SUB(today, INTERVAL 30 DAY);
-- 统计昨天DRU(就是昨天一天的注册人数)
INSERT INTO stat_remain(dru, stat_time, add_time) SELECT COUNT(uid) , DATE_SUB(CURDATE(), INTERVAL 1 DAY), NOW() FROM t_ar_role WHERE  latelyLogin>'2000-01-01' AND DATE BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND CURDATE();
-- 修改前天的2日留存
UPDATE stat_remain SET second_day = (
   (SELECT COUNT(DISTINCT uid) FROM t_ar_role WHERE (DATE BETWEEN days_ago_2 AND yesterday) AND (latelyLogin BETWEEN yesterday AND today))
/ 
(SELECT COUNT(DISTINCT uid) FROM t_ar_role WHERE (DATE BETWEEN days_ago_2 AND yesterday))
) WHERE stat_time = days_ago_2;
-- 修改大前天的3日留存
UPDATE stat_remain SET third_day = (
SELECT(
   (SELECT COUNT(DISTINCT uid) FROM t_ar_role WHERE (DATE BETWEEN days_ago_3 AND days_ago_2) AND (latelyLogin BETWEEN yesterday AND today))
/ 
(SELECT COUNT(DISTINCT uid) FROM t_ar_role WHERE (DATE BETWEEN days_ago_3 AND days_ago_2))
)
) WHERE stat_time = days_ago_3;
-- 7日留存
UPDATE stat_remain SET seventh_day = (
SELECT(
   (SELECT COUNT(DISTINCT uid) FROM t_ar_role WHERE (DATE BETWEEN days_ago_7 AND days_ago_6) AND (latelyLogin BETWEEN yesterday AND today))
/ 
(SELECT COUNT(DISTINCT uid) FROM t_ar_role WHERE (DATE BETWEEN days_ago_7 AND days_ago_6))
)
) WHERE stat_time = days_ago_7;
-- 14日留存
UPDATE stat_remain SET fourteen_day = (
SELECT(
   (SELECT COUNT(DISTINCT uid) FROM t_ar_role WHERE (DATE BETWEEN days_ago_14 AND days_ago_13) AND (latelyLogin BETWEEN yesterday AND today))
/ 
(SELECT COUNT(DISTINCT uid) FROM t_ar_role WHERE (DATE BETWEEN days_ago_14 AND days_ago_13))
)
) WHERE stat_time = days_ago_14;
-- 30日留存
UPDATE stat_remain SET thirtieth_day = (
SELECT(
   (SELECT COUNT(DISTINCT uid) FROM t_ar_role WHERE (DATE BETWEEN days_ago_30 AND days_ago_29) AND (latelyLogin BETWEEN yesterday AND today))
/ 
(SELECT COUNT(DISTINCT uid) FROM t_ar_role WHERE (DATE BETWEEN days_ago_30 AND days_ago_29))
)
) WHERE stat_time = days_ago_30;
END//


DELIMITER ;

调用留存率:

call stat_remain_player();

留存率的存取过程,

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

家有小辉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值