jacob jacob - 1 month ago 12
MySQL Question

Select number of customers grouped by number of orders

I would like to select the number of customers grouped by the number of orders. The result should somehow look like this:

Number of Orders | Numbers of Customers
---------------------------------------
1 | 29
2 | 16
3 | 23
4 | 26
....


I am not sure how to do this

SELECT COUNT(o.ID), COUNT(o.ID)
FROM ORDERS o
GROUP BY 1


Thanks for your help.

Answer

If I understood you correctly :

SELECT s.num_of_orders,count(*) as num_of_customers
FROM(
    SELECT count(*) as num_of_orders
    FROM Orders o
    GROUP BY o.customer_id) s
GROUP BY s.num_of_orders    
Comments