# TimeWeightedAvg

# Usage

This function is used to calculate the time-weighted average of time series. Time is weighted using the linearly interpolated integral of values, and the output equals to the area divided by the time interval using the same time unit. For more information of the area under the curve, please refer to Integral function.

Name: TIMEWEIGHTEDAVG

Input Series: Only support a single input numeric series. The type is INT32 / INT64 / FLOAT / DOUBLE.

Output Series: Output a single series. The type is DOUBLE. There is only one data point in the series, whose timestamp is 0 and value is the time-weighted average.

Note:

  • The time-weighted value equals to the integral value with any unit divided by the time interval of input series. The result is irrelevant to the time unit used in integral, and it's consistent with the timestamp precision of IoTDB by default.

  • NaN values in the input series will be ignored. The curve or trapezoids will skip these points and use the next valid point.

  • If the input series is empty, the output value will be 0.0, but if there is only one data point, the value will equal to the input value.

# Examples

Input series:

+-----------------------------+---------------+
|                         Time|root.test.d1.s1|
+-----------------------------+---------------+
|2020-01-01T00:00:01.000+08:00|              1|
|2020-01-01T00:00:02.000+08:00|              2|
|2020-01-01T00:00:03.000+08:00|              5|
|2020-01-01T00:00:04.000+08:00|              6|
|2020-01-01T00:00:05.000+08:00|              7|
|2020-01-01T00:00:08.000+08:00|              8|
|2020-01-01T00:00:09.000+08:00|            NaN|
|2020-01-01T00:00:10.000+08:00|             10|
+-----------------------------+---------------+

SQL for query:

select timeweightedavg(s1) from root.test.d1 where time <= 2020-01-01 00:00:10

Output series:

+-----------------------------+--------------------------------+
|                         Time|timeweightedavg(root.test.d1.s1)|
+-----------------------------+--------------------------------+
|1970-01-01T08:00:00.000+08:00|                            5.75|
+-----------------------------+--------------------------------+

Calculation expression: