Running average database tags

What would be the best method to have a tag that is read once a second to calculate a runnning average (lets say 1 minute intervals) and store that data to the database.

In the same Transaction group running your raw data, you can add an Exression Item with the SQL Query:

NOTE: Mysql Query. Your mileage may differ.

SELECT Avg(Input) FROM Table Where t_stamp>=date_sub(now(), interval 60 second)

This will get the average value over the last 60 seconds and let you store it in the table.

If, however, this is something you are just looking to graph, the easy chart will already do that for you.



Sorry, it just occurred to me that you may be trying to do this within an SQLTag

Really, same thing applies to SQLTags and the easy chart. Historian Tags aren’t very SQL friendly. :wink:

Thanks for the tip. However I was needing to address the concern of reporting on historical data. Data is monitored once a second but I would need minute, hour, and day averages to be stored in the database so that when it came time to query for reports it wouldnt take so long. I would need to set up somthing like rolling 24 hrs at 1 minute average , rolling 30 days at hour average, and rolling 365 days at day averages

Oh! So my first one is valid, then. :thumb_right:

[quote=“JordanCClark”]In the same Transaction group running your raw data, you can add an Exression Item with the SQL Query:

NOTE: Mysql Query. Your mileage may differ.

SELECT Avg(Input) FROM Table Where t_stamp>=date_sub(now(), interval 60 second)

This will get the average value over the last 60 seconds and let you store it in the table.

[/quote]

Instead of putting it into the same group, you can put that query into an Expression Item in a separate transaction group and save it to different table.

Seems to work as needed\expected. Thanks!