题解 | #牛客的课程订单分析(五)#

牛客的课程订单分析(五)

http://www.nowcoder.com/practice/348afda488554ceb922efd2f3effc427

第二次想想多用函数代码会少很多,只分两步。 1、用row_number和count函数,把时间排序,次数,user_id和时间都显示出来。

select user_id,date, row_number()over(partition by user_id order by date) rk, count(product_name)over(partition by user_id) cnt from order_info where date>='2025-10-16' and status='completed' and product_name in('C++','Java','Python')

结果: 57|2025-10-23|1|2 57|2025-10-24|2|2 557336|2025-10-23|1|3 557336|2025-10-25|2|3 557336|2025-10-25|3|3

2、从这里面筛选出我们要的数据。注意筛选条件,排名要<=2(只选出第一次和第二次购买的时间),总数要>=2(购买课程2次以上)。以user_id和cnt group by,这样找出来的最小时间就是第一次,最大时间就是第二次。

select a.user_id,min(a.date) first_date,max(a.date) second_date,a.cnt from ( select user_id,date, row_number()over(partition by user_id order by date) rk, count(product_name)over(partition by user_id) cnt from order_info where date>='2025-10-16' and status='completed' and product_name in('C++','Java','Python') ) a where a.rk<=2 and a.cnt>=2 group by user_id,cnt order by user_id

全部评论

相关推荐

09-17 10:53
四川大学 C++
牛客91242815...:会写标书没有任何卵用,鉴定为横向垃圾导师的受害者
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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