r15habh
r15habh

Reputation: 1468

Counting occurrences of unique values in a column using sql

Is there a simple way for counting occurrences of unique values in a column using sql.

for e.g if my column is

a
a
b
a
b
c
d
d
a

Then the output should be

a 4
b 2
c 1
d 2

Upvotes: 16

Views: 28394

Answers (3)

Cyberflow
Cyberflow

Reputation: 1305

After searching and giving some good tought here's the correct query :

SELECT SUM(uniqueValues) 
FROM (
    SELECT COUNT(DISTINCT values) as uniqueValues 
    FROM tablename GROUP BY values)

Upvotes: 1

Konerak
Konerak

Reputation: 39763

Use GROUP BY and COUNT

SELECT column, COUNT(*)
FROM table
GROUP BY column

Upvotes: 4

sll
sll

Reputation: 62504

SELECT ColumnName, COUNT(*)
FROM TableName
GROUP BY ColumnName

Upvotes: 31

Related Questions