如何使postgresql结果独特

时间:2013-03-01 09:19:44

标签: sql postgresql openstreetmap

这有点难以描述,但我有一个postgresql 9.1表(planet_osm_roads)。 我的问题是

SELECT
    osm_id, name, highway, way, md5(astext(way)) AS md5
FROM planet_osm_roads
WHERE highway IS NOT NULL
AND md5(astext(way)) IN (
    SELECT DISTINCT md5(astext(way))
    FROM planet_osm_roads
    WHERE highway IS NOT NULL
    GROUP BY md5
    HAVING count(osm_id) > 1
)
ORDER BY osm_id

结果是

  osm_id  | name |    highway    |       ...way ...  |               md5                
----------+------+---------------+-------...----...--+----------------------------------
 -1641383 |      | motorway      | 010200...CA96...0 | 04b4336b997e7ea9d99208bd487bbe7d
 -1641383 |      | motorway      | 010200...EC3E...0 | ae945148417ada285130c59277c48a25
 -1641383 |      | motorway      | 010200...7BF6...0 | 5c5a1b8ae40c1b7f24e293a012ad2add
 23133731 |      | motorway_link | 010200...EC3E...0 | ae945148417ada285130c59277c48a25
 31309105 |      | motorway      | 010200...7BF6...0 | 5c5a1b8ae40c1b7f24e293a012ad2add
 49339926 |      | motorway      | 010200...CA96...0 | 04b4336b997e7ea9d99208bd487bbe7d
(6 rows)

我想要一个包含3行(每个md5哈希一行)和其他相应行的任何的结果。 因此,“ae945148417ada285130c59277c48a25”的有效行可能包含osm_id-highway对“-1641383”& “高速公路”或“23133731”& “motorway_link” - 我不介意,并认为两者都是正确的。

如何解决这个问题以及如何调用所需的操作/技术?所以下次我知道如何将其称为搜索内容。

1 个答案:

答案 0 :(得分:2)

select
    md5(astext(way)) as md5,
    min(osm_id) osm_id,
    min(name) name,
    min(highway) highway,
    min(way) way
from planet_osm_roads
where highway is not null
group by 1
having count(osm_id) > 1