2014-11-02 29 views
0

我需要找到重複的條目(住宿)的名字,這將這樣進行:如何找到由地理過濾名重複協調

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      | 
+0

你能發表住宿和地址的表結構嗎? – Rahul 2014-11-02 18:13:01

+0

@Rahul請找到更新db方案 – dc10 2014-11-02 18:27:04

回答

0

你可以先通過lonlat列從addresses分組表中得到重複accommodation_id

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 
+0

,但您的方法如何確保過濾器僅在相同位置複製? – dc10 2014-11-02 18:26:36

+0

然後它應該是其他方式。順便說一句,你的地址表有城市/州。有了位置,你是指在同一個城市或同一個州內嗎? – Rahul 2014-11-02 18:31:38

+0

應該是城市,但在這種情況下,這並不重要,因爲我知道我有同名的住宿重複,但他們的地址。城市解析爲不同的城市名稱,所以唯一依靠的是lonch coordindate – dc10 2014-11-02 18:37:54

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) 
+0

使用ST_Dwithin可能會比舍入幾何座標更好。 – 2014-11-04 10:27:44