user3522506
user3522506

Reputation: 55

Explanation with this error?

I have this error: You tried to execute a query that does not include the specified expression 'Sales' as part of an aggregate function.

And my query is:

MyCommand.CommandText = "SELECT Sales, Created_Date FROM tblSales" & _
  "where Year(Created_Date)='" & year & "' and Month(Created_Date)='" & month & "' & _
   "GROUP BY Created_Date"

What does this mean and how to solve it?

Thanks in advance!

Upvotes: 0

Views: 42

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460228

You either have to add it to the group by or you need to specifiy how you want to agregate Sales. Consider that it's possible that you get multiple rows per date. Then the database doesn't know which one you want. So you have to aggregate these rows(f.e. COUNT,SUM,MIN,MAX,..).

You could for example take the Max-Sales for each date:

SELECT Max(Sales)AS MaxSales, Created_Date ...

It works also if you add it to the Group By but that could yield a different result since you could get multiple records per date.

SELECT Sales, Created_Date ...
... GROUP BY Sales, Created_Date

Upvotes: 3

Related Questions