PostgreSQL-聚合计数
我有下面的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
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