Skip to content
Advertisement

Follow the connections : recursive query

i have two columns of numbers. the goal is starting from a number f.e. 55, to extract all ‘connected’ numbers (starting with any of those numbers, should yield the same result)

in this case all numbers shown here : 55,56,35,69,60,22,47,2,26

I use the following query :

but I get back only those back : 55,56,35,69,60,22,47

I think using this will work :

but Postgres does not allow to use “merge” more than ONCE in the recursive query !!?

My goal is starting with number to find all connected numbers across the “chain” i.e.

because :

Advertisement

Answer

You can do:

Result:

See running example at DB Fiddle.

Or… if you prefer to place the parameter in a single place:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement