如何计算数据库列中所有不同值出现的次数?

时间:2023-01-19 07:59:16

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.

我有一个Postgre数据库,有10列。第五列叫做列5。数据库中有100行,column5的可能值是c5value1、c5value2、c5value3…c5value29 c5value30。我想打印一个表,显示每个值发生的次数。

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?

这是什么命令?

2 个解决方案

#1


65  

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

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

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

#2


15  

Use the GROUP BY clause and the COUNT() aggregate function:

使用GROUP BY子句和COUNT()聚合函数:

SELECT column5, COUNT(column5) AS Occurences
FROM myTable
GROUP BY column5

#1


65  

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

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

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

#2


15  

Use the GROUP BY clause and the COUNT() aggregate function:

使用GROUP BY子句和COUNT()聚合函数:

SELECT column5, COUNT(column5) AS Occurences
FROM myTable
GROUP BY column5