再次返回此查询。所以我以为我用子查询正确地做了这个。 。
use Northwind
Select * From (
SELECT 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
我得到的错误是“选择列表中的无效,因为它不包含在聚合函数或group by子句中。我之前没有子查询,但它工作正常......。
答案 0 :(得分:2)
聚合函数(例如SUM)和分组必须在查询的相同“级别”完成:
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