获取落入每 5 分钟桶中的时间戳计数

Getting a count of timestamps that fall into each 5 minute bucket

我有 table 个时间戳。我想将它们分组到 5 分钟的桶中,并获取该桶中的时间戳记数。我在正确设置 SQL 时遇到了一些问题。我正在使用 Postgres。它告诉我最后一行的时间戳列不存在,但它被定义为别名。

SELECT
    TIMESTAMP WITH TIME ZONE 'epoch' +
    INTERVAL '1 second' * round(extract('epoch' from my_timestamp) / 300) * 300
    as timestamp,
    count(my_timestamp)
FROM logs
GROUP BY
round(extract('epoch' from timestamp) / 300)

我认为您的 GROUP BY 已关闭。试试这个:

SELECT TIMESTAMP WITH TIME ZONE 'epoch' + INTERVAL '1 second' * round(extract('epoch' from my_timestamp) / 300) * 300 as timestamp,
       count(*)
FROM (values (now())) logs(my_timestamp)
GROUP BY timestamp