按几何分组可以吗?
我们根据多边形的几何形状进行很多计数,这涉及首先将数据相交以对学生实例进行计数。根据学校边界,街区组等:
SELECT
n.nbhd_id
, count(*) AS count_burglaries
FROM denver.crime AS c
JOIN denver.neighborhoods AS n
ON ST_Intersects(c.geom, n.geom)
WHERE c.offense_ty LIKE 'BURG%'
GROUP BY n.nbhd_id
第二步是包装一个子查询,以便将多边形表中的几何图形重新连接到计数查询中:
SELECT
count.*
, n.nbhd_name
, n.geom
FROM (
SELECT
n.nbhd_id
, count(*) AS count_burglaries
FROM denver.crime AS c
JOIN denver.neighborhoods AS n
ON ST_Intersects(c.geom, n.geom)
WHERE c.offense_ty LIKE 'BURG%'
GROUP BY n.nbhd_id
) AS count
JOIN denver.neighborhoods AS n
ON count.nbhd_id = n.nbhd_id
但似乎您也可以在GROUP BY中使用几何:
SELECT
n.nbhd_id
, n.nbhd_name
, n.geom
, count(*) AS count_burglaries
FROM denver.crime AS c
JOIN denver.neighborhoods AS n
ON ST_Intersects(c.geom, n.geom)
WHERE c.offense_ty LIKE 'BURG%'
GROUP BY n.nbhd_id, nbhd_name, n.geom
在GROUP BY中包括几何体是否正确?
它使我不必将count查询包装为要连接到nbhd_id的子查询-如果它增加了按几何进行分组的运行时间,那么这就是不这样做的可能原因,您不是说吗?
—
DPSSpatial
GROUP BY
收获?它不是可以通过唯一键来加入的nbhd_id
吗?您正在扩大排序范围,从而增加了运行时间,可能比后续的连接更多。