Skip to content
Advertisement

SQL – Distribution Count

Hi I have the following table:

I would like to show the following:

Basically I want to count the number crm_ids that have 1,2,3,4,5+ customer_ids.

Thanks

Advertisement

Answer

One approach is to aggregate twice. First, aggregate over crm_id and generate counts. Then, aggregate over those counts themselves and generate a count of counts.

Have a look at a demo below, given in MySQL as you did not specify a particular database (though my answer should run on most databases I think).

Demo

Advertisement