I have dwelling on this for hours and still cannot find out how to do this. How do you get the total sales of an item for every year.
What I have got so far is this:
SQLCommand.CommandText = @"SELECT SUM SalesNo AS TotalYearSales FROM SalesTable WHERE Product = " + ddItems.SelectedItem + "";.
The table headings are as follows Product, Year, SalesNo.
As Daniel pointed out, you would be wide open to sql-injection attacks and should parameterized queries.
To get total by year, you have to add the year as a basis of the query too. I don’t know the “date” column in your table the transactions are based on, so you’ll have to adjust. The “?” in the query is a “place-holder” for the parameter added immediately after.. And parameters should be added in the same order as listed in the query. Some SQL engines allow “named” parameters and use something like “@ProductIWant”, and your parameter would use the same…
Since you are querying for only a specific product, that doesn’t need to be in the list.. unless you wanted ALL products grouped by year.