SQL return 计数字段的总和。不允许我删除 GroupBy

SQL return a sum of a count field. Will not allow me to remove GroupBy

我有一个有点复杂的(对我来说)SQL查询。
我需要计算每个月 14:00:00 和 16:00:00 之间的所有记录,并且必须从字段内的时间戳中提取小时才能这样做。在申请 Java 程序

之前,我一直在使用 PGAdmin3 Postgre 练习 SQL 语句
   SELECT
    extract(hour from trips.tpep_pickup_datetime) AS hour,
    count(*)  AS aCount
FROM
    trips

where 

extract(hour from trips.tpep_pickup_datetime) >=14 and 

extract(hour from trips.tpep_pickup_datetime) <=16 and

month ='Jan'

group by
    1,extract(month from trips.tpep_pickup_datetime); 

这为我带来了查询中的 Jan 结果。我只想要总和

    hour  |  aCount
-----------------------
     16   |  16111 
     14   |  13301
     15   |  14796

!!!!更新!!!是:我曾尝试删除群组,我收到一条错误消息

    ERROR:  column "trips.tpep_pickup_datetime" must appear in the GROUP BY clause or be used in an aggregate function
LINE 2:     extract(hour from trips.tpep_pickup_datetime) as hour,
                              ^
********** Error **********

ERROR: column "trips.tpep_pickup_datetime" must appear in the GROUP BY clause or be used in an aggregate function
SQL state: 42803
Character: 31

我实际上需要 returned 的总和,而不是细分。 (44,208) 原因是我使用 Java 方法来 return 按月列出的给定时间之间的数字记录。

[第 mnth 是月份名称的数组]

private void conRecTime(int time1, int time2) {
    String mnth;
    try {
        Statement stat1 = conn.createStatement();
        ResultSet rs1;
        for (int i = 0; i < monthsArr.length; i++) {
            mnth = monthsArr[i];

            rs1 = stat1.executeQuery(
                    "SELECT "
                    + "extract(hour FROM trips.tpep_pickup_datetime)AS hour,"
                    + "count(*) AS COUNT "
                    + "FROM trips "
                    + "WHERE "
                    + "extract(hour from trips.tpep_pickup_datetime) >="+ time1 
                    + "AND extract(hour from trips.tpep_pickup_datetime) <="+ time2
                    + "AND month ='"+ mnth + "'"
                    + "GROUP BY 1,extract(month from trips.tpep_pickup_datetime);");
            rs1.next();
            count = rs1.getInt(2);

            System.out.println("Count of records of " + mnth + " between the times " + time1 + " and " + time2 +  " is " + count + " records"  );
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

它只是读取每个月的第一行,即第 16 小时(4 点钟),但我无法在打印语句中使用该方法到达第 14 或 15 小时。

我有点卡住了,因为我似乎对 SQL 语句所做的任何事情都会把它搞砸,而且我不喜欢它(我尝试了一些嵌套语句,但它只是被击中和错过).这是我第一次使用 SQL 和 Java 所以建议如何构造 Java 方法或 SQL 语句将不胜感激

如果不需要细分,只需放弃 group by 子句,并从 SELECT 列表中删除除计数之外的所有内容:

SELECT COUNT(*)  AS aCount
FROM   trips
WHERE  EXTRACT(HOUR FROM trips.tpep_pickup_datetime) >= 14 AND
       EXTRACT(HOUR FROM trips.tpep_pickup_datetime) <= 16 AND
       month = 'Jan'