Monday, April 20, 2009

[SQL] Counting Rows with the Same Value of a Given Field

Here's a piss-easy script to count the number of rows in a table where a given field has the same value:

SELECT myField, COUNT(*) AS 'inelegantCount'
FROM myTable
GROUP BY myField

It's the classic GROUP BY statement example - which I can't manage to remember, thus the blog post.


3 comments:

Jax1961 said...

Hi. I always forget this, too (hence my visit...).

Also useful to add is

order by count(*) desc/asc

so you can see which value has most/least entries

Anonymous said...

Thanks, I always forget this, too.

Anonymous said...

thanks!