Oracle , select relation - sql

I have 2 tables in oracle DB: Items and Relationship.
items:
ID
---
1
2
3
4
5
6
7
relationship:
ID parent child
--------------------
1 1 2
2 1 3
3 1 4
4 2 5
5 2 6
6 3 7
In the relationship table, I'm storing the hierarchial structure of the "items" (do not ask why it's stored in different tables).
The question:
When I execute this query:
SELECT PARENT_ID, CHILD_ID, CONNECT_BY_ISLEAF, MAX(LEVEL) OVER () + 1 - LEVEL as rev_level
FROM relationship
CONNECT BY PRIOR PARENT_ID = CHILD_ID
START WITH CHILD_ID = 7;
I do not see the root parent because he doesn't exist in this table as a child.
The question is how can I add the root parent(ID = 1) to the query result or join it whith the "items" table and keep the result columns (level and isleaf).

CONNECT_BY_ISLEAF works the other way around when using bottom up search (see this link http://technology.amis.nl/2009/11/14/oracle-11gr2-alternative-for-connect_by_isleaf-function-for-recursive-subquery-factoring-dedicated-to-anton/)
I assume that you want to show more data about the item (like name) If that is the case just left join the items table.
SELECT PARENT_ID AS PARENT_ID,CHILD_ID, i.name AS CHILD_NAME,
CONNECT_BY_ISLEAF,
MAX(LEVEL) OVER () + 1 - LEVEL AS rev_level
FROM items i
LEFT JOIN relationship r ON (i.id = r.CHILD_ID)
CONNECT BY PRIOR PARENT_ID = CHILD_ID
START WITH CHILD_ID = 7
ORDER BY REV_LEVEL;
check this SQLfiddle: http://sqlfiddle.com/#!4/5c9fa/17
In addition check this post about bottom up searches (http://bitbach.wordpress.com/2010/10/18/implementing-bottom-up-path-traversal-for-hierarchical-tables/)

Notice that you have both directions - parent and child.
pick one and dont mix the two.
1 with x as (
2 select 1 as id, 1 as parent, 2 as child from dual union all
3 select 2, 1 , 3 from dual union all
4 select 3 ,1, 4 from dual union all
5 select 4 ,2, 5 from dual union all
6 select 5 ,2, 6 from dual union all
7 select 6 ,3, 7 from dual)
8 select *
9 from x
10 sTART WITH child = 7
11* CONNECT BY PRIOR id= CHILD
SQL> /
ID PARENT CHILD
---------- ---------- ----------
6 3 7
5 2 6
4 2 5
3 1 4
2 1 3
1 1 2
connection is made by prior id = child and not prior parent = child

Related

DB2 : search depth first : (syntax) error

A few days ago I installed DB2 LUW (11.5) on a server to play around with.
Now I would like to do some recursive SQL (Recursive Common Table Expression):
Let me show how I setup :
drop table relations;
create table relations (id int, parent int);
insert into relations values(0,NULL);
insert into relations values(1,0);
insert into relations values(2,1);
insert into relations values(3,1);
insert into relations values(4,3);
insert into relations values(5,0);
insert into relations values(6,5);
insert into relations values(7,5);
insert into relations values(8,6);
insert into relations values(9,7);
insert into relations values(10,0);
insert into relations values(11,1);
commit;
Now I would like to see the hierarchy in the table. So I tried the following:
with recur(id, parent, level) as
(
select rel.id id, rel.parent parent, 0 level from relations rel where rel.id=0
union all
select rel.id, rel.parent, rec.level+1 from recur rec, relations rel where rec.id=rel.parent
and rec.level<10
)
select id, lpad(parent, level*2, ' ') from recur;
This gives me:
ID PARENT
----------- ------------------
0 -
1 0
5 0
10 0
2 1
3 1
11 1
6 5
7 5
4 3
8 6
9 7
This is (to me) : "Search Breadth First"
What I would like to see is "Search Depth First"
So I did this:
with recur(id, parent, level) as
(
select rel.id id, rel.parent parent, 0 level from relations rel where rel.id=0
union all
select rel.id, rel.parent, rec.level+1 from recur rec, relations rel where rec.id=rel.parent
and rec.level<10
)
search depth first by parent set ord
select id, lpad(parent, level*2, ' ') parent from recur order by ord;
But this delivers to me:
SQL0104N An unexpected token "search depth first by parent set ord sel" was
found following "t and rec.level<10 )". Expected tokens may include:
"<values>". SQLSTATE=42601
No clue how to solve it now. I (think I) have tried a lot of possible solutions. But none worked.
I'm starting to believe that DB2 LUW (11.5) doesn't know about Search Depth First. Or some setting must be made to make DB2 aware of the "SDF" possibility.
My question to you all:
How to solve this problem? How do I get Search Depth First to work?
On the positive....following works like a charms....but that is not whatI want tot know :-)
select id, lpad(parent, level*2, ' ') parent, level
from relations
start with id=0
connect by prior id=parent;
ID PARENT LEVEL
----------- ---------- -----------
0 - 1
1 0 2
2 1 3
3 1 3
4 3 4
11 1 3
5 0 2
6 5 3
8 6 4
7 5 3
9 7 4
10 0 2
This works like a charm, but I had tot make a switch in the database (and a restart):
db2set DB2_COMPATIBILITY_VECTOR=08
Your question is about displaying rows in a specific ordering, not about searching in a specific ordering.
You can display the rows in the ordering you want by assembling an ordering column that fits your needs.
For example:
with
n (id, parent, lvl, ordering) as (
select id, parent, 1, lpad(id, 3, '0') || lpad('', 30, ' ')
from relations
where parent is null
union all
select r.id, r.parent, n.lvl + 1, trim(n.ordering) || '/' || lpad(r.id, 3, '0')
from n, relations r where r.parent = n.id
)
select id, lpad(parent, lvl * 2, ' ') as parent, lvl
from n
order by ordering;
Result:
ID PARENT LVL
--- --------- ---
0 1
1 0 2
2 1 3
3 1 3
4 3 4
11 1 3
5 0 2
6 5 3
8 6 4
7 5 3
9 7 4
10 0 2
See running example at db<>fiddle.

oracle sql parent child with for multiple columns

I have a users table having two columns for Approval Hierarchy , Table structure is like below
User_ID Submit_to Approve_to
1 2 3
2 4 5
3 6 2
4 2 3
5 1 0
Data is just For example 0 Mention no Approver :
Submit to and Approve to both will be Approvers
I need a Query which can give the Approves details in sequence that who will be next approver for entry user have created .
Using CONNECT BY, you can connect the parent/child relationship within a table. The query below will show the approval path for each user.
Query
WITH
d (user_id, submit_to, approve_to)
AS
(SELECT 1, 2, 3 FROM DUAL
UNION ALL
SELECT 2, 4, 5 FROM DUAL
UNION ALL
SELECT 3, 6, 2 FROM DUAL
UNION ALL
SELECT 4, 2, 3 FROM DUAL
UNION ALL
SELECT 5, 1, 0 FROM DUAL)
SELECT d.user_id, LTRIM (SYS_CONNECT_BY_PATH (user_id, '<'), '<') AS approval_path
FROM d
START WITH approve_to = 0
CONNECT BY NOCYCLE PRIOR user_id = approve_to
ORDER BY user_id;
Result
In the APPROVAL_PATH column, the left most number is the USER_ID giving final approval and the right most number is the USER_ID initially submitting whatever needs to be approved.
USER_ID | APPROVAL_PATH
-----------------------
1 | 5<2<3<1
2 | 5<2
3 | 5<2<3
4 | 5<2<3<4
5 | 5

Get all of child nodes in a hierarchical data in SQL

I have the following sql table:
id parent_id
1 null
2 1
3 4
4 8
5 1
6 2
7 6
8 null
How can i get all of child nodes of some specific node?
For example with id = 1:
1 2 5 6 7
with id = 8
8 4 2
I've figured out in another blog, hope it will help other people:
with RECURSIVE cte(id,parent_id) as (
select
id,
parent_id
from forum
where id = 2
UNION ALL
select
forum.id,
forum.parent_id
from forum
inner join cte on forum.parent_id = cte.id
)
select * from cte

How to define the number of levels in a hierarchy using sql?

My situation is the following. I have a table containing a product hierarchy. The following table is an extract of the dataset:
child parent
1 2
2 3
4 5
6 7
I want to add a column containing the depth (of the child) of the hierarchy. Something as follows:
child parent depth
1 2 2
2 3 1
4 5 1
6 7 1
How would I do that in oracle? Thank you!
Something like:
SELECT child, parent, level
FROM your_table
START WITH parent NOT IN ( SELECT child FROM your_table )
CONNECT BY parent = PRIOR child;
Outputs:
CHILD PARENT LEVEL
----- ------ -----
1 2 2
2 3 1
4 5 1
6 7 1

SQL: Hierarchical query with multiple roots / parents

I have a table describing elements organized in a tree-like structure:
ID, PARENT_ID, NAME
0 null TOP
1 0 A
2 0 B
3 0 C
4 1 AA
5 2 BA
6 3 CA
7 6 CAA
...
There can be many levels in this hierarchy.
Suppose there is a list of elements (say IDs 2 and 3) for which I would like to get all child records from the table.
Something like this:
select *
from MY_TABLE
start with PARENT_ID in (2,3)
connect by PARENT_ID = prior ID
will return:
ID, PARENT_ID, NAME
5 2 BA
6 3 CA
7 6 CAA
However, I want the each output record to be mapped to the original parent from my list (2,3) so that the output would look like this:
ORIGINAL_PARENT_ID, ID, PARENT_ID, NAME
2 5 2 BA
3 6 3 CA
3 7 6 CAA
How can it be done?
connect_by_root may be what you're after?
SQL> select t.*, connect_by_root parent_id as ORIGINAL_PARENT_ID
2 from MY_TABLE t
3 start with PARENT_ID in (2,3)
4 connect by PARENT_ID = prior ID
5 /
ID PARENT_ID NAM ORIGINAL_PARENT_ID
---------- ---------- --- ------------------
5 2 BA 2
6 3 CA 3
7 6 CAA 3
Assuming your names are really as you have them, then the problem can be done without connect by. You can use simple string manipulation.
with ToFind (
select 'C' as parent from dual union all
select 'B' as parent from dual
)
select t.*
from t join
ToFind tf
on t.name like tf.parent, 100)||'%' and t.name <> tf.parent