如何按地理坐标筛选的名称查找重复项

时间:2014-11-02 18:04:59

标签: postgresql postgis

我需要按名称找到重复的条目(住宿),这样做:

CREATE TABLE tbl_folded AS
SELECT name
     , array_agg(id) AS ids
FROM   accommodations
GROUP  BY 1;

可以获得具有相同名称的所有住宿ID,不幸的是他们需要进一步过滤。我只需要在一个地方获得相同的住宿。 每个住宿都有一个地址(地址表有地理坐标的外键,accommodation_id和lonlat列)。 为了找到最接近的位置,我会去s.th.像这样

ORDER BY ST_Distance(addresses.lonlat, addresses.lonlat)

那么如何扩展上面的查询以应用此位置过滤? 非常感谢帮助。

   Column    |          Type          |                          Modifiers                          
-------------+------------------------+-------------------------------------------------------------
 id          | integer                | not null default nextval('accommodations_id_seq'::regclass)
 name        | character varying(255) | 
 category    | character varying(255) |



                                        Table "public.addresses"
      Column      |            Type             |                       Modifiers                        
------------------+-----------------------------+--------------------------------------------------------
 id               | integer                     | not null default nextval('addresses_id_seq'::regclass)
 formatted        | character varying(255)      | 
 city             | character varying(255)      | 
 state            | character varying(255)      | 
 country_code     | character varying(255)      | 
 postal           | character varying(255)      | 
 lonlat           | geography(Point,4326)       | 
 accommodation_id | integer                     | 

2 个答案:

答案 0 :(得分:0)

您可以先从accommodation_idaddresses列中获取重复的lonlat {/ 1}}

select accommodation_id
from addresses
group by lonlat
having count(*) > 1

然后将此结果与accommodation表结合使用,以获取names列,如下所示

CREATE TABLE tbl_folded AS
select ac.id,
ac.names
from accommodation ac 
join (
select accommodation_id
from addresses
group by lonlat
having count(*) > 1
) tab on ac.id = tab.accommodation_id

答案 1 :(得分:0)

所以这就是我解决它的方式。我只是过滤半径内的坐标

SELECT
  lower(name)                  AS base_name,
  array_agg(accommodations.id) AS ids

FROM accommodations
  INNER JOIN addresses ON accommodations.id = addresses.accommodation_id

GROUP BY 1, round(ST_X(lonlat::geometry)::numeric,2), round(ST_Y(lonlat::geometry)::numeric,2)