聚合函数的SQL比较

时间:2011-06-01 16:32:39

标签: sql ms-access

在ACCESS 2007中运行以下SQL时

  

选择位置,   COUNT(ApartmentBuildings)AS   TotalBuildingsManaged From Apartments   COUNT(ApartmentBuildings)> 3   按地点分组按顺序排列   COUNT(ApartmentBuildings)DESC;

我收到以下错误:

在where子句中不能有聚合函数。如何构建此查询以获取所有具有大于3的ApartmentBuildings数量的位置?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:3)

您需要使用HAVING子句

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