如何计算数据库列中所有不同值的发生次数? [英] How to count number of occurences for all different values in database column?

查看:175
本文介绍了如何计算数据库列中所有不同值的发生次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Postgre数据库,说10列。第五列称为 column5 。数据库中有100行, column5 的可能值为 c5value1,c5value2,c5value3 ... c5value29,c5value30 。我想打印一个表,显示每个值发生多少次。

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.

所以表格如下所示:

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? Thanks for help

推荐答案

按照您感兴趣的列分组,然后使用count获取行数在每个组中:

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

这篇关于如何计算数据库列中所有不同值的发生次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆