Answer the question
In order to leave comments, you need to log in
How to collect all interchanges in SQL?
No ideas come to mind yet.
How can I use a SQL query (used by Postgresql) to collect interchanges by group?
Let's say there is a table Foo with the structure:
main | cross
x | a
x | b
b | x
b | h
z | t
....
Answer the question
In order to leave comments, you need to log in
with recursive table_recursive as (
with "table" as (
select *
from (
values('x','a'),
('a','b'),
('b','c'),
('c','d'),
('n','k'),
('k','l')
) as _(main,"cross")
)
select *, t.main || ' -> ' || t."cross" as "path"
from "table" as t
union
select t_r.main, t."cross", t_r."path" || ' -> ' || t."cross" as "path"
from table_recursive t_r
join "table" t on t.main = t_r."cross"
)
select t_r.*
from table_recursive t_r
where
t_r."main" not in (select "cross" from table_recursive)
and t_r."cross" not in (select "main" from table_recursive)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question