sql - Query parent and child entries of one element in a single row -
I am looking for the best way to display parent and child entries in a line Example:
table an ID | PARENT_ID | VALUE ============================= 1 | | A2 1 | B3 | 2 | C4 | | D5 | 4 | E
So I have to get the following result:
id | Parents Children VALUE ========================================= 2 | 1 | 3 | A5 | 4 | | E
How do you solve it?
Daniel
with self-affiliation It can be easily done by:
SQL & gt; With 2 table (2 select 1 id, tap parent_id, 'Select all from dual 3 union 2, 1,' b 'two to 4 union all select 3, 2,' c 'to dual union 5 select all 4 , Tap, select all from 'D' Dual 6 union 5, 4, repeat with 'E' 7) Select 8 children. Children, Parents, 9 grand_children.id, children.value 10 FROM (Table of SELECT ID, parent_id, VALUE 11 WHERE LEVEL = 2 13 connect by parent_id = First ID starts with 14 parent_id NULL) Child 15 left Join table_a grand_children at 16 children.id = grand_children.parent_id; ID PARENT_ID id VALUE ---------- ---------- ---------- ----- 2 1 3B 5 4E
Comments
Post a Comment