[thelist] counting frequency of distinct values in MSSQL

Simon Willison cs1spw at bath.ac.uk
Sat Sep 13 14:19:35 CDT 2003


Joel D Canfield wrote:

> It's my weekend to be brain dead, I guess. I need to count the frequency
> of each unique numerical value in a table.
> 
> F'rinstance, if the table contained
> 
> 1
> 3
> 2
> 3
> 5
> 4
> 2
> 1
> 3
> 
> I'd like to see the results
> 
> 1 occurs 2 times
> 2 occurs 2 times
> 3 occurs 3 times
> 4 occurs 1 time
> 5 occurs 1 time

You need to use group by:

select column, count(column) from table group by column;

Hope that helps,

Simon Willison
http://simon.incutio.com/



More information about the thelist mailing list