很簡單的sql 用戶分析語句 :只要自定義簡單的udf函數 獲取統計時間createdatms字段的
使用的日歷類 add方法 和simpledateformat 將long類型的 定義多個重載方法 獲取返回值int類型 或者long類型 進行時間判斷即可
getdaybegin(天開始),比如2017-08-08這一天的createtime為15288888888888 獲取到 152888880000(代表20170808 00:00:00)當天開始的凌晨
getWeekbegin,getMonthgin 同上道理
1.過去的五周(包含本周)某個app每周的周活躍用戶數 2 注意,如果能夠界定分區區間的話,務必要進行分區限定查詢。 3 20170501 4 ym/day/hm 5 //過去的五周,每周的活躍數 6 select formattime(createdatms,'yyyyMMdd',0) stdate, count(distinct deviceid) stcount from ext_startup_logs where concat(ym,day)>=formattime(getweekbegin(-4),'yyyyMMdd') and appid ='sdk34734' group by formattime(createdatms,'yyyyMMdd',0) ; 7 2.最近的六個月(包含本月)每月的月活躍數。 8 select formattime(createdatms,'yyyyMM') stdate, count(distinct deviceid) stcount from ext_startup_logs where ym >= formattime(getmonthbegin(-5),'yyyyMM') and appid ='sdk34734' group by formattime(createdatms,'yyyyMM') ; 9 3.沉默用戶數 10 3.1)查詢今天沉默用戶數 //某個設備 啟動時間 在今天(本周、本月) 只有一次 ,后續在無啟動 11 select count(*) from (select deviceid , count(createdatms) dcount,min(createdatms) dmin from ext_startup_logswhere appid = 'sdk34734' group by deviceid having dcount = 1 and min(createdatms) > getdaybegin(-1)) t 12 4.啟動次數 13 4.1)今天app的啟動次數 14 啟動次數類似於活躍用戶數,活躍用戶數去重,啟動次數不需要去重。 15 select count(*) from ext_startup_logs where appid = 'sdk34734' and ym = formattime(getdaybegin(),'yyyyMM') and day = formattime(getdaybegin(),'dd'); 16 5.版本分布 17 5.1)今天appid為34734的不同版本的活躍用戶數。 18 select appversion,count(distinct deviceid) from ext_startup_logs where appid = 'sdk34734' and ym = formattime(getdaybegin(),'yyyyMM') and day = formattime(getdaybegin(),'dd') group by appversion ; 19 20 5.2)本周內每天各版本日活 21 select formattime(createdatms,'yyyyMMdd'),appversion , count(distinct deviceid) from ext_startup_logs where appid = 'sdk34734' and concat(ym,day) >= formattime(getweekbegin(),'yyyyMMdd') group by formattime(createdatms,'yyyyMMdd') , appversion 22 23 24 [用戶構成分析] 25 1.本周回流用戶 上周未啟動,本周啟動了的用 必須當使用not in 子查詢和后續查詢都必須加入別名 26 select 27 distinct a.deviceid 28 from ext_startup_logs a 29 where a.appid = 'sdk34734' and concat(a.ym,a.day) >= formattime(getweekbegin(),'yyyyMMdd') and a.deviceid not in ( 30 select 31 distinct t.deviceid 32 from ext_startup_logs t 33 where t.appid = 'sdk34734' and concat(t.ym,t.day) >= formattime(getweekbegin(-1),'yyyyMMdd') and concat(t.ym,t.day) < formattime(getweekbegin(),'yyyyMMdd') 34 ) 35 36 2.連續活躍n周 連續三周活躍 2018101 20181008 20181016 去掉重有三次就是活躍 37 select deviceid , count(distinct(formattime(createdatms,'yyyyMMdd',0))) c from ext_startup_logs where appid = 'sdk34734' and concat(ym,day) >= formattime(getweekbegin(-2),'yyyyMMdd') group by deviceid having c = 3 38 39 3.忠誠用戶 連續活躍5周的 40 select deviceid , count(distinct(formattime(createdatms,'yyyyMMdd',0))) c from ext_startup_logs where appid = 'sdk34734' and concat(ym,day) >= formattime(getweekbegin(-4),'yyyyMMdd') group by deviceid having c = 5 41 42 4.連續活躍用戶 連續活躍n周 43 select deviceid , count(distinct(formattime(createdatms,'yyyyMMdd',0))) c from ext_startup_logs where appid = 'sdk34734' and concat(ym,day) >= formattime(getweekbegin(-1),'yyyyMMdd') group by deviceid having c = 2 44 45 46 select distinct(a.deviceid) from ext_startup_logs a where concat(a.ym,a.day) < formattime(getweekbegin(-4),'yyyyMMdd') and deviceid not in ( select distinct(t.deviceid) from ext_startup_logs t where concat(t.ym,t.day)>=formattime(getweekbegin(-4),'yyyyMMdd')) 47 48 5.近期流失用戶 49 最近2、3、4都沒有啟動過app. 50 查詢所有用戶訪問的時間的max,max不能落在 51 //四周內流失 52 select 53 distinct(deviceid) 54 from ext_startup_logs 55 where appid='#' 56 and concat(ym,day) >= formattime(getweekbegin(-4),'yyyyMMdd') 57 and concat(ym,day) < formattime(getweekbegin(-3),'yyyyMMdd') 58 and deviceid not in ( 59 select 60 distinct(t.deviceid) 61 from ext_startup_logs t 62 where t.appid='' 63 and concat(t.ym,t.day) >= formattime(getweekbegin(-3),'yyyyMMdd') 64 65 ) 66 union 67 //三周內流失 68 select 69 distinct(deviceid) 70 from ext_startup_logs 71 where appid='#' 72 and concat(ym,day) >= formattime(getweekbegin(-3),'yyyyMMdd') 73 and concat(ym,day) < formattime(getweekbegin(-2),'yyyyMMdd') 74 and deviceid not in ( 75 select 76 distinct(t.deviceid) 77 from ext_startup_logs t 78 where t.appid='' 79 and concat(t.ym,t.day) >= formattime(getweekbegin(-2),'yyyyMMdd') 80 81 ) 82 union 83 //兩周內流失 84 select 85 distinct(deviceid) 86 from ext_startup_logs 87 where appid='#' 88 and concat(ym,day) >= formattime(getweekbegin(-2),'yyyyMMdd') 89 and concat(ym,day) < formattime(getweekbegin(-1),'yyyyMMdd') 90 and deviceid not in ( 91 select 92 distinct(t.deviceid) 93 from ext_startup_logs t 94 where t.appid='' 95 and concat(t.ym,t.day) >= formattime(getweekbegin(-1),'yyyyMMdd') 96 ) 97 98 99 100 [留存分析] 101 1.留存用戶 102 周留存用戶。上周新增的用戶在本周還使用的 103 select 104 distinct(a.deviceid) 105 from ext_startup_logs a 106 where a.appid = 'sdk34734' 107 and concat(a.ym,a.day) >= formattime(getweekbegin(-1),'yyyyMMdd') 108 and concat(a.ym,a.day) < formattime(getweekbegin(),'yyyyMMdd') 109 and a.deviceid in ( 110 select distinct(t.deviceid) 111 from ( 112 select tt.deviceid , min(tt.createdatms) mintime 113 from ext_startup_logs tt 114 where tt.appid = 'sdk34734' 115 group by tt.deviceid having mintime >= getweekbegin(-2) and mintime < getweekbegin(-1) 116 ) t 117 ) 118 119 120 121 122 2.用戶的新鮮度 123 新鮮度 = 某段時間的新增用戶數/某段時間的活躍的老用戶數 . 124 //今天活躍用戶 125 126 m = select count(distinct(t.deviceid)) 127 from ext_startup_logs where concat(ym,day) = formattime(getdaybegin(),'yyyyMMdd') and appid = ... ; 128 //今天新增用戶 129 n = select count(distinct(t.deviceid)) 130 from ( 131 select tt.deviceid , min(tt.createdatms) mintime 132 from ext_startup_logs tt 133 where tt.appid = 'sdk34734' 134 group by tt.deviceid having mintime >= getdaybegin(0) 135 ) t