Merge two nested sets into rows

61 Views Asked by At

I'm given a nested set database which has the following reduced structure:

Bus its root table (B)

CL CR SL SR
1 6 1 12

Compound table which represents levels of hierarchy with each level having different attributes (C) Level 1 (C1)

CL CR SL SR C5 C6
1 6 1 12 NULL test

Level 2 (C2a)

CL CR SL SR C5 C6
2 3 1 12 foo NULL

and (C2b)

CL CR SL SR C5 C6
4 5 1 12 bar NULL

Lastly S containing signals where S1 is it’s id.

S1 S2
2 0xFA
.. ..
11 0x10

Then comes the tricky part: I need to create a list of the 10 S table items with the complete path and hierarchical values up the tree.

So it would result in a list with values that look something like this

S C .. C R
S2-values C2a-values .. C1-values R
.. .. .. .. ..
S11-values C2a-values .. C1-values R
S2-values C2b-values .. C1-values R
.. .. .. .. ..
S11-values C2b-values .. C1-values R

Any suggestion to loosen the knot in my brain is helpful.

What I tried so far is the following, which does not seem to work as it just crashes the machine:

select
    *
from
    signal s,
    compound c 
join (
    select
        bus.short_name,
        cBase.dtype,
        cBase.value,
        cPdu.relative_bit_position,
        cPdu.bit_length,
        cPdu.left_signal,
        cPdu.right_signal,
        cSwp.switch_code
    from
        compound as cbase
    left join compound as cPdu on
        cPdu.dtype = 'P'
        and cBase.left_compound < cPdu.left_compound
        and cBase.right_compound > cPdu.right_compound
    left join compound as cswp on
        cSwp.dtype = 'SWP'
        and cBase.left_compound < cSwp.left_compound
        and cBase.right_compound > cSwp.right_compound
    left join bus as bus on
        bus.left_compound < cBase.left_compound
        and bus.right_compound > cBase.right_compound
    where
        cBase.dType = 'CF') as res on s.id between res.left_signal and res.right_signal

In the above code I restricted the depth of the categories to 3 (cBase, cPdu, cSwp). What I need is a generalized approach that can handle all depths.

0

There are 0 best solutions below