思路:对日期进行排序,用max(case when)转置(用sum需要转换格式)
select
user_id
,max(case when rk = 1 then date else 0 end) first_buy_date
,max(case when rk = 2 then date else 0 end) second_buy_date
,count(user_id) cnt
from
(
select
user_id
,date
,row_number()over(partition by user_id order by date) rk
from order_info
where date > '2025-10-15'
and status = 'completed'
and product_name in ('C++','Java','Python')
) a
group by 1
having cnt>=2
order by user_id