用戶頻次分布、新增用戶留存、活躍用戶留存
select * from a WHERE a.field1 NOT IN (select field1 from b)
select * from a WHERE NOT EXISTS (select 1 from b where a.field1 = b.field1)
表a的條件加在最後,表b的條件加在括弧中。
select id from aa left join bb on aa.id=bb.id and bb.id is null
select count(uid) as onl,date_format(time,‘%H‘) as hour from d_user_login201704 where id in (select * from ((select min(id) from d_user_login201704 where type=0 and time>= ‘2017-04-09 00:00:00‘ and time<=‘2017-04-09 23:59:59‘ group by(uid)) as tmptable)) group by hour;
--用戶頻次分布
select a.次數,count(a.userid)
from (select userid,count(userid)as 次數 from t_orderid where dt=‘10月29日’ order by userid)a
group by a.次數
--新增用戶留存、活躍用戶留存不會
select cityid,dt,count(a.userid)as cou
from (select userid from t_user where dt=‘2016年10月1日‘)a inner join(select cityid,dt,userid from t_order where dt>=‘2016年10月1日‘ and dt<=‘2016年12月1日‘)b
on a.userid=b.userid
group by city,dt
select * from t_user where dt between ‘2016-10-01‘ and ‘2016-12-01‘ ----新增用戶
要看相對於哪個時間段的存留,比如,跟這個月(2017-06-01-2017-06-30)比
select cityid,count(distinct userid) from t_order
where userid iN (select userid from t_user where dt between ‘2016-10-01‘ and ‘2016-12-01‘ )
and dt between 2017-06-01 and 2017-06-30
group by cityid
活躍用戶
select userid fromt_order
where dt between ‘2016-10-01‘ and ‘2016-12-01‘
and userid in (select userid from t_user where dt< ‘2016-10-01‘ )
---2016.10之前註冊的,並且在-這個時間段的有訂單的用戶是活躍的,然後 對比 哪個區間 他還在不在
select cityid,count(distinct userid) from t_order
where userid iN (select userid fromt_order
where dt between ‘2016-10-01‘ and ‘2016-12-01‘
and userid in (select userid from t_user where dt< ‘2016-10-01‘ )
)
and dt between 2017-06-01 and 2017-06-30
group by cityid
--單均價、人均單、車均單
select cityid,sum(balance)/count(orderid) as 單均價,count(orderid)/count(distinct userid) as 人均單,count(orderid)/count(distinct carid) as 車均單,
from t_order inner join t_price on t_order.orderid=t_price.orderid
group by cityid
本文出自 “百合花開” 博客,謝絕轉載!
用戶頻次分布、新增用戶留存、活躍用戶留存