select store_num, count(*) custcount from ( select store_num, concat(area_code,phone_num), count(concat(area_code,phone_num)) from orders_p where ord_date>='00000000' and ord_date<='20070128' and store_num=1 group by concat(area_code,phone_num) ) ordpsub group by store_num +--------+-------------+--------+--------+---------------+--------+---------+--------+--------+--------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +--------+-------------+--------+--------+---------------+--------+---------+--------+--------+--------+ | 2 | DERIVED | orders_p| range | PRIMARY,order_date,store_number,date_store| PRIMARY| 14 | [NULL] | 139250 | Using where; Using temporary; Using filesort| | 1 | PRIMARY | | ALL | [NULL] | [NULL] | [NULL] | [NULL] | 20720 | Using temporary; Using filesort| +--------+-------------+--------+--------+---------------+--------+---------+--------+--------+--------+