PSQL每10分钟选择1条记录

时间:2016-06-03 15:49:31

标签: postgresql psql

我有一个数据库,我每分钟都插入一次数据,而且我希望每隔10分钟取出一次记录:

实施例: 我有从9:00到5:00的数据,每分钟有1条记录,我想退出9:10,9:20,9:30,9:40等。

现在我有:

select * from main where date = '2016-06-02' and time > '09:00:00' and time < '17:00:00'

这会拉动所有记录,但我想将其限制为仅10分钟。

3 个答案:

答案 0 :(得分:2)

我最终至少在某种程度上弄明白了

select * from main where date = '2016-06-02' and and time > '09:00:00' and time < '17:00:00' and  time::varchar like '%:%0:%';

答案 1 :(得分:1)

另一种方法,只是为了好玩......

select *
from   main
where date in (
        select generate_series(timestamp '2016-06-02 09:00:00', 
                               timestamp '2016-06-02 16:50:00',
                               '10 minute'::interval));

答案 2 :(得分:0)

我通过下面的查询解决了它。我的日期列称为ts。 该查询的作用是首先将5分钟存储桶中的每一行分组,按ts对其进行排序,然后获取第一个。 在10分钟内,将300替换为600。

select distinct on (CAST (extract(epoch from date_trunc('second', ts)) AS integer) / 300) ts
   ts, CAST (extract(epoch from date_trunc('second', ts)) AS integer) % 300 as sec,
   col_a, col_b


from main
where ts > '2020-02-25'

order by CAST (extract(epoch from date_trunc('second', ts)) AS integer) / 300 asc, ts asc

limit 500;