每日sql-复购率问题count+case+timediff类函数

强推良心公众号:猴子数据分析
资料来源:
如何分析​复购用户?
人均付费如何分析?

今日要点:

  • timestampdiff(返回的时间格式,起始时间,结束时间)
  • case when ...then...else...end
  • count(case ……)
  • count(distinct)
image.png
select a.购买时间,
count(distinct a.用户id) 当日首次购买用户数,
count(distinct case when timestampdiff(month,a.购买时间,b.购买时间) <=1 
      then a.用户id  else null end ) as 此月复购用户数,
count(distinct case when timestampdiff(month,a.购买时间,b.购买时间) =3 
      then a.用户id  else null end ) as 第三月复购用户数,
count(distinct case when timestampdiff(month,a.购买时间,b.购买时间) =4
      then a.用户id  else null end ) as 第四月复购用户数,
count(distinct case when timestampdiff(month,a.购买时间,b.购买时间) =5
      then a.用户id  else null end ) as 第五月复购用户数,
count(distinct case when timestampdiff(month,a.购买时间,b.购买时间) =20
      then a.用户id  else null end ) as 第二十月复购用户数
from 课程订单表 as a
left join 课程订单表 as b
on a.`用户id` = b.`用户id`
where a.课程类型=2 and a.购买时间!=b.购买时间
group by a.购买时间;

timestampdiff与timediff的区别,前者可以返回时分秒的差,后者只能返回相差的天数


image.png

1、各地用户数(以后看到这个题目就要想到去重呀)

select 城市,count(distinct 用户id),sum(ARPU值)
from 各城市用户ARPU值
group by 城市

2、各城市各分段用户数是多少

select 
count(distinct case when ARPU值>0 and ARPU值<30 then 1 else null end )as '(0-30)'
count(distinct case when ARPU值>=30 and ARPU值<50 then 1 else null end )as '[30-50)'
count(distinct case when ARPU值>=50 and ARPU值<80 then 1 else null end )as '[50-80)'
count(distinct case when ARPU值>=80  then 1 else null end )as '[80以上)'
from 各城市用户ARPU值
group by 城市

3、找出表2中重复的用户数

select 用户id
from 用户套餐费用表
group by 用户id
having count(用户id)>2

4.mysql不支持全连接,left+union+right


image.png

image.png

一句SQL取出所有用户对商品的行为特征,特征分为已购买、购买未收藏、收藏未购买、收藏且购买(输出结果如下表)


image.png
select o.user_id,o.item_id,
(case when o.pay_time is not null then 1 else null end) as 已购
(case when o.pay_time is not null  and f.fav_time is null then 1 else null end)as 购买未收藏
(CASE when o.pay_time is null and f.fav_time is not null then 1 else 0 end) as  收藏未购买,
(CASE when o.pay_time is not null and f.fav_time is not null then 1 else 0 end) as 收藏且购买
from orders o
left join favourites f 
on o.user_id = f.user_id and o.item_id = f.item_id
UNION
SELECT
f.user_id,f.item_id,
(CASE when o.pay_time is not null then 1 else 0 end) as '已购买',
(CASE when o.pay_time is not null and f.fav_time is null then 1 else 0 end) as '购买未收藏',
(CASE when o.pay_time is null and f.fav_time is not null then 1 else 0 end) as '收藏未购买',
(CASE when o.pay_time is not null and f.fav_time is not null then 1 else 0 end) as '收藏且购买'
FROM orders o 
RIGHT JOIN favorites f 
ON o.user_id = f.user_id 
AND o.item_id = f.item_id
ORDER BY user_id, item_id;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.软件测试级别? 单元测试:单元测试是对软件组成单元进行测试。其目的是检验软件基本组成单位的正确性。测试的对象是...
    听闻白依阅读 1,494评论 0 9
  • 最近又重新复习了一遍SQL的基础和进阶知识,SQL可以很基础也可以很复杂,写一篇总结来梳理自己的知识脉络 SQL是...
    元宝2020阅读 431评论 0 0
  • 1、应用扩展(Extension)这是一个千呼万唤始出来的特性,也是一个可以发挥无限想象力的特性。现在Apple允...
    Trigger_o阅读 1,994评论 0 1
  • 渐变的面目拼图要我怎么拼? 我是疲乏了还是投降了? 不是不允许自己坠落, 我没有滴水不进的保护膜。 就是害怕变得面...
    闷热当乘凉阅读 4,349评论 0 13
  • 感觉自己有点神经衰弱,总是觉得手机响了;屋外有人走过;每次妈妈不声不响的进房间突然跟我说话,我都会被吓得半死!一整...
    章鱼的拥抱阅读 2,211评论 4 5