I need to write a spatial query which will return centroids
of clusters which are within a bounding box or polygon.
The query I have now is:
SELECT
ST_NumGeometries(unnest(ST_ClusterWithin(ST_PointFromText(centroid), 10))) AS venueCount,
ST_AsText(ST_Centroid(unnest(ST_ClusterWithin(ST_PointFromText(centroid), 10)))) AS clusterCentroid
FROM venue_locations
WHERE ST_MakeEnvelope (...) ~ ST_Centroid(unnest(ST_ClusterWithin(ST_PointFromText(centroid), 10)));
This, however, gives me the error:
ERROR: aggregate functions are not allowed in WHERE
If I replace WHERE
with HAVING
, I get the following error:
ERROR: argument of HAVING must not return a set
How can I write this to be efficient for large databases?
EDIT: to clear up any confusion, what I am trying to accomplish it to get clustered points from the database which are in a specific extent.