题解 | #牛客的课程订单分析(五)#
牛客的课程订单分析(五)
http://www.nowcoder.com/practice/348afda488554ceb922efd2f3effc427
#第一个from子句,括号中已筛选出符合条件的元组。只剩下将date按照第一次、第二次排出来。
#筛选出前两次购买的时间,那么在这两天中,最小的就是第一次购买的;最大的就是第二次购买的。因此,可以成功分离两列出来。
#最上面的一条select子句,其中的cnt是常数列,所以在使用了group by子句后,仍然可以选出来。
select user_id,min(date),max(date),cnt
from(
select user_id,date,row_number() over(partition by user_id order by date) as r,
count(user_id) over(partition by user_id) as cnt
from order_info
where date >'2025-10-15' and status ='completed'
and product_name in('C++','Java','Python')
and user_id in(
select user_id
from order_info
where date >'2025-10-15' and status ='completed'
and product_name in('C++','Java','Python')
group by user_id
having count(user_id)>1)) as n1
where n1.r<3
group by n1.user_id
order by n1.user_id;