שתף באמצעות


Datatable.compute("Avg()") for Null value

Question

Tuesday, March 17, 2009 4:57 PM

Dear,

I have a column: {2,2,null}. If i use datatable.compute to compute the avg of this column, will it return 2 or 1.66? In another word, will this function ignore the null value?

All replies (4)

Tuesday, March 17, 2009 6:02 PM ✅Answered

Ignores, I think you get 2 as the answer.Arjun Paudel


Wednesday, March 25, 2009 5:30 AM ✅Answered | 1 vote

Thanks Martin for the information, Sorry for my answer but I was meaning this way

 Dim dt As New DataTable  
        Dim dc As New DataColumn("Values")  
        dc.DataType = GetType(Double)  
        dt.Columns.Add(dc)  
        Dim dr As DataRow  
        dr = dt.Rows.Add  
        dr(0) = 2  
        dr = dt.Rows.Add  
        dr(0) = 2  
        dr = dt.Rows.Add  
        dr(0) = DBNull.Value  
         
        Dim avg As Double = dt.Compute("avg(Values)", Nothing)  
        MessageBox.Show(avg.ToString)  
 
        Dim avg1 As Double = dt.Compute("avg(Values)", " Values Is Not Null")  
        MessageBox.Show(avg1.ToString)  
 

Arjun Paudel


Wednesday, March 25, 2009 4:46 AM | 1 vote

A DataTable has a Compute() method to derive an aggregate value for a column expressed in the following format.

DataTable.Compute("AggregateFunction(DataColumn)", "condition|Nothing|null")

The Compute() method is passed two arguments in a comma-separated list of string values. The first argument is the name of an AggregateFunction() which has the name of a DataColumn included within paretheses; this is the DataSet column to which the aggregate function is applied. The second argument restricts the DataRows that are accessed. If all rows of the column are used in the function, there are no restrictions, so a null value (or the keyword Nothing) is passed. Otherwise, a condition is supplied to identify which rows are selected.

Available aggregate functions include those shown in the following table.

Avg() The average of values in a column
Count() The number of rows (values) in a column
Max() The largest value in a column
Min() The smallest value in a column
StDev() The standard deviation of values in a column
Sum() The sum of values in a column
Var() The statistical variance of values in a column

DataTable.Compute method http://msdn.microsoft.com/en-us/library/system.data.datatable.compute.aspx
Related thread: http://forums.asp.net/t/1323498.aspxPlease remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.


Wednesday, March 25, 2009 8:19 AM

Good and neat code sample! You're right.
I test your code sample and it works as you said "Ignores Null value, get 2 as the answer."
Thank you Arjun for your long-time friendly help.

Best regards,
Martin XiePlease remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.