I would like a query to return the number of customers who ordered once, twice, etc to the maximum number of times anyone has ordered.
I'm not great at SQL so I've got bits of a query, I just don't know how to piece them together.
I believe this will return the total number of orders per person as num_of_orders:
(SELECT
customer_id, customer_email,
COUNT(*) as num_orders
FROM
sales_flat_order AS sfo
WHERE
sfo.status='complete'
GROUP BY customer_id) AS num_of_orders
Therefore, this should give the highest number of orders:
MAX(num_of_orders) AS max_num_orders
I then need to count through each row in num_of_orders
perhaps using a WHILE loop?
WHILE x != max_num_orders
COUNT(num_orders = x),
SET x = x+1
END WHILE
Can anyone help put a query together to achieve this? Thanks!