题解 | #牛客的课程订单分析(四)#
牛客的课程订单分析(四)
http://www.nowcoder.com/practice/c93d2079282f4943a3771ca6fd081c23
窗口函数row_number()按user_id分组日期升序给出序号r
count()over()计算出符合条件的总购买次数cnt
最后条件删选cnt>=2且r=1的即为购买条件课程成功超过3次 且第一次购买的行
SELECT
a.user_id,
a.date first_buy_date,
a.cnt
FROM
(
SELECT
*,
ROW_NUMBER() over(partition by user_id order by date) r,
count(product_name) over(partition by user_id) cnt
from order_info
where status='completed'
and date>'2025-10-15'
and product_name in('C++','Java','Python')
)a
where a.r=1 and a.cnt>=2
order by 1