Not contained in aggregate or having. .
Back to this query again. So I thought I was doing this correctly with a subquery. . .
use Northwind
Select * From (
SEL开发者_JS百科ECT FirstName + ' ' + LastName as 'Full Name',
sum(UnitPrice * Quantity) as 'Total Sales',
YEAR(OrderDate) as SalesYear
From Employees e
Join Orders o on o.EmployeeID = e.EmployeeID
join OrderDetails od on od.OrderID = o.OrderID) as subst
Group by 'Full Name', SalesYear
Order by 'Total Sales' desc
The error I get is the "invalid in the select list because it isn't contained in either an aggregate function or the group by clause. I had it without the subquery earlier and it worked fine . . .
The aggregate function (e.g. SUM) and the grouping have to be done at the same "level" of the query:
use Northwind
Select 'Full Name',SalesYear,SUM(Sale) as 'Total Sales' From (
SELECT FirstName + ' ' + LastName as 'Full Name',
UnitPrice * Quantity as Sale,
YEAR(OrderDate) as SalesYear
From Employees e
Join Orders o on o.EmployeeID = e.EmployeeID
join OrderDetails od on od.OrderID = o.OrderID) as subst
Group by 'Full Name', SalesYear
Order by 'Total Sales' desc
精彩评论