2011-06-01 83 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