我有一个复杂的问题。我正在尝试创建一个表,该表列出表1中的所有人(“人”),其中表2中至少有3个人(“ nearby_people”)居住在<.25英里之外,并且
另一张桌子上的“人”,其附近至少有3个人居住在.25至.50之间。这两个组必须互斥,并且不能重叠。
首先,我运行查询以找出至少有3个“ nearby_people”居住在.50英里之外的“人”的总数。在查询中,我使用米而非英里。 804 m = .50英里
select count(*) from (
select person,
(select count(*) from table2
where ST_Distance(table1.geom::geography, table2.geom::geography)
< 804) as nearby_people
from table1) alias
where nearby_people > 3
DESC)
total_count;
我有COUNT = 5793
然后我对<.25进行查询,得到COUNT =738。
select count(*) from (
select column1, column2,
(select count(*) from table2
where ST_Distance(table1.geom::geography, table2.geom::geography)
< 402) as people_nearby
from table1) alias
where nearby_people > 3
DESC)
total_count;
因此,当我查询附近的人居住在.25至.50英里之间的人时,我希望得到5055 但是我得到了COUNT = 3719
select count(*) from (
select column1, column2,
(select count(*) from table2
where ST_Distance(table1.geom::geography, table2.geom::geography)
between 402 and 804) as people_nearby
from table1) alias
where nearby_people > 3
DESC)
total_count;
数字不累加。
我还尝试运行查询<.50英里远的near_people的数量,并使用EXCEPT提取了<.25英里内出现的结果
select count(*) from (
(select column1, column2,
(select count(*) from table2
where ST_Distance(table1.geom::geography, table2.geom::geography)
< 804) as people_nearby
from table1) alias
where nearby_people > 3
)
EXCEPT
(select column1, column2,
(select count(*) from table2
where ST_Distance(table1.geom::geography, table2.geom::geography)
< 402) as people_nearby
from table1) alias
where nearby_people > 3
)
)total_count;
但我得到的原始计数为5793
我撤消了查询并运行
<.25 EXCEPT <.50
获得COUNT = 738
我的问题是。如何选择两个互斥的组?如果有5793个“人”与居住在<.50英里之外的至少3个人相邻,那么我该如何将“人”与居住在<.25英里以外的near_people隔离?
来自
在.25至.50英里之间有近邻人的“人”?
我希望会重复,因为我设置的半径可能重叠,所以我认为EXCEPT可以解决该问题。但是结果是我的总数很短。
有人能帮助我理解我的逻辑吗?