计算属于每个5分钟时段的时间戳记

时间:2018-10-20 18:49:51

标签: sql postgresql postgresql-9.5

我有一张时间戳表。我想将它们分组为5分钟的时段,并获取该时段中的时间戳记。我在使SQL完全正确方面遇到一些麻烦。我正在使用Postgres。告诉我最后一行中的timestamp列不存在,但已定义为别名。

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)

1 个答案:

答案 0 :(得分:2)

我认为您的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