Search code examples
sqlms-access

tried to execute a query that does not include the specified expression 'CodeProduct' as part of an aggregate function


why does an error appear what is wrong in my code, please guide me

Thanks

Private itrservice As New StocksoutService()
 Public Function GetStockin() As IEnumerable(Of Stocksout)
            Dim sql = "SELECT Stocksout.Invno AS [Invno],StocksoutDetail.CodeProduct AS [CodeProduct] FROM Stocksout INNER JOIN StocksoutDetail ON Stocksout.Invno = StocksoutDetail.Invno GROUP BY Stocksout.Invno"
            Using _conn = New OleDbConnection(DbContext.GetOledbConnectionString())
                Return _conn.Query(Of Stocksout)(sql).ToList()
            End Using
        End Function
            grid.DataSource = itrservice.GetStockin()

Solution

  • The issue is that you are selecting multiple columns (two, in this case) and trying to group by one of them without specifying how to aggregate the others. If you group by Stocksout.Invno then you need to aggregate StocksoutDetail.CodeProduct, which is exactly what the error message is telling you that you haven't done. Aggregate functions are things like COUNT, MAX, SUM, etc.