id_start | id_end |
---|---|
1 | 2 |
2 | 3 |
3 | 4 |
I want to collect elements who are "connected" like [1, 2, 3, 4]
. (in an array for example)
I tried a recursive query like:
WITH RECURSIVE q_rec AS (
SELECT id_start,
id_end
FROM my_table
UNION
SELECT t.id_start
t.id_end
FROM my_table t
INNER JOIN q_rec r ON r.id_start = t.id_end
)
SELECT *
FROM q_rec;
But how can I aggregate them in an array despite they are not in the same column ?
2
Answers
Try this, I’m assuming you know the start and end elements:
Another approach gathers all ids in the recursive query, as a table. Then applies aggregation.
Output:
Check the demo here.