cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X

Calculate average value between multiple column in stream

fmanniti
9-Granite

Calculate average value between multiple column in stream

Hi

Let's say I have a stream with this Shape

TimestampTemperature_1Temperature_2Temperature_3Temperature_4Temperature_5Temperature_6Some other stuff...

And I would like this output

TimestampAverageMinimumMaximum
Average value between Temperature_1...Temperature_6Minimum value between Temperature_1...Temperature_6Maximum value between Temperature_1...Temperature_6

Is there any snippets which does this without do any loop?

Rightnow I am using DeriveFields but I don't think it's the proper way because what I have to do is

var expr = '';

expr += '(Temperature_1+Temperature_2+Temperature_3+Temperature_4+Temperature_5+Temperature_6)/6,';

var params = {

    types: 'NUMBER, NUMBER, NUMBER',

    t: stream,

    columns: 'Average,Minimum,Maximum'

    expressions: expr

};

var outputTable = Resources["InfoTableFunctions"].DeriveFields(params);

outputTable.RemoveField(<name of the field I want to remove>);

outputTable.RemoveField(<name of the field I want to remove>);

outputTable.RemoveField(<name of the field I want to remove>);

outputTable.RemoveField(<name of the field I want to remove>);

...

result = outputTable

but this has two problems:

1) DeriveFields I think is unproperly used because it's supposed to be used when the two infotables have the same fields, here they only have one in common then I have to remove all the other extra fields;

2) To calculate max e minimum temperature I use the function Math.max(Temperature_1, ..., Temperature_6) but expression parameter doesn't want expressions with comma inside so this won't work.

Do you have any better suggestion?

1 ACCEPTED SOLUTION

Accepted Solutions

1) Yes you have to remove the extra columns

2) To use services which requiere more than one parameter you should use ";" as separator for expressions instead

View solution in original post

4 REPLIES 4

1) Yes you have to remove the extra columns

2) To use services which requiere more than one parameter you should use ";" as separator for expressions instead

So you confirm that DeriveFields is the best snippet.

Thank you

Yes, but anyway you may do performance tests comparing iteration (Javascript) vs using DeriveFields (Java), becouse at the end the iteration it's always there, and as you are using Expressions on each Java iteration should do a Javascript call....

I see.
As long as I could check, Iteration is a quite long process when I have a stream on a long period (it takes something like 4 or 5 seconds) so I'm looking for the fastest (and CPU lighter) way.
I'll do all the tests.
Thank you

Top Tags