There is a table called Orders has three columns:
OrderID
UserID
OrderDate
I need to know, for X months in the past, A users placed 1 order, B users placed 2 orders, C users placed 3 orders, etc.
So the output is a table w/ a MinDate field and then columns for each # of orders from 1 to N, e.g.:
declare @t table (
MinDate datetime,
o_1 int,
o_2 int,
o_3 int,
o_4 int,
...
o_N int
)
Each field contains the number of unique customers (UserID's) that placed that many orders between MinDate and the current date. For example, if a row has MinDate = '2009-09-01' then o_6 contains the number of customers who have placed 6 orders between Sept 1 and present, etc.
I have tried several approaches, LEFT JOIN Orders to a number table, etc. but am having trouble getting the correct count of unique users into each column. I could do this easily (and slowly) with a CURSOR but that seems like cheating.
Any suggestions? Let me know if my explanation does not make sense...