PostgreSQL
 sql >> Datenbank >  >> RDS >> PostgreSQL

Holen Sie sich die Pfade aus einer Datenbank mit Punkten in SQL

Verwenden Sie ein rekursives cte mit einem array[array[source, destination]] als Aggregationsspalte:

with recursive cte(source, destination, path) as (
    select source, destination, array[array[source, destination]]
    from points
union all
    select p.source, p.destination, path || array[p.source, p.destination]
    from cte c
    join points p on c.destination = p.source
    where not array[array[p.source, p.destination]] <@ path
)
select distinct on (path[1:1]) path
from (
    select distinct on (source, destination) *
    from cte
    order by source, destination, array_length(path, 1) desc
    ) s    
order by path[1:1], array_length(path, 1) desc;

        path         
---------------------
 {{1,2},{2,3},{3,7}}
 {{5,7}}
 {{9,12}}
(3 rows)