Skip to main content
1-Visitor
June 7, 2017
Solved

Calculate average value between multiple column in stream

  • June 7, 2017
  • 4 replies
  • 2978 views

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?

Best answer by CarlesColl

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

4 replies

1-Visitor
June 7, 2017

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

fmanniti1-VisitorAuthor
1-Visitor
June 7, 2017

So you confirm that DeriveFields is the best snippet.

Thank you

1-Visitor
June 7, 2017

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....

fmanniti1-VisitorAuthor
1-Visitor
June 7, 2017

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