简体   繁体   中英

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 eg 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

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

Use GROUP BY and COUNT

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM