题解 | #零食类商品中复购率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 的思路 利用开窗函数来实现

全部评论

相关推荐

05-07 17:58
门头沟学院 Java
wuwuwuoow:1.简历字体有些怪怪的,用啥写的? 2.Redis 一主二从为什么能解决双写一致性? 3.乐观锁指的是 SQL 层面的库存判断?比如 stock > 0。个人认为这种不算乐观锁,更像是乐观锁的思想,写 SQL 避免不了悲观锁的 4.奖项证书如果不是 ACM,说实话没什么必要写 5.逻辑过期时间为什么能解决缓存击穿问题?逻辑过期指的是什么 其实也没什么多大要改的。海投吧
点赞 评论 收藏
分享
头像
04-17 09:29
已编辑
湖南农业大学 后端
睡姿决定发型丫:本硕末9也是0offer,简历挂了挺多,只有淘天 美团 中兴给了面试机会,淘天二面挂,美团一面kpi面,中兴一面感觉也大概率kpi(虽然国企,但一面0技术纯聊天有点离谱吧)
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务