Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Comparison on Aggregate function

Tags:

sql

ms-access

When I run the following SQL in ACCESS 2007

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

I get the following error:

Cannot have aggregate function in where clause. How should I be forming this query to get all of the locations which have a count of ApartmentBuildings greater than 3?

like image 992
Joseph U. Avatar asked Feb 24 '23 00:02

Joseph U.


1 Answers

Use having instead of where:

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

for more information see this page

like image 129
Elian Ebbing Avatar answered Mar 07 '23 03:03

Elian Ebbing