What is the return type of SUM() in mysql?

c#, mysql

Solution

SUM in MySql will return a decimal or double value, depending on the type within "frequency". From the documentation for SUM:

The SUM() and AVG() functions return a DECIMAL value for exact-value arguments (integer or DECIMAL), and a DOUBLE value for approximate-value arguments (FLOAT or DOUBLE). (Before MySQL 5.0.3, SUM() and AVG() return DOUBLE for all numeric arguments.)

If you want an integer, you can use `Convert` to get one no matter what the source type happens to be:

total_freq = Convert.ToInt32(result["sum_of_freq"]);

The advantage here is the Convert.ToInt32 will work no matter what type of value is returned from the database, provided it is a numeric type.

Problem

I am writing a program in C#.NET I would like to gather the total frequency of a class (let imagine every class has many words and each word has its own frequency in the corresponding class) So i used the sum() function in mysql. But there is an error saying that my cast is wrong. ``` public void average_each_type() { MySqlDataReader result; int total_freq = 0; string type = ""; command.CommandText = "select class_name ,SUM(frequency) as sum_of_freq from training_set group by class_name "; result = command.ExecuteReader(); while (result.Read()) { total_freq = (int)result["sum_of_freq"]; //error happened here type = result["class_name"].ToString(); //.....then so on...// ```

Original source

Related problems