Page 1 of 1

Hlep with creating a Rolling average

Posted: Tue Apr 23, 2013 11:56 am
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.

Re: Hlep with creating a Rolling average

Posted: Tue Apr 23, 2013 1:04 pm
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.

Re: Hlep with creating a Rolling average

Posted: Thu Apr 25, 2013 9:26 am
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