Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

sql - How to count number of occurrences for all different values in database column?

I have a Postgre database that has say 10 columns. The fifth column is called column5. There are 100 rows in the database and possible values of column5 are c5value1, c5value2, c5value3...c5value29, c5value30. I would like to print out a table that shows how many times each value occurs.

So the table would look like this:

Value(of column5)          number of occurrences of the value
     c5value1                              1
     c5value2                              5
     c5value3                              3
     c5value4                              9
     c5value5                              1
     c5value6                              1
        .                                  .
        .                                  .
        .                                  .

What is the command that does that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Group by the column you are interested in and then use count to get the number of rows in each group:

SELECT column5, COUNT(*)
FROM table1
GROUP BY column5

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...