Hlep with creating a Rolling average

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
Post Reply
dcook
Posts: 3
Joined: Tue Apr 09, 2013 10:47 am

Hlep with creating a Rolling average

Post by dcook »

I am trying to create a rolling 24hr average. So far no success. The analog value I would like to average is already being logged in the iX data logger. I figure there must be a way to pull the current time, and query the data log for the previous 24hrs worth of data points, add them, divide by count, and write to an internal tag.

Any pointers, or help is welcomed.

mark.monroe
Posts: 824
Joined: Tue Mar 13, 2012 9:53 am

Re: Hlep with creating a Rolling average

Post by mark.monroe »

This forum post has an example project on how to use SQL to query a datalogger. That is where I would start. To get the current time there is a system tag that display it.
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

User avatar
Edmund
Posts: 92
Joined: Thu Nov 29, 2012 2:27 pm

Re: Hlep with creating a Rolling average

Post by Edmund »

Simple, query the database (the datalogger table) for the average for the last 24hours, read it with a sql_reader and write the result to a tag.

Code: Select all


Sql_Command = new SqlCeCommand( "SELECT AVG(Tag_1) FROM DataLogger1 WHERE Time > DATEADD(HH,-24,GETDATE())", Sql_Connection);
Sql_Reader = Sql_Command.ExecuteReader();
while(Sql_Reader.Read())
	Globals.Tags.Tag_2.Value = Sql_Reader[0].ToString();

AVG(column) will give you the average number, MAX(column) the maximum value, MIN(column) the smallest and SUM(column) the total sum.

Best reagrds
Edmund Andersson

AITECH AB

Part of Beijer Integrator Group

Post Reply