Weighted Average


How would you calculate a weighted average of floatvalue based upon duration (t_stamp) in SQL or Python ?
Sample data :

floatvalue, t_stamp
110.41, '2011-08-18 16:53:58'
94.10, '2011-08-18 16:54:28'
91.96, '2011-08-18 16:54:58'
130.53, '2011-08-18 17:01:28'
121.35, '2011-08-18 17:01:58'

Thank you

Not sure I understand. If each sample is every thirty seconds, how is it weighted? Are you just looking for the average from the last few minutes?

Hi Jordan,

The samples are usually every 30s, but sometimes not like between the 3rd and the fourth. In fact, the table is bigger than this sample ; data are stored as sql tag history that’s the reason why time weight is important.

Hope this clearer.

Where do you want to do this from? Outside of Ignition? Because if it’s inside, you should be able to use the queryTagHistory function with an aggregation mode of “Average” and a result size of 1. However, we’re aware that this doesn’t currently work as one would expect, but we’re trying to fix it for 7.3 (it currently returns 2 rows for the result- one for the start time bounding value, and one for the rest- unfortunately causing the average to get skewed a bit).

However, to get around that, you can query with a result size that’s slightly larger (say 10). This will give you evenly spaced values that are themselves time-weighted, allowing you to do your own simple average on them.

Of course, I’m probably writing all of this for nothing, because if you wanted to use scripting, you could just write the average function yourself… :confused: