pgRouting最短路径获得唯一的osm_id值

时间:2013-03-09 14:09:06

标签: sql

我有sql:

SELECT osm_id, vertex_id
   FROM ways
   JOIN
   (SELECT * FROM shortest_path('
      SELECT gid AS id, 
          source::int4 AS source, 
          target::int4 AS target, 
          length::float8 AS cost
      FROM ways',
      7856,
      18774,
      false,
      false)) AS route
   ON
   ways.gid = route.edge_id 
   order by vertex_id   

它产生这样的东西:

osm_id;vertex_id
5024380;6223
5024380;6224
5024380;6225
5024380;6226
175339584;6227
5024380;6230
5024380;6231
5024380;6232
38521651;6233
5024380;6234

你可以看到,osm_id不是唯一的。我按osm_id排序vertex_id。我如何才能获得osm_id排序的唯一vertex_id值。我不需要vertex_id列。仅需osm_id。我尝试了group by条款,但这不起作用。陷入困境的查询:

SELECT distinct osm_id
   FROM ways
   JOIN
   (SELECT * FROM shortest_path('
      SELECT gid AS id, 
          source::int4 AS source, 
          target::int4 AS target, 
          length::float8 AS cost
      FROM ways',
      7856,
      18774,
      false,
      false)) AS route
   ON
   ways.gid = route.edge_id 
   --group by osm_id
   order by vertex_id 

给出错误:

ERROR:  for SELECT DISTINCT, ORDER BY expressions must appear in select list
LINE 21:    order by vertex_id 
                     ^

********** Error **********

ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
SQL state: 42P10
Character: 420

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT osm_id
   FROM ways
   JOIN
   (SELECT * FROM shortest_path('
      SELECT gid AS id, 
          source::int4 AS source, 
          target::int4 AS target, 
          length::float8 AS cost
      FROM ways',
      7856,
      18774,
      false,
      false)) AS route
   ON
   ways.gid = route.edge_id 
   GROUP BY osm_id
   ORDER BY min(vertex_id)