Thanks for update...
LegId is not in sequence always .
Our logic is need to get the sequence number using ANode -> Z Node traversing.
From Master table select the ID,Start Node & Logic in source table is
1. Select row using ID ( Value#S1) & Anode ( Value#N1)
For above selection need to return Sequence as "1"
i.e. ID LegID Sequence
S1 MSB1 1
2. For Second sequence Legid , Business Logic: Znode of previous sequence will be Anode in current sequence. In Below Case MSB3 will come as Second Sequence.
S1 MSB3 2
S1 MSB5 3
S1 MSB2 4
MasterTable: ( This Master table having details of start & End details )
ID, StartNode, EndNode
Below is the example ,
And final out put will be
ID,LegID,Anode,Znode , Sequence