This is very weird query and I am not sure how to proceed with it. Below is the table.
id descendentId attr_type attr_value
1 {4} type_a
2 {5} type_a
3 {6} type_a
4 {7,8} type_b
5 {9,10} type_b
6 {11,12} type_b
7 {} type_x TRUE
8 {} type_y "ABC"
9 {} type_x FALSE
10 {} type_y "PQR"
11 {} type_x FALSE
12 {} type_y "XYZ"
Input for the query will be 1,2,3.. output should be "ABC".
Logic is - loop through descendantId from 1,2,3 until attr_type x is reached. If attr_type x is reached which is 7,9 and 11 then check which one is true. For e.g. 7 is true, then
get it's sibling of type type_y (check row 4) which is 8 and return it's value.
All this is string format.
descendentId?parentIdinstead (that way, you could even use a self-referencing foreign key).