postgresql错误组和聚合错误

时间:2018-05-23 17:15:11

标签: postgresql odbc

我有一个代码用于我的表,以计算每天的航班日期和登记号码的下载次数。它给出了这个错误

SELECT  "public".aircraft.aircraft_id, COUNT(*) AS TOTALDOWNLOAD, "public".aircraft.register_number, to_char("public".flight.flight_date, 'DDMMYYYY') as flight_date , to_char("public".flight.health_start_date, 'hh24:mi') as health_start_date, to_char("public".flight.health_end_date, 'hh24:mi') as health_end_date,  "public".flight.maintenance_flight_time 
FROM  "public".flight  

INNER JOIN   "public".aircraft
ON
"public".flight.aircraft_id = "public".aircraft.id
WHERE "public".flight.maintenance_flight_time > 0 
Group by "public".flight.flight_date, "public".aircraft.register_number

错误详情如下:

ERROR [42803] ERROR: column "aircraft.aircraft_id" must appear in the GROUP BY clause or be used in an aggregate function;
Error while executing the query (PSQLODBC35W.DLL)

1 个答案:

答案 0 :(得分:0)

GROUP BY有点棘手。由于您只对COUNT感兴趣,因此我会减少查询以消除您不会使用的所有参数。如果你考虑一下,如果你按照flight_date进行分组,那么DB应该为其他一些领域返回什么 - 比如health_start_date?它不知道该怎么做,所以它会抛出一个错误。

从这个减少的版本开始。我不确定WHERE子句是否会引起麻烦。您可能需要GROUP BY flight_date别名而不是“public”.flight.flight_date。如果"public".flight.flight_date是时间戳,那么你肯定必须使用GROUP BY别名。根据这些表的大小,进行to_char转换可能会很慢。使用date("public".flight.flight_date可能会更快。

SELECT to_char("public".flight.flight_date, 'DDMMYY') as "flight date",
       COUNT(*) AS TOTALDOWNLOAD, "public".aircraft.register_number
FROM  "public".flight  
INNER JOIN  "public".aircraft ON
            "public".flight.aircraft_id = "public".aircraft.id
WHERE "public".flight.maintenance_flight_time > 0 
GROUP BY "flight date", "public".aircraft.register_number
ORDER BY "flight date"