Using AVG function on multiple columns
Hi there, For some reason this is more complicated than I thought. I'm trying to average fields from multiple columns as shown in the diagram below. when I use the average function: AVG(Fields!Col1.value + Fields!Col2.Value + Fields!Col3.Value) I would get 9.5 for Row1 etc; it's summing the column instead. Please can anyone help me with this or point me in the right direction? Would I need to use VB for this? Thanks column 1 column2 column3 Average Row1 3 3 3.5 3.17 Row2 2 2.5 2.5 2.33 Row3 4 2 2 2.67
April 27th, 2011 6:10am

You are getting correct value for row1. Is ta possible to do it via T-SQL? SELECT AVG(col) FROM ( SELECT 3 AS col UNION ALL SELECT 3 UNION ALL SELECT 3.5 ) AS DerBest Regards, Uri Dimant SQL Server MVP http://dimantdatabasesolutions.blogspot.com/ http://sqlblog.com/blogs/uri_dimant/
Free Windows Admin Tool Kit Click here and download it now
April 27th, 2011 8:37am

You are getting correct value for row1. Is ta possible to do it via T-SQL? SELECT AVG(col) FROM ( SELECT 3 AS col UNION ALL SELECT 3 UNION ALL SELECT 3.5 ) AS DerBest Regards, Uri Dimant SQL Server MVP http://dimantdatabasesolutions.blogspot.com/ http://sqlblog.com/blogs/uri_dimant/
April 27th, 2011 8:37am

Hi Cidr, From your information It seems your columns are static column, you could calculate their average value directly by typing in the expression for average column: =(SUM(Fields!Col1.Value)+SUM(Fields!Col2.Value)+SUM(Fields!Col2.Value))/3. If your columns are dynamical column, I think you could make sure of custom code to achieve this requirement, for the detail steps please follow these: Step one : Add custom code in report. 1. Select Report on top of the menu bar, select Report Properties. 2. Select “Code” tab on the left pane in the Report Properties dialog box. 3. Type below codes in the Custom code text box on the right side dim sum as integer=0 dim I as integer=0 public function Add(avg as integer) as integer sum=sum+avg I=I+1 return avg end function public function GetAvg() as Decimal GetAvg=sum/I sum=0 I=0 end function Step two: Call this function 1. In the data textbox please type in below expression =code.add(Fields!Data.Value) Note: Replace Data with your field. 2. In Average column type in =code.GetAvg() After these steps, I think you could get the requirement. If I misunderstand you, please feel free to let me know. thanks, Challen Fu Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Free Windows Admin Tool Kit Click here and download it now
April 29th, 2011 3:50am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics