Beginner Beginner - 1 year ago 47
SQL Question

Error with GROUP BY clause

I get the error:

Column 'dbo.Saved_ORDER_IMPORT.Company' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

When i execute :

SELECT [Order No], Company
where [sent] = 1 and datesent between '01/01/2009' and '01/27/2012'
group by [Order No]

do i have to change the column or my query?

Answer Source


You can't group your query by only one column because you select 2.

MSDN - Groups a selected set of rows into a set of summary rows by the values of one or more columns or expressions in SQL Server. One row is returned for each group. Aggregate functions in the SELECT clause list provide information about each group instead of individual rows.

We need more information but maybe this helps


SELECT [Order No], Company
FROM [dbo].[Saved_ORDER_IMPORT] 
WHERE [sent] = 1 AND datesent BETWEEN '01/01/2009' AND '01/27/2012'
GROUP BY [Order No], Company

More information: