2011-07-21 67 views
0

再次回到此查詢。所以我認爲我正在用子查詢正確地做到這一點。 。 。不包含在聚合或擁有。 。

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子句包含的。我是有沒有前面的子查詢和它工作得很好。

回答

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 
+0

掃清事情了感謝。 – surfasb

相關問題