select count(l.customer_code) as last_month, l.year,l.month from zz_store_activity_by_month t, zz_store_activity_by_month l where t.customer_code = l.customer_code and t.year * 12 + t.month = l.year * 12 + l.month - 1 group by l.year ,l.month order by l.year desc ,l.month desc; select count(customer_code) as this_month, year,month from zz_store_activity_by_month group by year,month order by year desc ,month desc;