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

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

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

1.tb1表是为了求出不同的人(uid),在近90天所购买不同商品的次数(product_id) 此处注意要最大日期减去89天才是确切的区间范围,如果减去90天会导致最后一组用例不通过!!!

2.求出商品总的购买人数 并且 左连接 商品购买次数大于二的人数,必须左连,不然商品购买次数没有大于二的数据会缺失!!!

3.求解,ifnull也必须要有,原因是分子可能为null值,即商品购买次数大于2的值可能会null(tips:对at2加ifnull没用,因为count不会出现null值,此处null值是因为第二步的左连接而出现的)

select t3.uid,t1.product_id,count(*) ant
from tb_product_info t1 join tb_order_detail t2 using(product_id) join tb_order_overall t3 using(order_id)
where t1.tag='零食' and t3.event_time between date_sub((select max(event_time) from tb_order_overall),interval 89 day) 
                                         and (select max(event_time) from tb_order_overall) and t3.status=1
group by t3.uid,t1.product_id
)
select T1.product_id id,ifnull(round(T2.at2/T1.at1,3),0) at3
from(
select product_id,count(*) at1
from tb1
group by product_id
)as T1
left join(
select product_id,count(*) at2
from tb1
where ant>=2
group by product_id
)as T2 using(product_id)
order by at3 desc,id asc
limit 3
全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务