Jump to content
  • 0

SQL - problem with SELECT AVG statement in Calculated Value / DataPage


Johan
 Share

Question

 

I have encountered the following issue when using SQL statements in a Calculated Value in a DataPage:

I want to calculate average scores in a database based on the values in specific fields in the same record. These fields function as filters.

Example of record:

Company_Name              Company_Size_Employees                  Company_Revenues              Score                    Context_AVG_Employees                         Context_AVG_Revenues

            ABC Inc.                                  <100                                                            < 50 M USD                               9                         AVG of SCORE for all records in             AVG of Score for all records in                                                                                                                                                                                                                                                                                                                                 the DB where employees <100              the DB where revenue < 50 M USD                                                                                                                                                                                                                                                                                                                                 -> SQL statement                                              -> SQL statement

 

In the DataPage I have fields with SQL statements defined as Calculated Value to capture the results:                                                                                                             

Context_AVG_Employees: IsNull((SELECT AVG(Score) FROM Assessment WHERE Company_Size_Employees = [@field:Company_Size_Employees]),0)                 ->  Result is 12 = CORRECT

Context_AVG_Revenues:  IsNull((SELECT AVG(Score) FROM Assessment WHERE Company_Revenues = [@field:Company_Revenues]),0)                      ->  Result is 12 = NOT CORRECT,  should be 6                                                                                                                                                                                                                                                                                                                                                                                         

It seems that the second field takes also the value of the first calculation.

What could be the problem and what is the solution ?

Thanks !

 

 

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Hello @Johan

Is this a Report DataPage? Perhaps you can try this query and add 'target.' prefix to your record fields.

Context_AVG_Employees: IsNull((SELECT AVG(Score) FROM Assessment WHERE Company_Size_Employees = target.[@field:Company_Size_Employees]),0) 

Context_AVG_Revenues:  IsNull((SELECT AVG(Score) FROM Assessment WHERE Company_Revenues = target.[@field:Company_Revenues]),0)  


We usually add 'target.' to make sure we are referencing the current row.

You may refer to this article: https://howto.caspio.com/datapages/reports/advanced-reporting/calculations-in-forms-and-reports/

Link to comment
Share on other sites

  • 0

Hello @Johan,

You may use statements to retrieve data and different functions on the DataPage level. Supported functions can be found in this article  https://howto.caspio.com/function-reference/

However, the Insert/Update/Delete actions are available in Tasks and Triggered Actions.

Please refer to these articles:
https://howto.caspio.com/tasks/
https://howto.caspio.com/tables-and-views/triggered-actions/

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...