0

This is related to Winforms/C#/ MYSQL. I have the following table:

Net Amount / DATE

50 /2013-3-15

40/ 2013-11-10

30 / 2013-3-25

The program lets the user picks the month he wants to see data from and ideally the output should be a graph ( which i know how to do) to demonstrate the total net amount vs month. The output should be something like the following:

Net Amount / Month

80/March

40/November

Someone suggested I try this :

var selectdataforGraph = "SELECT SUM(AMOUNT_NET) AS Amount_Net, MONTH(date_Check) as Month FROM testingproject.incomeinformation WHERE";
                var monthList = string.Join(",", selectedMonth);
                selectdataforGraph += " YEAR(date_check) = " + selected_year;
                selectdataforGraph += " AND MONTH(date_check) in (" + monthList + ")";

but when I run the query in the database

SELECT SUM(Amount_Net) AS Amount_Net, Month(date_check) AS Selected_Month FROM testingproject.incomeinformation WHERE YEAR(date_check) AND MONTH(date_check) in (1,11);

The result is the total Amount_Net Sum for both months something like this:

Amount_Net/Selected_Month

120/11

I did try to make another table with the Net Amount already calculated in per month but since i need to select multiple months in some cases, the column where it has the month is a varchar the month in part of the query above doesn't work. I could also just redesign the new table with the date column but i feel like I'm so close with the first response. I also know i should use parameters to avoid sql injections but this is more for quick demonstration of the problem.

4

1 回答 1

0

You need to add a group by to the SQL Statement after the where clause. Otherwise, MySQL just aggregates everything into a single row, choosing an arbitrary month:

The syntax for that line is:

group by MONTH(date_Check), year(date_check)

I would also suggest that you include the year in the select.

Your statement would be:

var selectdataforGraph = "SELECT SUM(AMOUNT_NET) AS Amount_Net, MONTH(date_Check) as Month, year(date_check) as Year FROM testingproject.incomeinformation WHERE";
            var monthList = string.Join(",", selectedMonth);
            selectdataforGraph += " YEAR(date_check) = " + selected_year;
            selectdataforGraph += " AND MONTH(date_check) in (" + monthList + ")";
            selectdataforGraph += " group by MONTH(date_Check), year(date_check)";
于 2013-07-19T13:47:20.270 回答