Time-weighted average: LOCF from last point until the end of the range #697
milgner
started this conversation in
Bugs / Internal Improvements
Replies: 1 comment
-
I believe switching to |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
While trying to implement time-weighted averages into my application, I stumbled over this somewhat confusing behaviour:
I want to calculate the time-weighted average (just one value) for a measurement in a given time range.
The data should be interpreted in LOCF-fashion. Since it is spaced irregularly, I'm first running a query in the form
Now the
prepared_data
expression contains both a starting value with the timestamp and the regular remaining records.It seemed like just invoking
average(time_weight('locf', ts, value))
would be enough.But unfortunately, the last data point doesn't get carried forward until the end of the queried timerange. Which is understandable because there's no way for the function to know how long the queried timerange is supposed to be, which would be required for the
locf
mode.This can be easily demonstrated using
which only yields
null
as a result. Even adding aWHERE
clause for some magic query introspection which apparently the functions do in some places, doesn't help:still produces
null
.Now of course I can work around that by adding another
UNION ALL
statement to produce a "virtual" last row. But it seems to me like there should be a better approach. Not sure if I'm doing something wrong or whether these constructs should be enhanced in some way.One thing that comes to mind is that instead of passing the interpolation method as a string, would be nice to have a separate
interpolation
type with alocf_interpolation
constructor function which (optionally) receives both an expression to look up the preceding value as well as the time range. A correspondinglinear_interpolation
wouldn't need those then.Looking forward to your suggestions.
Beta Was this translation helpful? Give feedback.
All reactions