I need to segment clients by the first date that their count of orders reached a specific value.
for example, we have a table of orders
ID, ClientID, Date
1, 1, 1/1/2011
2, 1 2/1/2011
3, 1 3/1/2011
4, 1 4/1/2011
I would like to get all the clients who has at least 3 orders and get the Date field when they reached 3 orders.
on my example, the query will show:
ClientID: 1, Date: 3/1/2011
(cause on the 3/1/2011, the client reached 3 orders)
How can I do it?
Thanks
Update:
I’m looking at your solutions (thank you very much!) but I need to do the same thing also with SUM (lets say the table above has a field called amount and I would like to segment the clients and get the first date that the client orders reached to $100)
I though that the solutions will help me understand the logic and easily convert the count to sum and make a new query but I got little bit lost here.. any ideas?
This would select all the clients with 3 orders with the date of 3rd order.
To select single client with date of 3rd order where could be modified with additional condition for that.
UPDATE based on question update – This query with little modification from count woud give you the running sum selection. There are few more techniques listed here for running sum.