【问题标题】:SQL Comparison on Aggregate function聚合函数的 SQL 比较
【发布时间】:2011-06-01 16:32:38
【问题描述】:

当我在 ACCESS 2007 中运行以下 SQL 时

选择位置, COUNT(公寓楼) AS TotalIBuildingsManaged From Apartments 其中 COUNT(ApartmentBuildings) > 3 按位置分组 COUNT(公寓楼) DESC;

我收到以下错误:

where 子句中不能有聚合函数。我应该如何形成此查询以获取所有 ApartmentBuildings 数量大于 3 的位置?

【问题讨论】:

  • 如果您使用 Access 查询生成器,它会为您编写适当的 SQL,并且会使用 HAVING 子句而不是 WHERE 子句。

标签: sql ms-access


【解决方案1】:

使用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

【讨论】:

    【解决方案2】:

    您需要使用HAVING 子句

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-19
      • 2021-09-09
      • 2021-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      相关资源
      最近更新 更多