Retrieve Unique Values and Counts For Each

2020-05-23 02:41发布

Is there a simple way to retrieve a list of all unique values in a column, along with how many times that value appeared?

Example dataset:

A
A
A
B
B
C

... Would return:

A  |  3
B  |  2
C  |  1

2条回答
Lonely孤独者°
2楼-- · 2020-05-23 02:58

Use GROUP BY:

select value, count(*) from table group by value

Use HAVING to further reduce the results, e.g. only values that occur more than 3 times:

select value, count(*) from table group by value having count(*) > 3
查看更多
狗以群分
3楼-- · 2020-05-23 03:06
SELECT id,COUNT(*) FROM file GROUP BY id
查看更多
登录 后发表回答