metabase sql统计留存率

 1 -- 上周注册了100人,上周以前注册(200人),
 2 -- 本周登录了50人(上周注册的100人里面有50人本周登录),
 3 -- 本周登录了20人(上周之前注册注册的200人里面有80人在上周登录过并且本周登录了20人)
 4 -- 新用户留存率 50/100
 5 -- 老用户留存率 20/80
 6 
 7 使用sql统计出新旧留存率:
 8 
 9 SELECT CONCAT(ROUND((MA.CNT3/MA.CNT1)*100,2),'%')  AS "新用户留存率",
10        CONCAT(ROUND((MA.CNT5/MA.CNT4)*100,2),'%') AS "老用户留存率"
11 FROM (SELECT
12         SUM(CASE WHEN A.CREATE_TIME BETWEEN DATE_SUB({{S_TIME}},INTERVAL WEEKDAY({{S_TIME}}) + 8 DAY) AND DATE_SUB({{S_TIME}},INTERVAL WEEKDAY({{S_TIME}}) + 2 DAY) -- 上周日至上周六
13                  THEN 1 ELSE 0 END ) AS CNT1, -- 上周注册数(新用户留存分母),
14         SUM(CASE WHEN A.CREATE_TIME < DATE_SUB({{S_TIME}},INTERVAL WEEKDAY({{S_TIME}}) + 8 DAY) -- 上周日
15                  THEN 1 ELSE 0 END ) AS CNT2,  -- 上周以前注册数
16         SUM(CASE WHEN A.CREATE_TIME BETWEEN DATE_SUB({{S_TIME}},INTERVAL WEEKDAY({{S_TIME}}) + 8 DAY) AND DATE_SUB({{S_TIME}},INTERVAL WEEKDAY({{S_TIME}}) + 2 DAY) -- 上周日至上周六
17                   AND EXISTS (SELECT 1 FROM FCOIN.SETTLEDETAILS B 
18                                WHERE B.SETTLEDATE BETWEEN DATE_SUB({{S_TIME}},INTERVAL WEEKDAY({{S_TIME}}) +1 DAY) AND DATE_SUB({{S_TIME}},INTERVAL WEEKDAY({{S_TIME}}) - 5 DAY) -- 本周日至本周六
19                                  AND B.USERID = A.ID) 
20                  THEN 1 ELSE 0 END) AS CNT3,  -- 上周注册的在本周登录(新用户留存分子)
21         SUM(CASE WHEN A.CREATE_TIME < DATE_SUB({{S_TIME}},INTERVAL WEEKDAY({{S_TIME}}) + 8 DAY) -- 上周日
22                   AND EXISTS (SELECT 1 FROM FCOIN.SETTLEDETAILS B  
23                                WHERE B.SETTLEDATE BETWEEN DATE_SUB({{S_TIME}},INTERVAL WEEKDAY({{S_TIME}}) + 8 DAY) AND DATE_SUB({{S_TIME}},INTERVAL WEEKDAY({{S_TIME}}) + 2 DAY) -- 本周日至本周六
24                                  AND B.USERID = A.ID)
25                  THEN 1 ELSE 0 END) AS CNT4,  -- 上周以前注册,上周登录过,本周也登录过(旧用户留存分母)
26         SUM(CASE WHEN A.CREATE_TIME < DATE_SUB({{S_TIME}},INTERVAL WEEKDAY({{S_TIME}}) + 8 DAY) -- 上周日
27                   AND EXISTS (SELECT 1 FROM FCOIN.SETTLEDETAILS B  
28                                WHERE B.SETTLEDATE BETWEEN DATE_SUB({{S_TIME}},INTERVAL WEEKDAY({{S_TIME}}) + 8 DAY) AND DATE_SUB({{S_TIME}},INTERVAL WEEKDAY({{S_TIME}}) + 2 DAY) -- 上周日至上周六
29                                  AND B.USERID = A.ID)
30                   AND EXISTS (SELECT 1 FROM FCOIN.SETTLEDETAILS B 
31                                WHERE B.SETTLEDATE BETWEEN DATE_SUB({{S_TIME}},INTERVAL WEEKDAY({{S_TIME}}) +1 DAY) AND DATE_SUB({{S_TIME}},INTERVAL WEEKDAY({{S_TIME}}) - 5 DAY) -- 本周日至本周六
32                                  AND B.USERID = A.ID) 
33                  THEN 1 ELSE 0 END) AS CNT5  -- 上周以前注册,上周登录过,本周也登录过(旧用户留存分子)
34    FROM ACCOUNT.USERS A
35   WHERE A.NATION<>'86' and A.account_role<50 and service_area<>'cn') MA
上一篇:vue 定时器和清除定时器


下一篇:两条水位线的业务需求分析-Interval JOIN方案(转载+自己分析整理)