select user_id,`date` first_buy_date,second_buy_date,cnt from (select user_id,`date`,r,lead(`date`,1)over(partition by user_id order by `date`) second_buy_date,cnt from (select user_id,`date`,rank()over(partition by user_id order by `date`) r,count(*)over(partition by user_id) cnt from order_info where 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(*)>=2) and `date`>'2025-10-15' and status='completed' and product_name in ('C++','Java','Python')) t1) t2 where r=1