select cid, count(*) from(
select distinct cid, gid from thetablewhereyourddatais)
group by cid
order by cid;