题解 |

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

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

# 1.查询出所有商品被购买记录	
	select
		a.product_id,
		b.uid , 
		b.order_id
	from
		tb_product_info a ,
		tb_order_overall b ,
		tb_order_detail c
	where
		a.product_id = c.product_id
		and c.order_id = b.order_id
		and b.status = 1
		and tag = '零食'
		and datediff(date((select max(event_time) from tb_order_overall)),
		date(event_time))<90;
8002|101|301001|2021-09-30 10:00:00
8003|102|301011|2021-10-31 11:00:00
8001|102|301011|2021-10-31 11:00:00
8001|102|301002|2021-10-01 11:00:00
8003|102|301002|2021-10-01 11:00:00
8002|101|301003|2021-11-02 10:00:00
8003|101|301003|2021-11-02 10:00:00
8002|105|301013|2021-11-02 10:00:00
8003|104|301005|2021-11-03 10:00:00

# 2. 再根据商品id和用户id分组,计算每个用户购买某个商品的订单数量。如果数量>=2 计数为1
	select
		a.product_id,
		b.uid ,
       	if(count(b.order_id)>= 2,1,0)
	from
		tb_product_info a ,
		tb_order_overall b ,
		tb_order_detail c
	where
		a.product_id = c.product_id
		and c.order_id = b.order_id
		and b.status = 1
		and tag = '零食'
		and datediff(date((select max(event_time) from tb_order_overall)),
		date(event_time))<90
	group by
		a.product_id,
		b.uid
8002|101|1
8003|102|1
8001|102|1
8003|101|0
8002|105|0
8003|104|0		
		
# 3. 按照商品id分组.  count(t.cnt) 就是某一商品被购买过的人数,  sum(t.cnt)是某商品被购买至少两次的人数。 

select
	product_id,round(sum(t.cnt)/ count(t.cnt),3)  rate
from
	(select
		a.product_id,uid ,if(count(b.order_id)>= 2,1,0) cnt
	from tb_product_info a , tb_order_overall b ,tb_order_detail c
	where a.product_id = c.product_id and c.order_id = b.order_id and b.status = 1
	and tag = '零食' and datediff(date((select max(event_time) from tb_order_overall)), date(event_time))<90
	group by a.product_id, b.uid) t
group by t.product_id
order by rate desc,product_id limit 3;

8001|1.000
8002|0.500
8003|0.333


全部评论
楼主打印出数据来是在本地建库还是有在线的可以推荐使用
点赞 回复 分享
发布于 2022-03-12 13:04

相关推荐

点赞 评论 收藏
分享
头像
11-03 16:48
已编辑
百度_高级研发工程师
事实是检验真理的唯一标准。&nbsp;无论我们怎么去说,去讲述,去证明,都抵不过一个offer来得实在,无论我们怎么去复现求职中的摸爬滚打、扒皮抽筋、狼狈不堪,都抵不过你在简历写上大厂的名字(外包不算)。&nbsp;所以在我求职期间,我什么话都不说,什么话都不讲,因为没有意义,虽然我总讲过程才是意义,但只有当你上岸的那一刻,你才有资格回想在水里的挣扎,只有等你出了山,你才知道山的全貌。&nbsp;我为什么一定要离开华为OD,难道它不稳定吗,不能赚钱吗。为了证明自己,那肯定有的。其实更多的是印证我的认知是否真的正确。&nbsp;(给不了解我的人交代一下背景,在下双非一本,gap一年,华为OD外包,摸爬滚打4个月,艰难上岸百度正编)一、...
先锋战士:说得很真诚。鄙视链自古有之,学历,家庭背景,财富,权利。从小有之,小学羡慕那些当班委的,中学羡慕那些学生会的,高中羡慕尖子班拿教学金的,大学羡慕高绩点,毕业了羡慕进大厂的。工作了,又羡慕高职级的,再后来又羡慕别人早早结婚的。我想表达的观点很简单,无论是华为od还是百度,都是经历,没有孰高孰低,为了抵达下一个风景,总会付出更多东西,但不就是人生吗?正如登山,每个阶段的山,都要想办法攀登,在博主的文字中,见到了坚持和积极寻找问题解决办法的心态
学历对求职的影响
点赞 评论 收藏
分享
评论
17
收藏
分享

创作者周榜

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