views:

55

answers:

2

I need to get the number of items that have a comment but I cant get this SQL statement to work for me........any advice?

Select count(Name) as TotalComments 
from TableName where comment <> '' 
order by ID 

Error Message:

Column "TableName.ID" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.

What am I missing exactly?

+4  A: 

Wait a minute...

Select count(Name) as TotalComments  
from TableName where comment <> ''  
order by ID 

You're selecting a count, so the Order By clause is pointless. You should be getting a scalar result. ( a single value, not a set if rows)

Is this a trick question? It's too early for that.

Simply remove the "Order By" clause. It's unnecessary.

David Stratton
David is not giving you a query to try.
Nirmal
That's right, @Nirmal. Thank you.. I was repeating his query (in case he changes it after I sign off. I've had people do that so it looks like I'm answering the wrong question.)
David Stratton
Thanks! TO early in the morning!!!
Etienne
@David: Now he deleted his previous comment, I look stupid.
Nirmal
That's too funny! Well, I'll vouch for your smarts.
David Stratton
A: 

try this (I tried it in Sql server not in MySql)

SELECT     Name, COUNT(ID) AS TotalComments
FROM       TableName
WHERE     (Comment IS NOT NULL)
GROUP BY Name
ORDER BY TotalComments
Sameh Serag