我有一个相当简单的查询:
SELECT table.foo, array_agg([ARRAY[EXTRACT(epoch FROM table.date), table.bar]) AS array
FROM table
GROUP BY table.foo,
ORDER BY table.date ASC;
当我运行此查询时出现错误:
ERROR: column "table.date" must appear in the GROUP BY clause or be used in an aggregate function
我不太明白为什么会发生这种情况,因为日期出现在聚合函数中。有没有办法实现这种分组?
答案 0 :(得分:2)
您不能按现有列排序。如果要在聚合中对值进行排序,请使用:
SELECT table.foo, array_agg([ARRAY[EXTRACT(epoch FROM table.date), table.bar] ORDER BY table.date ASC) AS array
FROM table
GROUP BY table.foo;