小编Mis*_*a M的帖子

PostgreSQL:如何获得值的总和以及进入每个值的总行数?

我有一张表格,MyData格式如下:

|id|theme_id|priority_id|value|
Run Code Online (Sandbox Code Playgroud)

我正在尝试为所有行获得以下结果:

theme_id | priority_id | total | sum
------------------------------------
    1    |      1      |   3   |  5
    1    |      2      |   5   |  12
    1    |      3      |   1   |  3
    2    |      1      |   2   |  6
Run Code Online (Sandbox Code Playgroud)

我们的想法是组中的所有行通过theme_idpriority_id再总结的数值为分组,以及,以获取进入那笔总行。

当我尝试以下操作时,我得到一个与 sum 列相等的 total 列:

|id|theme_id|priority_id|value|
Run Code Online (Sandbox Code Playgroud)

我也试过:

SELECT theme_id, priority_id, sum(value) AS values, count(id) as total
FROM MyData
GROUP BY theme_id, priority_id
Run Code Online (Sandbox Code Playgroud)

以上两个都给了我相同的输出:

theme_id | priority_id | total | sum
------------------------------------
    1    |      1 …
Run Code Online (Sandbox Code Playgroud)

postgresql aggregate

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

aggregate ×1

postgresql ×1