MySQL Query: get new customers in a month

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

id, item_id, customer_id, date

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

What is the best and easiest request for this.

thank

+4
source share
3 answers
select customer_id , min(date) as min_date
from theTable
group by customer_id 
having min_date>=<the desired date>
+3
source

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;
+2
source

Something like this might be:

select distinct(id) from customer
where month(date)='Your_month'
order by date
+1
source

Source: https://habr.com/ru/post/1529755/


All Articles