这个Postgres查询有什么问题?

时间:2014-05-20 11:35:47

标签: sql postgresql

: SELECT created_at, count(created_at) as count FROM "candidates"  GROUP BY date(created_at)
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR:  column "candidates.created_at" must appear in the GROUP BY clause or be used in an aggregate function

created_at是日期时间列

candidates是一个表格

如何让它发挥作用?感谢。

1 个答案:

答案 0 :(得分:2)

问题是您正在选择created_at作为单独的列。您需要在其周围使用聚合函数(如MINMAX)或将其添加到GROUP BY

由于您已经按date(created_at)进行分组,因此您可能希望在选择中使用该分组。所以像这样:

SELECT
    DATE(created_at),
    COUNT(created_at) as count
FROM "candidates"
GROUP BY date(created_at)