Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to obtain the average by group in a mysql database and grapg them in a chart.
I am able to group the items in the db and graph, but when I use the avg function on a column, I get column not found exception.
Can someone please help?
Thanks.

What I have tried:

public void GetData()
    {
        String constr = System.Configuration.ConfigurationManager.ConnectionStrings["web"].ToString();
        //MySqlConnection con = new MySqlConnection(constr);

        using (MySqlConnection con = new MySqlConnection(constr))
        {
            MySqlCommand cmd = new MySqlCommand("select " + col1.Text.ToString() + ", AVG(" + col2.Text.ToString() + ") from avance_presupuestario group by "+col1.Text, con);
            //Series s = Chart1.Series["Series1"];
            Series series = Chart1.Series["Series1"];
            con.Open();
            MySqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                series.Points.AddXY(rdr[col1.Text].ToString(), rdr[col2.Text].ToString());
                //this.Chart1.Series["Series1"].Points.AddXY(rdr.GetString(col1.Text), rdr.GetInt32(col2.Text));
            }
        }
    }
Posted
Updated 21-Dec-17 4:51am

1 solution

I just found the solution to my problem.
MySqlCommand cmd = new MySqlCommand("select " + col1.Text.ToString() + ", AVG(" + col2.Text.ToString() + ") AS " + col2.Text + " from avance_actividad group by " + col1.Text, con);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900