SELECT cidnum1,count(*) as num from ( SELECT DISTINCT id,cidnum, (CASE WHEN 0<cidnum and cidnum<=1 THEN '1個公司' WHEN 1<cidnum and cidnum<=5 THEN '2-5個公司' WHEN 5<cidnum and cidnum<=10 THEN '6-10個公司' when 10<cidnum and cidnum<=50 then '11-50個公司' when 50<cidnum and cidnum<=100 then '51-100個公司' when 100<cidnum and cidnum<=500 then '101-500個公司' when 501<cidnum and cidnum<=1000 then '501-1000個公司' ELSE '1001個公司以上' END) as cidnum1 FROM TABLE where *** ) a GROUP BY cidnum1 ORDER BY num desc