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

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

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

# max(time) timestampdiff(day, event_time, max(time)) < 90
# group by product_id,uid
# not in status = 0,2 . id
# 窗口函数 count(*) 算 pid.uid 内有多少个  ->  开窗去算 pid 内的满足要求的uid 有多少个 ->  avg() over(pid)

select 
    distinct product_id, round( avg( if(count(event_time) > 1,1,0) ) over(partition by product_id)   , 3) repurchase_rate
from 
tb_order_overall a 
left join tb_order_detail b using(order_id)
left join tb_product_info c using(product_id)
where  timestampdiff(day,event_time, (select  max(event_time) from tb_order_overall)) < 90 and  a.order_id not in (select order_id from tb_order_overall where status != 1) and tag = '零食'
group by product_id, uid
order by repurchase_rate desc, product_id asc limit 3

借用 sql 159 的思路 利用开窗函数来实现

全部评论

相关推荐

09-14 20:51
四川大学 Java
慢热的鲸鱼在学习:985加粗就行了,第二个项目来不及准备也没事,省的写了问你你还不会。你只需准备面试八股和项目场景,剩下的交给985。即使面不过也没事,面试经验是最重要的,你现在不缺时间
简历中的项目经历要怎么写
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
11-06 20:49
某国企 研发工程师 31W 硕士211
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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