select count(distinct UserID) "访客数",
(select count(*) from userbehavior where Behavior = "pv") "点击数",
ROUND((select count(*) from userbehavior where Behavior = "pv")/
count(distinct UserID), 2) "人均访问数" from userbehavior;
select 总用户数,只访问一次页面数,concat((只访问一次页面数 * 100) / (总用户数), "%") "跳出率"
from (select UserID,count(distinct UserID) "只访问一次页面数" from userbehavior
where UserID not in
(select distinct UserID from userbehavior where behavior ='fav')
and UserID not in
(select distinct UserID from userbehavior where behavior ='cart')
and UserID not in
(select distinct UserID from userbehavior where behavior ='buy')) as a
inner join (select UserID, count(distinct UserID) "总用户数" from userbehavior) as b
on a.UserID = b.UserID
select e.访客数,a.time_date,a.活跃点击量,b.收藏次数,c.加入购物车次数,d.购买次数 from
(select time_date, count(behavior) "活跃点击量"
from userbehavior
where behavior = "pv" group by time_date order by time_date) as a inner join
(select time_date, count(behavior) "收藏次数"
from userbehavior
where behavior = "fav" group by time_date order by time_date) as b inner join
(select time_date, count(behavior) "加入购物车次数"
from userbehavior
where behavior = "cart" group by time_date order by time_date) as c inner join
(select time_date, count(behavior) "购买次数"
from userbehavior
where behavior = "buy" group by time_date) as d join
(select time_date,count(distinct UserID) "访客数" from userbehavior
group by time_date order by time_date) as e
on a.time_date = b.time_date
and b.time_date = c.time_date
and c.time_date = d.time_date
and d.time_date = e.time_date;
select mid(time_hour,1,2) "时间",
sum(case when Behavior = "pv" then 1 else 0 end) "活跃点击量",
count(distinct UserID) "活跃用户数",
sum(case when Behavior = "fav" then 1 else 0 end) "收藏次数",
sum(case when Behavior = "cart" then 1 else 0 end) "加入购物车次数",
sum(case when Behavior = "buy" then 1 else 0 end) "购买次数"
from userbehavior group by mid(time_hour,1,2)
select UserID,count(UserID) as 购买次数
from userbehavior
where behavior = "buy"
group by UserID
order by count(UserID) desc
limit 10
create view score_R as
select UserID,
(case when 购买天数 between 0 and 2 then 1
when 购买天数 between 3 and 4 then 2
when 购买天数 between 5 and 6 then 3
when 购买天数 between 7 and 8 then 4 else 0 end
)as 购买得分
from
(select UserID,datediff(max(time_date),'2017-11-25')as 购买天数
from userbehavior
where behavior='buy'
group by UserID)as a
order by 购买得分 desc;
create view users_classify as
select UserID,
(case when R>3 and F>1 then '重要价值用户'
when R>3 and F<=1 then '重要保持用户'
when R<=3 and F>1 then '重要发展用户'
when R<=3 and F<=1 then '一般价值用户' else 0 end
)as 用户类型
from
(select a.UserID,a.购买得分 as R,b.购买频率得分 as F
from score_R as a inner join score_F as b
on a.UserID=b.UserID) c;