题解 | #牛客的课程订单分析(五)#
牛客的课程订单分析(五)
https://www.nowcoder.com/practice/348afda488554ceb922efd2f3effc427
想到了个比较复杂的代码,不用窗口函数。
先做了一个第一次购买成功的表,其实也就是上一题,直接拿过来,单独做一个表
然后又做了第二次购买成功的表,其实用了连接和子查询,剔除第一次购买成功的时间后的时间中筛选最小购买时间
两个表连接,查找数据
select a.user_id,a.first_buy_date,b.second_buy_date,a.cunt from
(select user_id,min(date) first_buy_date,count(*) cunt from order_info
where status='completed' and date>'2025-10-15' and product_name in ('C++','Java','Python')
group by user_id
having count(*)>=2) a , (select user_id,min(date) second_buy_date from order_info o1 where date>'2025-10-15' and product_name in ('C++','Java','Python') and status='completed' and
`date` not in (select min(date) from order_info o2
where o2.user_id=o1.user_id and status='completed' and date>'2025-10-15' and product_name in ('C++','Java','Python') ) group by user_id ) b
where a.user_id=b.user_id
order by a.user_id

影石Insta360公司氛围 452人发布