gpt4 book ai didi

postgresql - GROUP BY 并按提取为时间戳的日期排序

转载 作者:行者123 更新时间:2023-11-29 14:36:24 26 4
gpt4 key购买 nike

我有一个相当简单的查询:

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

我不太明白为什么会这样,因为日期出现在聚合函数中。有什么办法可以实现这种分组吗?

最佳答案

您不能按不存在的列进行排序。如果要对聚合中的值进行排序,请使用:

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;

关于postgresql - GROUP BY 并按提取为时间戳的日期排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44002204/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com