Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 条件时间序列数据聚合_Postgresql_Time Series_Aggregate Functions - Fatal编程技术网

Postgresql 条件时间序列数据聚合

Postgresql 条件时间序列数据聚合,postgresql,time-series,aggregate-functions,Postgresql,Time Series,Aggregate Functions,我有一个关系数据库postgres,它有一个保存时间序列度量的表。每行由-obj_id、度量_id、时间戳和值组成 让我们假设我有3个与代码相关的度量-1、4、5。我想过滤掉所有对象,对于相同的时间戳,让我们假设所有度量的时间戳都在固定的时间间隔内,度量1

我有一个关系数据库postgres,它有一个保存时间序列度量的表。每行由-obj_id、度量_id、时间戳和值组成

让我们假设我有3个与代码相关的度量-1、4、5。我想过滤掉所有对象,对于相同的时间戳,让我们假设所有度量的时间戳都在固定的时间间隔内,度量1<10,度量4+度量5<10,以及该事件发生的特定时间戳

一个更具体的例子:

obj_id       metric_id         timestamp        value
------------------------------------------------------
1             1                83827             9
1             4                83827             2
1             5                83827             1
2             1                73261             11
2             4                73261             2
2             5                73261             5
1             1                92381             24
1             4                92381             10
1             5                92381             100
2             1                38239             7
2             4                38239             3
2             5                38239             4
预期结果将是:

obj_id     timestamp
---------------------
  1         83827
  2         38239
我正在尝试创建一个高效的查询。这就是我想要得到相同时间戳的4+5之和的想法,但我不确定将这些查询粘合在一起的最佳方法是什么:

SELECT obj_id, timestamp, sum(value) AS x
FROM metric
WHERE metric_id = 4 OR metric_id = 5
group by obj_id, timestamp
我不知道如何添加到这个查询度量1中,我们应该单独查询,然后根据obj_id和时间戳过滤出结果


我考虑过可能使用自连接,通过时间戳将同一个表的两个内部选择连接起来。

也许这可以做得更好,但要理解这一点感觉很简单,需要9.4+:

从布尔值到整数的转换产生0或1

select obj_id, timestamp
from metric
where metric_id in (1,4,5)
group by obj_id, timestamp
having
    sum(value * (metric_id in (4,5))::integer) < 10
    and
    sum(value * (metric_id = 1)::integer) < 10

元组2,173261,1失败,这是因为metric1>10。。。这不是一个要求吗?FWIW,查询中的数据框在最后一列中给出的值是11,而不是1。我的意思是,如果该元组被插入表中。绝对精彩,效果非常好,我甚至可以阅读它。谢谢
select obj_id, timestamp
from metric
where metric_id in (1,4,5)
group by obj_id, timestamp
having
    sum(value * (metric_id in (4,5))::integer) < 10
    and
    sum(value * (metric_id = 1)::integer) < 10