This is a migrated thread and some comments may be shown as answers.

Problem with Avg function in a Grouping field

9 Answers 79 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Scott Li
Top achievements
Rank 1
Scott Li asked on 08 May 2009, 06:54 PM
Hi,

I have a text box in a group section and the value is Avg(fields.fieldsName), it works fine if at least one record in detail section in the group has not null value on this column, however, I have a group, none of its records has value on this field, and the textbox in the grouping section through the exception:

#ERROR# Exception has been thrown by the target of an invocation.

is there any alternative I could fix the problem? 

Thanks

9 Answers, 1 is accepted

Sort by
0
Kathy Colson
Top achievements
Rank 1
answered on 08 May 2009, 08:49 PM
Hello,

This worked for me:   IsNull(Expression, ReplacementValue)

So my value in the textbox is:
=Avg(IsNull(Fields.MyFieldName,0))

0
Scott Li
Top achievements
Rank 1
answered on 09 May 2009, 02:50 PM
Thanks for your reply, yes, your solution will not cause exception, however, it will affect the average result, I don't count records having null value into average result. 
0
Steve
Telerik team
answered on 11 May 2009, 02:58 PM
Hi Scott Li,

This is expected as when your field contains only NULL or DbNull, there is no rational result we can return. If you break up the Avg function into Sum(fields.fieldsName)/Count(fields.fieldsName) you would see the actual error: "Attempted to divide by zero". We're not aware of your exact goals, but there are a few possible ways to handle this:
  • correct your data on the database server
  • disallow null for the field
If you have a suggestion on what we should return in such scenario - please let us know.
Best wishes,
Steve
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
anis
Top achievements
Rank 1
answered on 27 Feb 2017, 08:59 AM

Hi Scott Li,

May i know if any solution found for this issue?

Coz i'm facing the same problem.

Thx.

0
Stef
Telerik team
answered on 27 Feb 2017, 09:23 AM
Hello anis,

In recent versions the error is suppressed - if all values are null, the AVG=null. If there are non-null values, you will get the AVG calculated based on non-null values' sum and count.

You can also use a custom average based on sum and count, where you can use the ?: operator to prevent a zero division error.

Regards,
Stef
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
anis
Top achievements
Rank 1
answered on 27 Feb 2017, 10:07 AM

Hi Stef,

Thanks for your help.

I manage to get the solution already.

 

 

0
anis
Top achievements
Rank 1
answered on 12 Mar 2017, 10:50 AM

Hi Stef,

I encounter with the same problem when I tried to find the total average for average A, B, and C. I am using ?: operator  to find the average. It seems like it attempt to divide by zero. How can I prevent this? 

0
anis
Top achievements
Rank 1
answered on 12 Mar 2017, 10:51 AM

Hi Stef,

I encounter with the same problem when I tried to find the total average for average A, B, and C. I am using ?: operator  to find the average. It seems like it attempt to divide by zero. How can I prevent this? 

0
Stef
Telerik team
answered on 13 Mar 2017, 01:18 PM
Hello anis,

In general, the ?: operator does not evaluate both possible results and zero-division cases should not appear, unless the division is in the condition part. Check also if you sre using the IIf function, where both possible results are evaluated.

If you need further help, please post the code for generating the report and point the expression that throws the error on processing the report.

Regards,
Stef
Telerik by Progress
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Scott Li
Top achievements
Rank 1
Answers by
Kathy Colson
Top achievements
Rank 1
Scott Li
Top achievements
Rank 1
Steve
Telerik team
anis
Top achievements
Rank 1
Stef
Telerik team
Share this question
or