Skip to content
Advertisement

How can I get a count of how many times a distinct value appears in a column?

How can I find out how many times each distinct value appears in a column?

NOTE: This is not a case of “how do I find distinct values in a column?”

For example, this column contains five values:

Col1
AA
AA
AB
AC
AD
AA
AC
AE

Which query can I run that would result in: the following?

AA: 3
AB: 1
AC: 2
AD: 1
AE: 1

So far I can only get the following:

SELECT COUNT(DISTINCT(Col1))
FROM table1

Result: 5

I have tried different variations of it but get the same result each time.

Advertisement

Answer

You are looking for GROUP BY:

select col1, count(*)
from table1
group by col1;
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement