简体   繁体   English

PostgreSQL时间序列间隔

[英]Postgresql time series interval

I'm using postgres (RDS) for storing time series data. 我正在使用postgres(RDS)来存储时间序列数据。

Let's say my data look like this: 假设我的数据如下所示:

  • timestamp: (Index and partition key) 时间戳:(索引和分区键)
  • source: Integer Index 资料来源:整数索引
  • data: Binary json contains the data 数据:二进制json包含数据
 timestamp | source | data ---------------------+----------+------------------ 2017-01-24 19:24:41 | 1 | { some jsonb } 2017-01-24 19:25:41 | 1 | { some jsonb } 2017-01-24 19:25:41 | 2 | { some jsonb } 2017-01-24 19:26:41 | 3 | { some jsonb } 2017-01-24 19:32:41 | 1 | { some jsonb } 2017-01-24 19:33:41 | 2 | { some jsonb } 2017-01-24 19:45:41 | 3 | { some jsonb } 2017-01-24 19:50:41 | 1 | { some jsonb } 2017-01-24 19:56:41 | 1 | { some jsonb } 2017-01-24 20:01:41 | 1 | { some jsonb } 

I would like to sort the data by source and to have the data split by interval meaning let's say split by a 15 minutes interval. 我想按source对数据进行排序,并按间隔对数据进行拆分,这意味着要按15分钟的间隔进行拆分。 I also would like round the time when splitting it to interval. 我还想round分裂它间隔的时间。

So far I got 到目前为止,我知道了

SELECT date_trunc('hour', timestamp) + date_part('minute', timestamp)::int / 15 * interval '15 min' AS fifteen_minutes, data
FROM MY_TABLE
where source=1
GROUP BY data, fifteen_minutes
ORDER BY fifteen_minutes desc

Which returns 哪个返回

 fifteen_minutes | source | data ---------------------+----------+------------------ 2017-01-24 19:15:00 | 1 | { some jsonb } 2017-01-24 19:15:00 | 1 | { some jsonb } 2017-01-24 19:30:00 | 1 | { some jsonb } 2017-01-24 19:45:00 | 1 | { some jsonb } 2017-01-24 19:45:00 | 1 | { some jsonb } 2017-01-24 20:00:00 | 1 | { some jsonb } 

The issue is that I'm still getting multiple results for each interval. 问题是,每个间隔我仍然得到多个结果。 I would like to distinct by the interval and get the closest timestamp 我想按时间间隔distinct并获取最接近的时间戳

Ideally I would like to get: (single result per interval) 理想情况下,我希望得到:(每个间隔单个结果)

 fifteen_minutes | source | data ---------------------+----------+------------------ 2017-01-24 19:15:00 | 1 | { some jsonb } 2017-01-24 19:30:00 | 1 | { some jsonb } 2017-01-24 19:45:00 | 1 | { some jsonb } 2017-01-24 20:00:00 | 1 | { some jsonb } 

Any better idea? 有更好的主意吗? Thanks! 谢谢!

select distinct on (fifteen_minutes, source)
    fifteen_minutes, source, data
from (
    select 
        to_timestamp((extract(epoch from timestamp) / (15 * 60))::int * 15 * 60) as fifteen_minutes,
        data, timestamp
    from t
) t
order by
    fifteen_minutes, source,
    abs(extract(epoch from timestamp) - extract(epoch from fifteen_minutes))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM