我有一张桌子,每5分钟就会有一个浮动阅读。
我试图得到它们之间的差异总和 (以前阅读减去之前的读数)以一小时的格式。
我的尝试:
但我不想要power
的总和,而是差异的总和。
另一次尝试
select
date_trunc('hour', log_time) as per_hour,
sum(power-lag(power) over(order by log_time))
from log
where location_id='4'
and log_time BETWEEN now() - (interval '1 day') AND now()
group by per_hour, log_time, power
order by per_hour
我得到的错误:
ERROR: aggregate function calls cannot contain window function calls
LINE 3: sum(power-lag(power) over(order by log_time))
答案 0 :(得分:0)
正如您在评论中所定义的那样:
电源列之间的差异总和
select sum(diff) from
(
select power - coalesce(lag(power) over (),power) diff, * from log
) diff
我省略了group by
和order by
cos仍然不理解这个问题。