RANGE PRECEDING is only supported with UNBOUNDED

吃可爱长大的小学妹 提交于 2021-02-07 19:57:10

问题


I want to try avg() aggregation within a time window sql code

select
user_id,timestamp
avg(y) over(range between '5 second' preceding and '5 second' following),
from A

but the system report error

RANGE PRECEDING is only supported with UNBOUNDED

Is there any method to implement, say, a 10 second window for avg() window function?

frame of window function is as wide as range from n seconds preceding the timestamp of current row and m seconds following the timestamp of current row


回答1:


 RANGE PRECEDING is only supported with UNBOUNDED

Yep ... PostgreSQL's window functions don't yet implement ranges.

I've had many situations where they would've been useful, but it's a lot of work to implement them and time is limited.

Is there any method to implement, say, a 10 second window for avg() window function?

You will need to use a left join over generate_series (and, if appropriate, aggregation) to turn the range into a regular sequence of rows, inserting null rows where there's no data, and combining multiple data from within one second to a single value where there are multiple values.

Then you do a (ROWS n PRECEDING ...) window over the left-joined and aggregated data to get the running average.



来源:https://stackoverflow.com/questions/26791167/range-preceding-is-only-supported-with-unbounded

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!