In an Adjacency List table, given the id of a node, how can I find it's associated root node?
Note:
The table contains multiple trees so I cannot simply search for the null parentId.
Further Information:
This is what I currently have, any issues or improvements to this?
with tree as
(
select
t.*
from table1 t
where t.id = @id
union all
select
t2.*
from tree
join table1 t2 on tree.parentId = t2.id
)
select *
from tree
where parentId is null