我的PostgreSQL语句出了什么问题?这在MySql上运行

时间:2015-01-29 06:28:57

标签: postgresql group-by

在PostgreSQL中运行以下查询时: -

SELECT group_, 
       COUNT(*), 
       MIN(time) 
FROM TRIP 
WHERE time >= NOW() 
GROUP BY group_ 
ORDER BY time ASC

我收到错误: -

ERROR: column "trip.time" must appear in the GROUP BY clause or be used in an aggregate function

我不明白。 Isn&#t; t min是一个聚合函数吗?此查询在MySql上运行。

1 个答案:

答案 0 :(得分:0)

time不是group by项或聚合术语,因此您不能在具有group by子句的查询中使用它。据推测,您打算按min(time)订购,可以明确说明:

SELECT group_, 
       COUNT(*), 
       MIN(time) 
FROM TRIP 
WHERE time >= NOW() 
GROUP BY group_ 
ORDER BY MIN(time) ASC

或者通过位置索引:

SELECT group_, 
       COUNT(*), 
       MIN(time) 
FROM TRIP 
WHERE time >= NOW() 
GROUP BY group_ 
ORDER BY 3 ASC