sql server 2008 - Counting with SQL -


how count how many values of each distinct value there in specific table? have table column containing different values varying number of different values. create table 1 column listing value , listing number of each value. have column 'letter' values a a b b c c going down want make table column 'letter' , 'number' b c vs 4 2 2

select count(letter) occurences,        letter table group letter order letter asc 

basically you're looking count() function. aware aggregate function , must use group @ end of select statement


if have letters on 2 columns (say col1 , col2) should first union them in single 1 , count afterwards, this:

select count(letter) occurences,        letter (select col1 letter       table       union        select col2 letter       table) group letter  order letter; 

the inner select query appends content of col2 col1 , renames resulting column "letter". outer select, counts occurrences of each letter in resulting column.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -