多亏了@JimJones的建议,我找到了解决办法。我定义了两种观点:一种是以旧的方式找到所有的房子(find_houses_all),另一种是只返回唯一的房子(find_houses_unique)。
-- Find all houses within a radius of ~200m of an event
DROP VIEW IF EXISTS find_houses_all;
CREATE VIEW find_houses_all AS
SELECT bag.openbareruimte, bag.huisnummer, bag.huisletter, bag.huisnummertoevoeging,
event_count_2019_geo.locatie
FROM event_count_2019_geo
INNER JOIN utrecht_2020 AS bag ON (ST_DWithin(bag.geo_lokatie, event_count_2019_geo.geo_lokatie, 0.002));
-- Find all *unique* houses within a radius of ~200m of an event
-- Each house is uniquely identiefied by openbareruimte, huisnummer, huisletter
-- and huisnummertoevoeging, so these are the columns to apply DISTINCT ON
DROP VIEW IF EXISTS find_houses_unique;
CREATE VIEW find_houses_unique AS
SELECT DISTINCT ON(bag.openbareruimte, bag.huisnummer, bag.huisletter, bag.huisnummertoevoeging)
bag.openbareruimte, bag.huisnummer, bag.huisletter, bag.huisnummertoevoeging,
event_count_2019_geo.locatie
FROM event_count_2019_geo
INNER JOIN utrecht_2020 AS bag ON (ST_DWithin(bag.geo_lokatie, event_count_2019_geo.geo_lokatie, 0.002));
SELECT locatie, COUNT (locatie)
FROM find_houses_all -- find_houses_unique
GROUP BY locatie
ORDER BY locatie;
在所有情况下,find_houses_all的输出都大于或等于find_houses_unique的输出。电子表格中的示例输出和减去的结果如下所示:
Locatie All Unique All - Unique
achter st.-ptr. 617 222 395
berlijnplein 87 87 0
boothstraat 653 175 478
breedstraat 1057 564 493
buurkerkhof 914 163 751
catharijnesngl. 134 38 96
domplein 842 149 693
...
Total 35399 13196 22203
负数表示错误。