Reputation: 1468
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
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