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

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

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

select 
t.user_id
,min(t.date) as first_buy_date
,max(t.date) as second_buy_date
,t.cnt
from 
(
select 
user_id
,date
,rank() over(partition by user_id order by date) r_no
,count(*) over(partition by user_id) cnt
from order_info
where user_id in 
(
    select user_id 
    from order_info a
    where date>'2025-10-15'
    and product_name in ('C++','Java','Python')
    and status='completed'
    group by user_id 
    having count(user_id)>=2
)
and product_name in ('C++','Java','Python')
and status='completed'
and date>'2025-10-15'
) t
where t.r_no<=2 and t.cnt>=2
group by t.user_id
order by t.user_id

思路:目前题目里写的行数最多的代码,中间套用79#牛客的课程订单分析(三)# 的代码,然后对日期和个人次数用了窗口函数,然后作为一个子查询嵌套到外部查询中,在外部查询的where条件中,选取的日期为前俩天,所以t.r_no<=2,既然要选俩个日期,所以至少需要俩行记录,即t.cnt>=2,然后分组排序就好。

sql题 文章被收录于专栏

牛客网里面的sql题,把答案和思路写下来

全部评论

相关推荐

10-17 23:18
已编辑
西北农林科技大学 Web前端
独行m:给25可以试试,但他只能给12,那就是纯纯的事精
秋招,不懂就问
点赞 评论 收藏
分享
Java转测开第一人:这种就是饼 把应届当廉价劳动力用完然后丢掉
你觉得今年秋招难吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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