Monday, May 21, 2012

Need hints on seemingly simple SQL query

I'm trying to do something like:



SELECT c.id, c.name, COUNT(orders.id)
FROM customers c
JOIN orders ON o.customerId = c.id


However, SQL will not allow the COUNT function. The error given at execution is that c.Id is not valid in the select list because it isn't in the group by clause or isn't aggregated.



I think I know the problem, COUNT just counts all the rows in the orders table. How can I make a count for each customer?





No comments:

Post a Comment