Skip to content
Advertisement

Charting the data from database and merging row with the same value

I want to create chart that read the category column in my database.. I have repeating category because its general term and i notice this.

I have many “short” slice:

..

I dont have any idea how to merge the category with the same name and add its value..

Here’s my code

MySqlCommand cmd = new MySqlCommand("Select * From sketchit.inventory;", dc.con);
MySqlDataReader myreader;
try {
    dc.con.Open();
    myreader = cmd.ExecuteReader();
    while (myreader.Read()) {

        this.chart1.Series["Report 1"].Points.AddXY(myreader.GetString("Category"), myreader.GetInt32("Stocks"));
        chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;

    }
}
catch(Exception ex) {
    MessageBox.Show(ex.Message);
}
dc.con.Close();

Advertisement

Answer

Use aggregation.

SELECT category,
       sum(stocks) stocks
       FROM sketchit.inventory
       GROUP BY category;
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement