I have a table of customer orders which is something like below:
order_id | customer_id | date | ...
-----------------------------------------
583 192 2015-05-01 ...
734 143 2015-06-04 ...
801 455 2015-07-02 ...
... ... ... ...
I want to find how many new customers I receive in a given month. A new customer can be found by querying the orders table above and finding the number of customer_id
values that appear in that month but do not appear before that month (after that month is fine). I'm looking for the first instance of a new customer_id
.
Is there a way to do this in one SQL statement? I could do something like the below I think:
- get all
order_id
andDISTINCT customer_id
from a month by querying database usingLIKE YYYY-MM-%
- check the table for instances of
customer_id
with a smallerorder_id
(it stands to reason a smallerorder_id
will be an earlier order) - if found, skip
- if not found, increment a counter