Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PostgreSQL-聚合计数_Sql_Postgresql_Aggregate - Fatal编程技术网

PostgreSQL-聚合计数

PostgreSQL-聚合计数,sql,postgresql,aggregate,Sql,Postgresql,Aggregate,我有下面的PostgreSQL表,我正试图为其获取一些总计 number | firstused | monthtotal --------+------------+------------ 264 | 2017-11-02 | 1 269 | 2017-11-02 | 1 262 | 2017-01-02 | 3 270 | 2017-11-02 | 2 268 | 2017-10-02 | 1 2

我有下面的PostgreSQL表,我正试图为其获取一些总计

number | firstused  | monthtotal 
--------+------------+------------
264 | 2017-11-02 |          1
269 | 2017-11-02 |          1
262 | 2017-01-02 |          3
270 | 2017-11-02 |          2
268 | 2017-10-02 |          1
265 | 2017-04-02 |          1
267 | 2017-11-02 |          1
263 | 2017-01-02 |          3
266 | 2017-04-02 |          3
我想要一个可以返回如下结果集的查询的帮助。每一行都有一年/一个月内有多少个月具有相同的值。该示例的最后两行显示,2017-2011年,共有3个月数1和1个月数2

firstused | monthtotal | total
----------+------------+------
2017-01-02| 3          | 2
2017-04-02| 1          | 1
2017-04-02| 3          | 1
2017-10-02| 1          | 1
2017-11-02| 1          | 3
2017-11-02| 2          | 1
生成上述条件的查询也可以表示为Hibernate条件

select firstused, monthtotal, count(*) as total
from t
group by firstused, monthtotal