1. 程式人生 > 資料庫 >metabase sql統計留存率

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