题解 | #某店铺的各商品毛利率及店铺整体毛利率#

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

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

1.统计每个零食类商品每个用户在近90天内的购买次数

with t1 as(
select t1.product_id, t2.uid,count(t2.uid) cnt
from tb_product_info t1 
join tb_order_detail t3
on t1.product_id = t3.product_id
join tb_order_overall t2
on t2.order_id = t3.order_id
where status=1 and tag = '零食'
and date(event_time)>=date_sub((select date(max(event_time)) from tb_order_overall),interval 89 day)
group by t1.product_id,t2.uid)

得到如下表所示的统计结果

alt

2.计算每个商品的复购率并得到top3

select product_id, round(sum(if(cnt>1,1,0))/count(uid),3) repurchase_rate
from t1
group by 
product_id
order by repurchase_rate desc, product_id
limit 3

3.最终的sql

with t1 as(
select t1.product_id, t2.uid,count(t2.uid) cnt
from tb_product_info t1 
join tb_order_detail t3
on t1.product_id = t3.product_id
join tb_order_overall t2
on t2.order_id = t3.order_id
where status=1 and tag = '零食'
and date(event_time)>=date_sub((select date(max(event_time)) from tb_order_overall),interval 89 day)
group by t1.product_id,t2.uid)

select product_id, round(sum(if(cnt>1,1,0))/count(uid),3) repurchase_rate
from t1
group by 
product_id
order by repurchase_rate desc, product_id
limit 3

全部评论

相关推荐

程序员小白条:主要没亮点,项目也是网上的,平平无奇,那只能海投了,奖项总得有一些,然后就是现在最好是前后端都会,自己能做项目并且运维的,要么找星球项目改改,要么找个开源项目改改,自己能拓展功能才是主要的,跟做效率很低很低
点赞 评论 收藏
分享
星辰再现:裁员给校招生腾地方
点赞 评论 收藏
分享
评论
2
2
分享

创作者周榜

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