2017-08-04 53 views
0

我有下面的PostgreSQL表,我試圖得到一些聚合總數。PostgresSQL - 聚合計數

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-11它有monthtotal 1的3個計數和monthtotal 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 

的1個計數可能產生上述的查詢也可以表示爲一個Hibernate標準。

謝謝大家的任何援助

回答

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