question

Thomas JR avatar image
0 Likes"
Thomas JR asked Jeanette F commented

Accessing Statistic collector table in SQL query

I have a Statistic Collector table that contains waiting time for each patient in my model.

After the simulation is finished I want to calculate the average waiting time like this:

double total = Table.query("SELECT AVG(Waiting_time) FROM $1", Table("Patient_info"))[1][1];
double avg = total / n;

but I get this error:

exception: FlexScript exception: Invalid row number: 1 in Table.query() result table. at <no path> c: <no path> i: <no path>

I then tried to clone the result to a table:

Table.query("SELECT AVG(Waiting_time) FROM $1", Table("Patient_info")).cloneTo(Table("Some_table"));

but I only get an empty table with no rows and a single column named avg($1.Waiting_time)

I know that I could use Calculated tables as an alternative, but I'm still curious to why the above isn't possible?

FlexSim 21.0.0
statistics collectorflexsim 21.0.0sql queries
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jeanette F avatar image Jeanette F ♦♦ commented ·

Hi @Thomas JR, was martin.j's answer helpful? If so, please click the red "Accept" button on their answer. Or if you still have questions, add a comment and we'll continue the conversation.

If we haven't heard back from you within 3 business days we'll auto-accept an answer, but you can always unaccept and comment back to reopen your question.

0 Likes 0 ·

1 Answer

martin.j avatar image
1 Like"
martin.j answered Thomas JR commented

Hi Thomas,

The error that you get is caused by you trying to acces a row in a result table that has no rows/result. Why your query fails to get a result I cannot say without more information, but it could be that you have entered the wrong column name or table name - the query itself looks fine. Copy the names from the table with Ctrl+C of needed.

You can make your code more error safe by putting the query result in a temporary table and check if it holds any results before doing anything furhter with it. The following script has worked for me to produce an average from 10 Waiting_time entries in the Patient_info table.

Table result = Table.query("SELECT AVG(Waiting_time) FROM $1", Table("Patient_info"));
double avg = -1;
if (result.numRows) { //iF there are rows in the result table our query worked     
    avg = result[1][1];  //Now we can try to access the rows. 
}
return avg;

I am unsure what the code "double avg = total / n;" is supposed to do. Remeber that if you use the keyword AVG in the query it will take the average of all the queried entries - not the total sum (Keyword SUM) but the average.

If you want more help with your query, please provide a copy of a model where he problem exists.

EDIT: I just tested your query with a statistics collector instead of a global table and realized why your script does not work. You apparently cannot query a statistics collector table like you would a global table using the Table("Table_Name") syntax as a query parameter.

This would be the correct way to query a statistics collector:

Table result = Table.query("SELECT AVG(Waiting_time) FROM Patient_info");
double avg = -1;
if (result.numRows) {    
     avg = result[1][1];
}
return avg;

However this hardcodes the name of the collector into your query string. If you really need it to be a query parameter, you can use the following workaround where you put the statistics collector data table in a temporary table and use that as a parameter in your query:

//Put the Collector data into a Table variable
Table table = Table("Patient_info").as(Object).attrs.stats.find("data");
//Now we can use the 'table' variable as a parameter in our query
Table result = Table.query("SELECT AVG(Waiting_time) FROM $1", table);
double avg = -1;
if (result.numRows) {
     avg = result[1][1];
}
return avg;

It is not pretty but it works. There may be a more correct way of doing this that I don't know about though.

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Thomas JR avatar image Thomas JR commented ·

Yea, I also suspected that the problem originates from whether you want to query a global table or statistic collector table. I‘m gonna try out your approach.

0 Likes 0 ·