Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Query: Get new customers in the month

Tags:

sql

mysql

I have millions of records in a table with following columns:

id, item_id, customer_id, date

I want to find customer_id for a specific month which are inserted first time in the table in that month.

What is best and simple query for this.

Thanks

like image 883
Awan Avatar asked Mar 03 '14 07:03

Awan


2 Answers

select customer_id , min(date) as min_date
from theTable
group by customer_id 
having min_date>=<the desired date>
like image 125
StanislavL Avatar answered Sep 22 '22 21:09

StanislavL


try something like:

SELECT
date_format(date,"%M") month,group_concat(customer_id ORDER BY customer_id) customer_ids
FROM
<table_name>
GROUP BY month;
like image 38
avisheks Avatar answered Sep 24 '22 21:09

avisheks