Minor editing of @Mark Bannister's answer. OP announced its intention to record the number of new customers by the month in which they placed their first order. The OP definition of a “new customer” is confused in that it indicates:
"new customer": (a) one who has never ordered before date / month; and (b) who has at least one order after date / month (allocation added)
Perhaps this is really what the OP wants, but it can more accurately be called a “new customer who will become a repeat customer in the coming month”, as it eliminates:
- Customers who place only one order
- Customers who place all their orders on the same date / month
The @Mark solution correctly (?) Eliminates these clients as its SQL FIDDLE indicates that Fred @home is NOT considered a new client.
However, the OP also states at the end of its report that the “applicable customers” are:
those who did not order before the entered month
Thus, in order to capture ALL new customers (this is what I think OP is really worth it), we can still use the @Mark nice PIVOT app, and all we need to do is look at min (OrderDate) and just ignore maxDate comparison as below:
select yr, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12] from (select datepart(month,minDate) mth, datepart(year,minDate) yr, count(*) cnt from (select min(OrderDate) minDate from tblOrder group by email) sq group by datepart(month,minDate), datepart(year,minDate)) src PIVOT (max(cnt) for mth in ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]) ) pvt
A quick check to make sure all clients are captured can be done by summing up all the calculations in the pivot table (quickly and easily in Excel) and comparing with:
SELECT COUNT(DISTINCT(email)) FROM tblOrder