You can use row_number()
and conditional aggregation. Here is an exaple:
select customerid, avg(sales) as avgsales,
max(case when segnum = 1 then sales end) as sales_01,
max(case when segnum = 2 then sales end) as sales_02,
max(case when segnum = 3 then sales end) as sales_03
from (select t.*, row_number() over (partition by customerid order by visitdate) as segnum
from table t
) t
group by customerid;
By the way, your question really should include sample data, desired results, and the table layout,
2
solved Identifying Unique Visits by Customer [closed]