freeswitch=> select cnt, count(*) from (select company_id, count(*) cnt from lines group by company_id) q1 group by cnt order by cnt desc; cnt | count -----+------- 42 | 1 40 | 1 38 | 1 36 | 1 27 | 1 24 | 2 23 | 1 22 | 1 21 | 2 20 | 2 19 | 1 18 | 1 16 | 2 15 | 2 13 | 1 12 | 1 11 | 2 10 | 9 9 | 7 8 | 19 7 | 10 6 | 17 5 | 15 4 | 49 3 | 55 2 | 147 1 | 248 (27 rows)