2011-06-01 85 views
4

當我在Access 2007SQL比較

Select Location, COUNT(ApartmentBuildings) AS TotalIBuildingsManaged From Apartments Where COUNT(ApartmentBuildings) > 3 Group By Location Order By COUNT(ApartmentBuildings) DESC;

我碰到下面的錯誤運行下面的SQL:

不能有where子句中聚合函數。我應該如何形成這個查詢來獲得所有的ApartmentBuildings數量大於3的位置?

+0

如果你使用Access查詢生成器,它會爲您編寫適當的SQL,它將使用HAVING子句而不是WHERE子句。 – 2011-06-03 03:07:51

回答

5

使用having而不是where

Select Location, COUNT(ApartmentBuildings) AS TotalIBuildingsManaged 
From Apartments 
Group By Location 
Having COUNT(ApartmentBuildings) > 3 
Order By COUNT(ApartmentBuildings) DESC; 

瞭解更多信息see this page

3

您需要使用HAVING條款

Select Location, COUNT(ApartmentBuildings) AS TotalIBuildingsManaged 
From Apartments 
Group By Location 
HAVING COUNT(ApartmentBuildings) > 3