Get the paths from a database of points in sql

后端 未结 2 402
忘了有多久
忘了有多久 2021-01-16 04:30

Consider that I have a table of points, each point has 2 coordinates. For example:

Source       |       Destination 
1            |       2
2            |            


        
2条回答
  •  生来不讨喜
    2021-01-16 05:21

    with recursive
      points(src, dst) as (values(1,2),(3,7),(2,3),(5,7),(9,12)),
      result as (
        select src, dst, array[src,dst] as point, 1 as n
        from points p1
        where not exists(select * from points p2 where p2.dst = p1.src)
        union all
        select result.src, points.dst, array[points.src, points.dst], n+1
        from result join points on (result.dst = points.src)
      )
    select array_agg(point order by n) from result group by src;
    

提交回复
热议问题