如何解析逐个错误的查询

时间:2013-05-27 06:29:34

标签: postgresql

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

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'

我做错了什么?

2 个答案:

答案 0 :(得分:1)

参考PostgreSQL 8.0 Group By

不在聚合函数中的所有列必须在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

答案 1 :(得分:1)

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

SELECT
  "time" as last_time,
  column_A,
  column_B
from table_1
WHERE column_c = 'foo'
order by "time" desc
limit 1