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; ----------------------------------------------------------- select count(l.customer_code) as last_month, count(x.customer_code) as this_month, l.year, l.month from zz_store_activity_by_month t, zz_store_activity_by_month l, zz_store_activity_by_month x where t.customer_code = l.customer_code and t.year * 12 + t.month = l.year * 12 + l.month - 1 and t.year = x.year and t.month = x.month and t.customer_code = x.customer_code group by l.year ,l.month order by l.year desc ,l.month desc; 137 137 2008 2 129 129 2008 1 148 148 2007 12 156 156 2007 11 ... .. .