with sub_t as ( select r.customer_id , r.order_id , r.order_date , date_format(r.order_date ,'%Y-%m-01') as order_month , date_format(c.first_order_date , '%Y-%m-01') as first_order_month from records r left join customer_stats c on r.customer_id = c.customer_id)select first_order_month, count(distinct customer_id) as month0, count(distinct case when date_add(first_order_month, interval 1 month..