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
The COUNT(DISTINCT) function returns the number of rows with unique non-NULL values. Hence, the inclusion of the DISTINCT keyword eliminates duplicate rows from the count. Its syntax is: COUNT(DISTINCT expr,[expr...])
You can count multiple COUNT() for multiple conditions in a single query using GROUP BY. SELECT yourColumnName,COUNT(*) from yourTableName group by yourColumnName; To understand the above syntax, let us first create a table.
The SQL SELECT DISTINCT Statement The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.
SELECT ColumnName, COUNT(*)
FROM TableName
GROUP BY ColumnName
Use GROUP BY
and COUNT
SELECT column, COUNT(*)
FROM table
GROUP BY column
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With