难点在于第二次购买时间,利用窗口函数解决
select u.user_id, max(if(u.date_rk = 1, u.date ,null)), max(if(u.date_rk = 2, u.date ,null)), max(u.cnt) from (select user_id,date, dense_rank() over(partition by user_id order by date) as date_rk, count(user_id) over(partition by user_id) as cnt from order_info where date > '2025-10-15' and product_name in ('C++','Python','Java') and status = 'completed') as u where u.cnt > 1 group by user_id order by user_id;