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

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

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

with t as
(
select 
tod.product_id product_id,tag,uid,date(event_time) dt,
max(date(event_time)) over() max  # 最大日期
from tb_order_overall too
join tb_order_detail tod on too.order_id = tod.order_id
join tb_product_info tpi on tpi.product_id = tod.product_id	
where status = 1 and tag = "零食"
), 

r as(    # 各商品每个用户的购买次数,先执行窗口函数,后distinct
select 
distinct product_id,uid,count(uid) over(partition by product_id,uid) c
from t 
where dt between date_sub(max,interval 89 day) and max )

# p as
# (select product_id,count(uid) m from r where c>=2 group by product_id)
# 把购买次数大于等于2的人数筛选出来连接t,存在复购人数为0需要左连接的情况

select 
product_id,
round(sum(if(c>=2,1,0))/count(uid),3) repurchase_rate
from r 
group by product_id
order by repurchase_rate desc,product_id
limit 3





全部评论

相关推荐

09-22 15:45
门头沟学院 Java
谁给娃offer我给...:我也遇到了,我说只要我通过面试我就去,实际上我根本就不会去😁
点赞 评论 收藏
分享
09-03 17:49
已编辑
四川大学 供应链管理
迷茫的大四🐶:提前实习有些坑的,卡毕业后薪资以及能力不达预期提前违约这种
我的OC时间线
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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