首页 > 解决方案 > 时间尺度上的计算字段

问题描述

我正在尝试使用传感器数据将计算字段添加到我的超表中,例如,我想添加体积读数的导数以获得体积流量。

我试图用一个连续的聚合来做到这一点,但我得到了invalid SELECT query for continuous aggregate.

create view public.readings_raw_with_calc_fields
WITH (timescaledb.continuous)
as
select 
serial,
time,
type,
value
from public.readings_raw
union all
select 
serial,
time,
'Volume flow calc.' as type,
(lead(value) over (partition by serial,devicetype,manufacturer order by time))-value as value
from 
public.readings_raw
where type='Volume'

这是因为lead函数还是因为连续聚合中必须有聚合函数?什么是最好的做法?我可以做一个每分钟在原始表中插入数据的工作,但是如果新数据被及时插入,我就无法捕捉到(这是一个相当大的表,所以我不能每分钟都遍历整个表)。

标签: sqlpostgresqltimescaledb

解决方案


窗口函数和 UNION 都将阻止连续聚合工作。

在这种情况下,由于每行执行的计算非常低,时间索引和常规视图可以提供所需的性能。


推荐阅读