0

I need to construct the query or function to solve my data structure problem.

My data struture only keep the parent node not the child node. Here the sample.

                        ┌───────┐            ┌───────┐
Level-1                 │   A   │            │   B   │          ────────► node is Unlimited
                        └───┬───┘            └───┬───┘
                            │                    │
                      ┌─────┴─────┐         ┌────┴──────┐
                      │           │         │           │
                      │           │         │           │
                ┌─────┴─┐     ┌───┴───┐ ┌───┴───┐    ┌──┴────┐
Level-2         │  C1   │     │  EE   │ │  EF   │    │  G1   │  ────────► node is Unlimited
                └───┬───┘     └───────┘ └───┬───┘    └───┬───┘
                    │                       │            │
         ┌──────────┴────────┐              │         ┌──┴─────────┐
Level-3  │                   │              │         │            │
      ┌──┴────┐          ┌───┴───┐      ┌───┴───┐   ┌─┴─────┐  ┌───┴───┐
      │  HH   │          │   IU  │      │  JK   │   │  JA   │  │  JJ   │ ──► node is Unlimited
      └───────┘          └───────┘      └───────┘   └───────┘  └───────┘
         │                                              │     
Level-4  │                                              │     
      ┌──┴────┐                                     ┌───┴───┐
      │  OT   │                  │                  │  PT   │ ──► node is Unlimited
      └───────┘                  │                  └───────┘
                                 ▼
                    
                     Deep of level is unlimited
    

Table Node

+------+------+-------------+
| Rec# | Node | Parent_Node |
+------+------+-------------+
|  1   |  A   |             |
|  2   |  B   |             |
|  3   |  C1  |     A       |
|  4   |  EE  |     A       |
|  5   |  EF  |     B       |
|  6   |  G1  |     B       |
|  7   |  HH  |     C1      |
|  8   |  IU  |     C1      |
|  9   |  JK  |     EF      |
|  10  |  JA  |     G1      |
|  11  |  JJ  |     G1      |
|  12  |  OT  |     HH      |
|  13  |  PT  |     JA      |
+------+------+-------------+

The question is how to build query or function for retrieving data from this structure ?,.

  1. For getting all data below the level.
    Example :
    from node A (level-1) => { the result should be: C1,EE,HH,IU,OT }
    from node C1 (level-2) => { the result should be: HH,IU,OT }

  2. For getting just 1 level down, or several down of level.
    Example :
    Get 1 Level down, from node B (level-1) => { the result should be: EF and G1 Only}
    Get 2 level down, from node G1 (level-2) => { the result should be: JA, JJ and PT }

    Thank You.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Altimuksin
  • 67
  • 5

1 Answers1

1

Here is an recursive CTE for your query. I haven't finished the formatting but it gives you the recursive CTE.
You haven't specified your RBMS, this is in SQL server. See dbFiddle link at the bottom.

create table nodes(
Rec int,
Node varchar(5),
Parent_Node varchar(5));
insert into nodes values
(1,'A',null),
(2,'B',null),
(3,'C1','A'),
(4,'EE','A'),
(5,'EF','B'),
(6,'G1','B'),
(7,'HH','C1'),
(8,'IU','C1'),
(9,'JK','EF'),
(10,'JA','G1'),
(11,'JJ','G1'),
(12,'OT','HH'),
(13,'PT','JA');
with noder as 
( 
select  
  Parent_Node n1, 
  Node n2, 
  cast(null as varchar(5)) n3, 
  cast(null as varchar(5)) n4, 
  cast(null as varchar(5)) n5
from nodes
  union all 
select 
  Parent_node,n1,n2,n3,n4
from nodes
join noder on n1 = Node 
)
select *
from Noder 
where n1 is not null
n1 | n2 | n3   | n4   | n5  
:- | :- | :--- | :--- | :---
A  | C1 | null | null | null
A  | EE | null | null | null
B  | EF | null | null | null
B  | G1 | null | null | null
C1 | HH | null | null | null
C1 | IU | null | null | null
EF | JK | null | null | null
G1 | JA | null | null | null
G1 | JJ | null | null | null
HH | OT | null | null | null
JA | PT | null | null | null
G1 | JA | PT   | null | null
B  | G1 | JA   | PT   | null
C1 | HH | OT   | null | null
A  | C1 | HH   | OT   | null
B  | G1 | JJ   | null | null
B  | G1 | JA   | null | null
B  | EF | JK   | null | null
A  | C1 | IU   | null | null
A  | C1 | HH   | null | null

db<>fiddle here

  • Hi Kendle, thank you,. but we don't know how many n.. , how we can find the number of n ?. because the n is unlimited in this structure. – Altimuksin Mar 14 '22 at 14:37
  • oh.. I am using SQL Server... – Altimuksin Mar 14 '22 at 14:37
  • Here there are not entries in n5 so we know that we have gone far enough. There must be a dynamic method but it's easy to add columns until we get an empty one. NB You must have the same number of columns in the 2 queries `union all` –  Mar 14 '22 at 14:40