I have table with self-related foreign keys and can not get how I can receive firs child or descendant which meet condition. My_table structure is:
id | parent_id | type |
---|---|---|
1 | null | union |
2 | 1 | group |
3 | 2 | group |
4 | 3 | depart |
5 | 1 | depart |
6 | 5 | unit |
7 | 1 | unit |
I should for id 1 (union) receive all direct child or first descendant, excluding all groups between first descendant and union. So in this example as result I should receive:
id | type |
---|---|
4 | depart |
5 | depart |
7 | unit |
id 4 because it’s connected to union through group with id 3 and group with id 2 and id 5 because it’s connected directly to union.
I’ve tried to write recursive query with condition for recursive part: when parent_id = 1 or parent_type = ‘depart’ but it doesn’t lead to expected result
with recursive cte AS (
select b.id, p.type_id
from my_table b
join my_table p on p.id = b.parent_id
where b.id = 1
union
select c.id, cte.type_id
from my_table c
join cte on cte.id = c.parent_id
where c.parent_id = 1 or cte.type_id = 'group'
)
2
Answers
Here’s my interpretation:
type='group'
, thenid
andparent_id
are considered in the same groupid#1
andid#2
are in the same group, they’re equalsid#2
andid#3
are in the same group, they’re equalsid#1
,id#2
andid#3
are in the same groupIf the above is correct, you want to get all the first descendent of
id#1
‘s group. The way to do that:id
s in the same group withid#1
type not in ('union', 'group')
)Result:
Sounds like you want to start with the row of id
1
, then get its children, and continue recursively on rows of typegroup
. To do that, use