with test (mindatetime, customerid)
as
(select min(datetime) as 'mindatetime', customerid from sales
group by customerid)
select s.productid, count(s.productid) as 'count' from test t
join sales s
on t.customerid = s.customerid and t.mindatetime = s.datetime
group by s.productid
go