题解 | #零食类商品中复购率top3高的商品#

零食类商品中复购率top3高的商品

http://www.nowcoder.com/practice/9c175775e7ad4d9da41602d588c5caf3

【问题】统计零食类商品中复购率top3高的商品
  1. 商品复购率 =<近90天内>购买它至少两次的人数 ÷ 购买它的总人数
  2. 近90天指包含<最大日期>(记为当天)在内的近90天
  3. 结果中复购率保留3位小数,并按复购率倒序、商品ID升序排序

首先,左连接表,生成<原始表>,并根据条件将数据进行筛选

SELECT  * from tb_order_overall o
left join tb_order_detail d
on o.order_id=d.order_id
left join tb_product_info p
on p.product_id=d.product_id
然后,对得到的表进行原始数据筛选,需要满足条件
  1. 商品是零食类 p.tag='零食'
  2. 商品成功支付  o.status=1
  3. 交易日期在近90天,即交易日期date(o.event_time)>=距今90天的日期
 于是,锁定【距今90天的日期,如何计算?】
    1.先计算标杆日期,也就是最大日期
select date(max(event_time)) recent_day from tb_order_overall
    2.以recent_day为基准,向前推89天date_sub(recent_day,interval 89 day),得到距今90天的日期
select DATE_SUB(t.recent_day,INTERVAL 89 day) FROM
(select date(max(event_time)) recent_day from tb_order_overall)t
所以交易在近90天为
date(o.event_time)>=(select DATE_SUB(t.recent_day,INTERVAL 89 day) FROM
(select date(max(event_time)) recent_day from tb_order_overall)t)
以上,完成原始表的构造
SELECT * from tb_order_overall o
left join tb_order_detail d
on o.order_id=d.order_id
left join tb_product_info p
on p.product_id=d.product_id
where p.tag='零食'
and o.status=1
and date(o.event_time)>=(select DATE_SUB(t.recent_day,INTERVAL 89 day) FROM
(select date(max(event_time)) recent_day from tb_order_overall)t)

然后,统计每类商品下,各用户购买的次数


SELECT p.product_id,o.uid,count(p.product_id) cnt from tb_order_overall o
left join tb_order_detail d
on o.order_id=d.order_id
left join tb_product_info p
on p.product_id=d.product_id
where p.tag='零食'
and o.status=1
and date(o.event_time)>=(select DATE_SUB(t.recent_day,INTERVAL 89 day) FROM
(select date(max(event_time)) recent_day from tb_order_overall)t)
group by p.product_id,o.uid

接着,通过计算每类商品购买两次以上的人数以及总人数,可以计算出复购率

思路:按照商品ID分组
  • 总人数则为count(distinct uid)
  • 购买两次以上的人数,因为上步骤已经计算出没中商品每人购买的次数,所以使用case when 筛选出购买次数在2次以上的,并进行人数统计count(distinct case when 购买次数>=2 then uid else null end)
SELECT t1.product_id,
round(count(distinct case when t1.cnt>=2 then t1.uid else null end)/count(DISTINCT t1.uid),3) repurchase_rate  
FROM
(SELECT p.product_id,o.uid,count(p.product_id) cnt from tb_order_overall o
left join tb_order_detail d
on o.order_id=d.order_id
left join tb_product_info p
on p.product_id=d.product_id
where p.tag='零食'
and o.status=1
and date(o.event_time)>=(select DATE_SUB(t.recent_day,INTERVAL 89 day) FROM
(select date(max(event_time)) recent_day from tb_order_overall)t)
group by p.product_id,o.uid)t1
group by t1.product_id
最后,按照复购率倒序desc、商品ID升序排序,筛选出top3(limit 3)
~~~~~~代码省略 

全部评论

相关推荐

06-13 17:33
门头沟学院 Java
顺序不记了,大致顺序是这样的,有的相同知识点写分开了1.基本数据类型2.基本数据类型和包装类型的区别3.==和equals区别4.ArrayList与LinkedList区别5.hashmap底层原理,put操作时会发生什么6.说出几种树型数据结构7.B树和B+树区别8.jvm加载类机制9.线程池核心参数10.创建线程池的几种方式11.callable与runnable区别12.线程池怎么回收线程13.redis三剑客14.布隆过滤器原理,不要背八股,说说真正使用时遇到了问题没有(我说没有,不知道该怎么回答了)15.堆的内存结构16.自己在写项目时有没有遇见过oom,如何处理,不要背八股,根据真实经验,我说不会17.redis死锁怎么办,watchdog机制如何发现是否锁过期18.如何避免redis红锁19.一个表性别与年龄如何加索引20.自己的项目的QPS怎么测的,有没有真正遇到大数量表21.说一说泛型22.springboot自动装配原理23.springmvc与springboot区别24.aop使用过嘛?动态代理与静态代理区别25.spring循环依赖怎么解决26.你说用过es,es如何分片,怎么存的数据,1000万条数据怎么写入库中27.你说用limit,那么在数据量大之后,如何优化28.rabbitmq如何批次发送,批量读取,答了延迟队列和线程池,都不对29.计网知不知道smtp协议,不知道写了对不对,完全听懵了30.springcloud知道嘛?只是了解反问1.做什么的?短信服务,信息量能到千万级2.对我的建议,基础不错,但是不要只背八股,多去实际开发中理解。面试官人不错,虽然没露脸,但是中间会引导我回答问题,不会的也只是说对我要求没那么高。面完问我在济宁生活有没有困难,最快什么时候到,让人事给我聊薪资了。下午人事打电话,问我27届的会不会跑路,还在想办法如何使我不跑路,不想扣我薪资等。之后我再联系吧,还挺想去的😭,我真不跑路哥😢附一张河科大幽默大专图,科大就是大专罢了
查看30道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务