0

我正在尝试查询以下内容,但未执行

SELECT
  MAX(time) as last_time,
  column_A,
  column_B
from
  table_1
WHERE
  column_c='foo'

它回复以下错误消息

错误:列“column_A”必须出现在 GROUP BY 子句中或在聚合函数中使用

但是它正确回复了以下查询

SELECT MAX(time) as last_time from table_1 WHERE column_c='foo'

我究竟做错了什么?

4

2 回答 2

1

在另一个答案的评论中,您说您只需要一个值:

SELECT
  "time" as last_time,
  column_A,
  column_B
from table_1
WHERE column_c = 'foo'
order by "time" desc
limit 1
于 2013-05-27T11:13:14.980 回答
1

参考PostgreSQL 8.0 分组依据

如果聚合函数中有任何列,则所有不在聚合函数中的列都必须在 group by 子句中。

SELECT MAX(time) as last_time, column_A, column_B  
FROM table_1 WHERE column_c='foo' 
GROUP BY column_A, column_B
于 2013-05-27T06:31:23.307 回答