The recursive subquery factoring vs hierarchical query
Experts,Here it is two queries, I'm executing using hierarchical of recursive subquery and new classical approach. Problem came out was different for the recursive subquery factoring, as this is not displayed parent-child approach, while forwards connect shows parent-child mode. Query 1, I use hierarchical as show good output parent-child, is approaching the 2 query displays all of the nodes of level 1 and then level 2 nodes. I want the query 2 output to be the same as query 1. change query 2 as required.
Note: the output of the two queries post as in sqlplus and toad. Please copy and use in your command prompt.
QUERY 1:
with the hand in the form (select 1 id, name 'John', null, mgrid Union double all the)
Select 2 id, the name of 'michael', null, mgrid Union double all the
Select 3 id, the name of "peter", null, mgrid Union double all the
Select 4 id, the name of "henry", 1 mgrid Union double all the
Select 5 id, "nickname", mgrid 2 Union double all the
Select 6 id, "pao" name, mgrid 3 of all the double union
Select 7 id, the name of 'kumar', mgrid 3 of all the double union
Select 8 id, the name 'parker', mgrid 3 of all the double union
Select 9 id, the name of "mike", 5 double mgrid),
Select lpad (' ', 2 *(level-1)). name, key start with mgrid level is null connect by prior id = mgrid.
OUTPUT:
LEVEL NAME
------------------------------ ----------
John 1
Henry 2
Michael 1
Nick 2
Mike 3
Stone 1
PAO 2
Kumar 2
Parker 2
9 selected lines.
QUERY 2:
with the hand in the form (select 1 id, name 'John', null, mgrid Union double all the)
Select 2 id, the name of 'michael', null, mgrid Union double all the
Select 3 id, the name of "peter", null, mgrid Union double all the
Select 4 id, the name of "henry", 1 mgrid Union double all the
Select 5 id, "nickname", mgrid 2 Union double all the
Select 6 id, "pao" name, mgrid 3 of all the double union
Select 7 id, the name of 'kumar', mgrid 3 of all the double union
Select 8 id, the name 'parker', mgrid 3 of all the double union
Select 9 id, the name of "mike", 5 double mgrid),
/ * Select lpad (' ', 2 *(level-1)). name, key start with mgrid level is null connect by prior id = mgrid. * /
secmain (id, name, mgrid, hierlevel) as (select id, name, mgrid, 1 hierlevel of the main where mgrid is null
Union of all the
Select m.id, $m.name, m.mgrid, sm.hierlevel + 1 in m main join secmain sm on(m.mgrid=sm.id))
cycle is_cycle set id 1 default 0
Select lpad (' ', 2 *(hierlevel-1)). name, secmain hierlevel.
OUTPUT:
NAME HIERLEVEL
------------------------------ ----------
John 1
Michael 1
Stone 1
Henry 2
Nick 2
Parker 2
Kumar 2
PAO 2
Mike 3
9 selected lines.
For example
SQL> with main as(select 1 id,'john' name,null mgrid from dual union all
2 select 2 id,'michael' name,null mgrid from dual union all
3 select 3 id,'peter' name,null mgrid from dual union all
4 select 4 id,'henry' name,1 mgrid from dual union all
5 select 5 id,'nick' name,2 mgrid from dual union all
6 select 6 id,'pao' name,3 mgrid from dual union all
7 select 7 id,'kumar' name,3 mgrid from dual union all
8 select 8 id,'parker' name,3 mgrid from dual union all
9 select 9 id,'mike' name,5 mgrid from dual),
10 secmain (id,name,mgrid,hierlevel) as
11 (select id,name,mgrid,1 hierlevel from main
12 where mgrid is null
13 union all
14 select m.id,m.name,m.mgrid,sm.hierlevel+1 from main m join secmain sm on(m.mgrid=sm.id))
15 search depth first by name set seq
16 cycle id set is_cycle to 1 default 0
17 select lpad(' ',2*(hierlevel-1))||name name,hierlevel from secmain order by seq;
NAME HIERLEVEL
---------- ----------
john 1
henry 2
michael 1
nick 2
mike 3
peter 1
kumar 2
pao 2
parker 2
9 rows selected.
SQL>
Published by: Dom Brooks on November 23, 2011 13:52
Edited for the scope of the search and detection of cycle
Tags: Database
Similar Questions
-
ORA-30654: 'missing DEFAULT keyword' in the recursive subquery factoring
The subquery recursive factoring works without clause CYCLE and cycle only.
When I have a cycle (uncomment SELECT 5, 2 double UNON ALL) ORACLE detects the cycle properly, but when I include the clause CYCLE I get ORA-30654.
I'm pretty shure that this statement already worked in my DB (11.2.0.3.0 - 64 bit)
CREATE TABLE temp_rsq (a,b) AS SELECT 1, 2 FROM dual UNION ALL SELECT 2, 3 FROM dual UNION ALL SELECT 3, 4 FROM dual UNION ALL SELECT 4, 5 FROM dual UNION ALL --SELECT 5, 2 FROM dual UNION ALL SELECT 5, 6 FROM dual UNION ALL SELECT 6, 7 FROM dual; -- -- -- b-> new A WITH cte ( pb, a, b, weg) AS (SELECT NULL, a, b, a FROM temp_rsq WHERE a=1 UNION ALL SELECT cte.b, n.a a, n.b, weg+n.a FROM temp_rsq n JOIN cte ON (cte.b=n.a)) SEARCH depth FIRST BY a SET abst --CYCLE a SET is_cycle to '1' DEFAULT '0' SELECT * FROM cte;
Found last al:
It does not with cusor_sharing = FORCE, only with the cursor sharing = TRUE!
For me, a very strange behavior.
-
Identify cycles recursive subquery factoring (RSF/CTE)
It is possible to detect cycles AFTER executing a recursive factoring subquery with the cycle_clause.
I need to detect already in the query, similar to the virtual CONNECT_BY_ISCYCLE in a hierarchical query.
Here is a test case of the use of factoring of the recursive subquery (RSF) to the algorithm of Dijkstra shortest Implement path?.
DROP TABLE edges;
CREATE TABLE edges (char (1) src, dst char (1), distance, NUMBER (3, 0));
DROP TABLE nodes.
CREATE TABLE nodes (nodes CHAR (1));
-INSERTION in the edges
-normal direction
INSERT INTO edges (SRC, DST, DISTANCE) VALUES ('A', '' B, 2');
INSERT INTO edges (SRC, DST, DISTANCE) VALUES ('A', 'C', '4');
INSERT INTO edges (SRC, DST, DISTANCE) VALUES ('A' 'd', '3' ");
INSERT INTO edges (SRC, DST, DISTANCE) VALUES (' B', 'E', 7' ");
INSERT INTO edges (SRC, DST, DISTANCE) VALUES ("C", "E", "3");
INSERT INTO edges (SRC, DST, DISTANCE) VALUES (', 'E', '4');
INSERT INTO edges (SRC, DST, DISTANCE) VALUES (' B', 'F', 4' ");
INSERT INTO edges (SRC, DST, DISTANCE) VALUES ('C', 'F', "2");
INSERT INTO edges (SRC, DST, DISTANCE) VALUES (', 'F', '1');
INSERT INTO edges (SRC, DST, DISTANCE) VALUES (' B', 'G', 6' ");
INSERT INTO edges (SRC, DST, DISTANCE) VALUES ('C', 'G', "4");
INSERT INTO edges (SRC, DST, DISTANCE) VALUES (', 'G', '5');
INSERT INTO edges (SRC, DST, DISTANCE) VALUES ('E', 'H', '1');
INSERT INTO edges (SRC, DST, DISTANCE) VALUES ('F', 'H', '6');
INSERT INTO edges (SRC, DST, DISTANCE) VALUES ('G', 'H', '3');
INSERT INTO edges (SRC, DST, DISTANCE) VALUES ('E', 'I', '4');
INSERT INTO edges (SRC, DST, DISTANCE) VALUES ('F', 'I', '3');
INSERT INTO edges (SRC, DST, DISTANCE) VALUES ('G', 'I', '3');
INSERT INTO edges (SRC, DST, DISTANCE) VALUES ('H', 'J', '3');
INSERT INTO edges (SRC, DST, DISTANCE) VALUES ('I', 'J', '4');
-inversion
INSERT INTO edges (DST, SRC, DISTANCE) VALUES ('A', '' B, 2');
INSERT INTO edges (DST, SRC, DISTANCE) VALUES ('A', 'C', '4');
INSERT INTO edges (DST, SRC, DISTANCE) VALUES ('A' 'd', '3' ");
INSERT INTO edges (DST, SRC, DISTANCE) VALUES (' B', 'E', 7' ");
INSERT INTO edges (DST, SRC, DISTANCE) VALUES ("C", "E", "3");
INSERT INTO edges (DST, SRC, DISTANCE) VALUES (', 'E', '4');
INSERT INTO edges (DST, SRC, DISTANCE) VALUES (' B', 'F', 4' ");
INSERT INTO edges (DST, SRC, DISTANCE) VALUES ('C', 'F', "2");
INSERT INTO edges (DST, SRC, DISTANCE) VALUES (', 'F', '1');
INSERT INTO edges (DST, SRC, DISTANCE) VALUES (' B', 'G', 6' ");
INSERT INTO edges (DST, SRC, DISTANCE) VALUES ('C', 'G', "4");
INSERT INTO edges (DST, SRC, DISTANCE) VALUES (', 'G', '5');
INSERT INTO edges (DST, SRC, DISTANCE) VALUES ('E', 'H', '1');
INSERT INTO edges (DST, SRC, DISTANCE) VALUES ('F', 'H', '6');
INSERT INTO edges (DST, SRC, DISTANCE) VALUES ('G', 'H', '3');
INSERT INTO edges (DST, SRC, DISTANCE) VALUES ('E', 'I', '4');
INSERT INTO edges (DST, SRC, DISTANCE) VALUES ('F', 'I', '3');
INSERT INTO edges (DST, SRC, DISTANCE) VALUES ('G', 'I', '3');
INSERT INTO edges (DST, SRC, DISTANCE) VALUES ('H', 'J', '3');
INSERT INTO edges (DST, SRC, DISTANCE) VALUES ('I', 'J', '4');
-SELECT the recursive subquery factoring (RSF) / Common Table Expressions (CTE)
-INCLUDE the starting point of Sub
WITH the railways (root, src, dst, path, distance, cost, lev, iscyc) AS
(SELECT 'A', NULL, 'A', 'A', 0, 0, 1, 0
OF the double
UNION ALL
SELECT p.root,
e.SRC,
e.DST,
p.Path | ',' || e.DST,
e.distance,
p.cost + e.distance,
p.Lev + 1,
-good idea?
p.iscyc
RAILWAYS p
JOIN the edges e WE (e.src = p.dst)
AND lev + 1 < = 3
)
Lev RESEARCH FIRST WIDTH, cost line_no SET
CYCLE of dst SET is_cycle to 1 by DEFAULT 0
SELECT *.
TRAIL pr
WHERE 1 = 1
- AND is_cycle = 0
- AND DST = 'J '.
ORDER BY lev;
DISTANCE FROM ROOT SRC DST LEV ISCY LINE_NO IS_CYCLE COST PATH
-------------------------------------------------------------------
A A A 0 0 1 0 1 0
A A D A,D 3 3 2 0 3 0
A A B A,B 2 2 2 0 2 0
A A C A,C 4 4 2 0 4 0
A C F A,C,F 2 6 3 0 9 0
A E C E A, C, 3 7 3 0 10 0
AN E D A, D, E 4 7 3 0 11 0
B G G, B, 6 8 3 0 12 0
A C AN A, C, 4 8 3 0 13 1
A D G, D, G 5 8 3 0 14 0
G C G, C, 4 8 3 0 15 0
B E A, B, E 7 9 3 0 16 0
A B F A,B,F 4 6 3 0 7 0
A B A A,B,A 2 4 3 0 6 1
A D F A,D,F 1 4 3 0 5 0
A D A A,D,A 3 6 3 0 8 1
There is a powerful way to fill the column ISCY, while the recursion like the IS_CYCLE column?
I tried the table nested in CTE before but had a few questions. The code below works in 11g but not 12 c:
create or replace type nt_char in the table to the varchar2 (20);
/
WITH the railways (root, src, dst, path, distance, cost, lev, iscyc) AS
(SELECT 'A', NULL, 'A', CAST (nt_char ('A') AS nt_char), 0, 0, 1, 0)
OF the double
UNION ALL
SELECT p.root,
e.SRC,
e.DST,
nt_char (e.dst) MULTISET UNION ALL p.Path,
e.distance,
p.cost + e.distance,
p.Lev + 1,
CASE WHEN e.dst MEMBER OF p.path, 1 ELSE 0 END AS iscyc
RAILWAYS p
JOIN the edges e WE (e.src = p.dst)
AND lev + 1<>
)
Lev RESEARCH FIRST WIDTH, cost line_no SET
CYCLE of dst SET is_cycle to 1 by DEFAULT 0
SELECT *.
TRAIL pr
WHERE 1 = 1
- AND is_cycle = 0
- AND DST = 'J '.
ORDER BY lev;
-
Recursive subquery factoring: calculate aggregates
Table T represents a tree. Each record is a node, and each node has only one parent. This query calculates the SUM() of each branch for each node.
WITH T AS (SELECT 1 ID, NULL parent_id, NULL VALUE FROM dual UNION ALL SELECT 10 ID, 1 parent_id, 1000 VALUE FROM dual UNION ALL SELECT 20 ID, 1 parent_id, 2000 VALUE FROM dual UNION ALL SELECT 30 ID, 10 parent_id, 3000 VALUE FROM dual UNION ALL SELECT 40 ID, 10 parent_id, 4000 VALUE FROM dual UNION ALL SELECT 50 ID, 20 parent_id, 5000 VALUE FROM dual UNION ALL SELECT 60 ID, 1 parent_id, 6000 VALUE FROM dual UNION ALL SELECT 70 ID, 60 parent_id, 7000 VALUE FROM dual UNION ALL SELECT 80 ID, 70 parent_id, 8000 VALUE FROM dual ) SELECT CAST(LPAD(' ', (LEVEL-1)*4) || ID AS VARCHAR2(20)) id ,VALUE self_value , (SELECT SUM (VALUE) FROM T t2 CONNECT BY PRIOR t2.ID = t2.parent_id START WITH ID = T.ID) branch_value FROM T CONNECT BY PRIOR t.id = t.parent_id START WITH t.parent_id IS NULL ORDER SIBLINGS BY t.id; ID SELF_VALUE BRANCH_VALUE -------------------- ---------- ------------ 1 36000 10 1000 8000 30 3000 3000 40 4000 4000 20 2000 7000 50 5000 5000 60 6000 21000 70 7000 15000 80 8000 8000 9 rows selected.
I tried to reach the same result of this query using the new syntax for subquery factoring. Any help would be really appreciated!
Hello
I think it's one of those things that CONNECT BY is better.
Here's a way to do it using a recursive clause (AND not CONNECT BY):
WITH recursive_results (ancestor_id, descendant_id, value, lvl, lineage) AS
(
SELECT id AS ancestor_id
id LIKE descendant_id
value
, 1 AS lvl
, TO_CHAR (id, "9999") AS line
T
UNION ALL
SELECT r.ancestor_id
t.id AS descendant_id
t.valeur
r.lvl + 1 AS lvl
r.lineage | ' /'
|| To_char (t.id, '9999') AS line
T
JOIN recursive_results r WE t.parent_id = r.descendant_id
)
SELECT LPAD (' ' ')
, 4 * (
SELECT MAX (lvl) - 1
OF recursive_results
WHERE descendant_id = m.ancestor_id
)
) || ancestor_id AS indented_id
SUM (CASE WHEN ancestor_id = descendant_id THEN value END) AS self_value
The amount (value) AS branch_value
OF recursive_results m
GROUP BY ancestor_id
ORDER BY)
SELECT MAX (lineage) DUNGEON (DENSE_RANK LAST ORDER BY lvl)
OF recursive_results
WHERE descendant_id = m.ancestor_id
)
;
Output (even you have):
INDENTED_ID SELF_VALUE BRANCH_VALUE
-------------------- ---------- ------------
1 36000
10-1000-8000
30 3000 3000
40 4000 4000
20-2000-7000
50 5000 5000
60 6000 21000
70-7000-15000
80-8000-8000
-
Hierarchical data, how to aggregate over levels in hierarchical query?
Hello
I hope someone can help me.
I held in a data table ("" what part was built in what other part of when when? "')
ID parent_id build_in build_out
1 NULL NULL NULL
2/1 2010 2012
3 2 2011 2013
4 2 2013 NULL
What are the parts is stored in a separate table.
Now I want to know when when which part was built in the first level, in the example, I want to know that
1 was simply a part of 1
2 was part of 1 between 2010 and 2012
3 was part of 1 between 2011 and 2012
4 has never been a part of 1
I tried several approaches - if there is a fixed number of levels between the types, I am interested I can do using joins and more/less (similarly as some nvl). Unfortunately this is not always the case (some parts appear on several levels).
If I'm only interested in the parts that are never deleted, I can get by using a style of connect request and get the current configuration. But I can't seem to understand the time connecting part to the high level in such a query, or even filtering absurd combinations (like "4 in 1" in the example above). I could deal with the data recovered outside the database, but I prefer not.
Is there a way to obtain the hierarchical data with an aggregate (min, max) for all levels?What version of Oracle you are on?
In 11.2.x, you can use the recursive subquery factoring. Something like
with t (id, parent_id, build_in, build_out) as ( select 1, null, null, null from dual union all select 2, 1, 2010, 2012 from dual union all select 3, 2, 2011, 2013 from dual union all select 4, 2, 2013, null from dual ) , c1 (root_id, id, parent_id, build_in, build_out) as ( select id, id, parent_id, 0, 9999 from t where parent_id is null union all select root_id, t.id, t.parent_id , greatest(nvl(t.build_in,0), nvl(b.build_in,0)) , least(nvl(t.build_out,9999), nvl(b.build_out,9999)) from c1 b, t where b.id = t.parent_id ) select * from c1 where build_in < build_out ; ROOT_ID ID PARENT_ID BUILD_IN BUILD_OUT ------- ----- ---------- --------- ---------- 1 1 0 9999 1 2 1 2010 2012 1 3 2 2011 2012
With a hierarchical query by using the syntax connection, you could do something like
select * from ( select connect_by_root id as root, id , greatest(nvl(build_in,0), nvl(prior build_in,0)) as max_in, least(nvl(build_out,9999), nvl(prior build_out,9999)) as min_out from t start with parent_id is null connect by parent_id = prior id ) where max_in < min_out ;
but it is not powerful enough. This version compares the dates between a current and previous levels, but the recursive subquery is to compare the dates in the current level for the winners of the comparisons to the previous level. Not sure if it's an important distinction for your needs, however.
If you are on 11.2 I advise to use the recursive subquery factoring. If this isn't the case, you can try the link by version.
Kind regards
Bob -
Getting the line without the use of hierarchical query values
Hi all
I want to know the hierarchical values without using a hierarchical query. I have two tables EMP, DEPT
EMP table is to have two columns (empid, mgrid)
DEPT table is to have two columns (deptid, empid)
Data of the EMP
1,
2, 1
3, 2
4, 3
Data DEPT
10, 1
Each time, I gave deptid = 10, I need to know the this deptid empid then who is this empid (child levels as well). In this case, the output should be
1
2
3
4
I don't want to use hierarchical query.
Thanks in advance.
Thank you
PALYou can use the RECURSIVE subquery, if you are 11 GR 2, like this
SQL> with EMP (empid,mgrid) as 2 ( 3 select 1,null from dual union all 4 select 2, 1 from dual union all 5 select 3, 2 from dual union all 6 select 4, 3 from dual 7 ), 8 dept(deptid,empid) as 9 ( 10 select 10, 1 from dual 11 ), 12 t(empid,mgrid) as 13 ( 14 select empid,mgrid 15 from emp e 16 where empid in ( 17 select d.empid 18 from dept d 19 where deptid = 10 20 ) 21 union all 22 select e.empid,e.mgrid 23 from emp e, t 24 where e.mgrid = t.empid 25 ) 26 select * 27 from t; EMPID MGRID ---------- ---------- 1 2 1 3 2 4 3
Yet, the question is valid - why can't use you a hierarchical query?
Published by: JAC on May 29, 2013 12:37
-
I have a table with ID of origin and destination.
There may be a dynamic number of connections (wait is no more than 5) and the relationship is still one by one: A-> B-> C-> D
A or B or C or D can only apear in a relationship, this average-> C does not already connected to B e because.
I want to create one interviewed who receives the last values of destination and a column with the original values that are associated with this return value:
for example:
If my parameter value is D, my result will be a column of three lines: A, B, C
If my parameter value is C, my result will be a column with two lines: A, B
example:
I tried to conect by front and connect as root but could not achieve.create table test_list as ( select 32000 origin, 68200 destination from dual union all select 60000 origin, 168200 destination from dual union all select 8200 origin, 36600 destination from dual union all select 36600 origin, 8400 destination from dual union all select 8400 origin, 61800 destination from dual )
found also some articles on 'Recursive subquery factoring' but it got even worse because I couldn't make it work.
My database is 'Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64 bit Production'
So in my list:SELECT origin from test_list where origin in (select * FROM ( SELECT CONNECT_BY_ROOT destination FROM test_list CONNECT BY PRIOR origin = destination and destination =68200 ) )
32000 connects to 68200
60000 connects to 168200
8200 connects to 36600
36600 connects to 8400
8400 connects to 61800
My results are expected:
If the parameter 'VALUE_TO_SEARCH' = 68200 I wait only: 32000
If the parameter 'VALUE_TO_SEARCH' = 168200 I wait only: 60000
If the parameter 'VALUE_TO_SEARCH' = 61800 I expect: 8400,36600,8200
If the "VALUE_TO_SEARCH" parameter = 32000 I expect any results.
What would be the best method to use for optimal performance and cash is with the CONNECT_BY_ROOT and "Se CONNECT BY FRONT" that I am I did wrong here?
Best regards
Ricardo TomasHello
With the help of a WITH recursive clause:
VARIABLE value_to_search NUMBER EXEC :value_to_search := 168200; EXEC :value_to_search := 61800; -- Only the last one above matters WITH tree_results (origin, given_destination) AS ( SELECT origin, destination FROM test_list WHERE destination = :value_to_search UNION ALL SELECT t.origin , r.given_destination FROM test_list t JOIN tree_results r ON r.origin = t.destination ) SELECT * FROM tree_results ;
-
I'm trying to learn more about the recursive subquery, but I'm stuck with this one. For each word, recursively remove 1, 2, 3... n characters of the word for all possible combinations. The word length is variable. For example, would be the expected result for the words 'abcd' and '123'. A PLSQL solution would be better?
ABCD:
1 deleted character: bcd, CDA, abd, abc
2 deleted characters: cd, bd, BC., ad, ac, ab
3 characters deleted: d, c, b, a
4 removed characters: null
123:
1 deleted character: 12, 13 and 23
2 deleted characters: 1, 2, and 3
3 deleted characters: null
Hello
Here's a solution that makes everything you asked for, including characters removed (in order or appearance).
As I used SYS_CONNECT_BY_PATH for a concatenated string of all of the included characters, so I used SYS_CONNECT_BY_PATH for a concatenated string of all characters between those included.
WITH single_characters (c, n word) AS
(
SELECT Word
, NULL AS c
, 1 AS n
The CBC
UNION ALL
SELECT Word
SUBSTR (word, n - 1) c
, n + 1 AS n
OF single_characters
WHERE n<= length="">=>
)
SELECT Word
REPLACE (SYS_CONNECT_BY_PATH (c, "ab")
, 'ab '.
) AS comb
LENGTH (word) + 1 - LEVEL AS num_removed
REPLACE (SYS_CONNECT_BY_PATH (SUBSTR (Word
N ADVANCE
, n - (n + 1 PREREQUISITE)
)
, "ba".
)
, "ba".
) || SUBSTR (Word, n) AS deleted
OF single_characters
START WITH n = 1
CONNECT BY NOCYCLE Word = Word PREREQUISITE
AND n > PREREQUISITE n
AND LEVEL<= length="">=>
ORDER BY word
LEVEL DESC
deleted
;
Output:
NUM_REMOVED DELETED WORD COMB
---------- ---------- ----------- -------
123 23 1 1
123 13 1 2
123 12 1 3
123 3 2 12
123 2 2 13
123 1 2 23
123 3 123
ABCD bcd 1A
ABCD DCO 1 b
Abu abd 1 c
ABCD abc 1 d
AB cd 2 ABCD
ABCD 2 ac comics
ABCD BC 2
ABCD ad 2 BC.
ABCD ac 2 bd
ABCD ab 2 cd
ABCD d 3 abc
ABCD c 3 abd
ABCD b 3 CDA
ABCD has 3 bcd
ABCD ABCD 4
lee le 1 e
lee le 1 e
lee ee 1 l
lee l 2 ee
lee e 2 le
lee e 2 le
lee 3 lee
With the exception of the order of the rows in the result, it's exactly what you asked in response #2.
If the order of the rows is important, I'm sure we can get it exactly as you wish; just explain exactly how it needs to be sorted.
-
How to write a hierarchical query so that only the child nodes are displayed?
Hi all
I have a hierarchical query that I use in an area of tree demand APEX and there are nodes that have no children and I am trying to find a way to not display these nodes. Essentially if the user does not have to develop a lot of knots to know that nothing exists at the most detailed level.
The data are based on the Oracle Fusion FND tables but for example purposes here is enough data to illustrate my question:
create table APPL_TAXONOMY_HIERARCHY (SOURCE_MODULE_ID varchar2(30), TARGET_MODULE_ID varchar2(30)); create table APPL_TAXONOMY_TL (module_id varchar2(30), description varchar2(100), user_module_name varchar2(30), language varchar2(5)); create table APPL_TAXONOMY (MODULE_ID varchar2(30), MODULE_NAME varchar2(30), MODULE_TYPE varchar2(10), MODULE_KEY varchar2(30)); create table TABLES (table_name varchar2(30), module_key varchar2(30));
insert into APPL_TAXONOMY_TL values ('1', null, 'Oracle Fusion', 'US' ); insert into APPL_TAXONOMY_TL values ('2', null, 'Financials', 'US' ); insert into APPL_TAXONOMY_TL values ('3', null, 'Human Resources', 'US' ); insert into APPL_TAXONOMY_TL values ('20', null, 'Accounts Payable', 'US' ); insert into APPL_TAXONOMY_TL values ('10', null, 'General Ledger', 'US' ); insert into APPL_TAXONOMY_HIERARCHY values ('1', 'DDDDDDDD'); insert into APPL_TAXONOMY_HIERARCHY values ('2', '1'); insert into APPL_TAXONOMY_HIERARCHY values ('3', '1'); insert into APPL_TAXONOMY_HIERARCHY values ('4', '1'); insert into APPL_TAXONOMY_HIERARCHY values ('10', '2'); insert into APPL_TAXONOMY_HIERARCHY values ('20', '2'); insert into APPL_TAXONOMY values ('1', 'Fusion', 'PROD', 'Fusion'); insert into APPL_TAXONOMY values ('2', 'Financials', 'FAMILY', 'FIN'); insert into APPL_TAXONOMY values ('10', 'GL', 'APP', 'GL'); insert into APPL_TAXONOMY values ('3', 'Human Resources', 'FAMILY', 'HR'); insert into APPL_TAXONOMY values ('20', 'AP', 'APP', 'AP'); insert into tables values ('GL_JE_SOURCES_TL','GL'); insert into tables values ('GL_JE_CATEGORIES','GL');
My hierarchical query is as follows:
with MODULES as ( SELECT h.source_module_id, b.user_module_name, h.target_module_id FROM APPL_TAXONOMY_HIERARCHY H, APPL_TAXONOMY_TL B, APPL_TAXONOMY VL where H.source_module_id = b.module_id and b.module_id = vl.module_id and vl.module_type not in ('PAGE', 'LBA') UNION ALL select distinct table_name, table_name, regexp_substr(table_name,'[^_]+',1,1) from TABLES --needed as a link between TABLES and APPL_TAXONOMY union all select module_key as source_module_id, module_name as user_module_name, module_id as target_module_id from appl_taxonomy VL where VL.module_type = 'APP') SELECT case when connect_by_isleaf = 1 then 0 when level = 1 then 1 else -1 end as status, LEVEL, user_module_name as title, null as icon, ltrim(user_module_name, ' ') as value, null as tooltip, null as link FROM MODULES START WITH source_module_id = '1' CONNECT BY PRIOR source_module_id = target_module_id ORDER SIBLINGS BY user_module_name;
In Oracle APEX, this gives a tree with the appropriate data, you can see here:
https://Apex.Oracle.com/pls/Apex/f?p=32581:29 (username: guest, pw: app_1000);
The SQL of the query results are:
STATUS TITLE LEVEL VALUE ---------- ---------- ------------------------------ ------------------------------
1 1 oracle Fusion Oracle Fusion -1 2 financial tables Financials -1 3 accounts payable Accounts payable 0 4 AP AP -1 General Accounting 3 General Accounting -1 4 GL GL 0 5 GL_JE_CATEGORIES GL_JE_CATEGORIES 0 5 GL_JE_SOURCES_TL GL_JE_SOURCES_TL 0 2 human resources Human resources The lowest level is the name of the table to level 5. HR is not any level under level 2, in the same way, "AP" (level 4) has nothing below, i.e. no level 5 and that's why I don't want to show these nodes. Is this possible with the above query?
Thanks in advance for your suggestions!
John
Hello
The following query will include only the nodes of level = 5 and their ancestors (or descendants):
WITH modules LIKE
(
SELECT h.source_module_id
b.user_module_name AS the title
h.target_module_id
To appl_taxonomy_hierarchy:
appl_taxonomy_tl b
appl_taxonomy vl
WHERE h.source_module_id = b.module_id
AND b.module_id = vl.module_id
AND vl.module_type NOT IN ('PAGE', "LBA")
UNION ALL
SELECT DISTINCT
table-name
table_name
, REGEXP_SUBSTR (table_name, ' [^ _] +')
From the tables - required as a link between the TABLES and APPL_TAXONOMY
UNION ALL
SELECT module_key AS source_module_id
AS user_module_name module_name
module_id AS target_module_id
Of appl_taxonomy vl
WHERE vl.module_type = 'APP '.
)
connect_by_results AS
(
SELECT THE CHECK BOX
WHEN CONNECT_BY_ISLEAF = 1 THEN 0
WHEN LEVEL = 1 THEN 1
OF ANOTHER-1
The END as status
LEVEL AS lvl
title
-, NULL AS icon
, LTRIM (title, "") AS the value
-, NULL as ToolTip
-, Link AS NULL
source_module_id
SYS_CONNECT_BY_PATH (source_module_id - or something unique
, ' ~' - or anything else that may occur in the unique key
) || ' ~' AS the path
ROWNUM AS sort_key
Modules
START WITH source_module_id = '1'
CONNECT BY PRIOR Source_module_id = target_module_id
Brothers and SŒURS of ORDER BY title
)
SELECT the status, lvl, title, value
-, icon, tooltip, link
OF connect_by_results m
WHEN THERE IS)
SELECT 1
OF connect_by_results
WHERE the lvl = 5
AND the path AS ' % ~' | m.source_module_id
|| '~%'
)
ORDER BY sort_key
;
You may notice that subqueries modules and the connect_by_results are essentially what you've posted originally. What was the main request is now called connect_by_results, and it has a couple of additional columns that are necessary in the new main request or the EXISTS subquery.
However, I am suspicious of the 'magic number' 5. Could you have a situation where the sheets you are interested in can be a different levels (for example, some level = 5 and then some, into another branch of the tree, at the LEVEL = 6, or 7 or 4)? If so, post an example. You have need of a Query of Yo-Yo, where you do a bottom-up CONNECT BY query to get the universe of interest, and then make a descendant CONNECT BY query on this set of results.
-
Subquery factoring clause and the temporary table
Is it possible (probably a hint) to specify the Oracle to create a temporary table in subquery factoring (with...) clause?
So if I have a query
How can I do Oracle to generate a plan that creates a temporary table for t1 (and not using t1 as inline view)?with t1 as (select ...) select ...
Hello
use the indicator to materialize:
with t1 as (select /*+ materialize */ ...) select ...
Herald tiomela
http://htendam.WordPress.com -
hierarchical query of VO in the ofa page
Hello
I try to use the hierarchical query in VO for the display of all levels of supervisors for employee and want to display in populist:
This is the query
REPLACE SELECT distinct (mgx.full_name, "',' ') supervisor_full_name;
XPP. Employee_number
OF per_assignments_x pax,.
per_people_x ppx,
per_people_x mgx,
per_positions pp,
per_jobs pj,
per_position_definitions ppd
WHERE ppx.person_id = pax.person_id
AND ppx.current_employee_flag = 'Y '.
AND mgx.person_id = pax.supervisor_id
AND pp.position_id = pax.position_id
AND pj.job_id = pax.job_id
AND ppd.position_definition_id = pp.position_definition_id
START WITH ppx.employee_number =: 1
CONNECT BY PRIOR MGX.employee_number = ppx.employee_number AND LEVEL < 6
I'm trying to get this VO implemented PR when I run the page in jdeveloper with parameter binding, it's getting error.
oracle.apps.fnd.framework.OAException: oracle.jbo.SQLStmtException: 27122 Houston: SQL error in the preparation of the statement.
I would like to delete the parameter binding and executing CO VO past these two statements may be:
START WITH ppx.employee_number =: 1
CONNECT BY PRIOR MGX.employee_number = ppx.employee_number AND LEVEL < 6
How can I do?
Thank you
MK
has worked?
-
Summarize the costs with hierarchical query
Hello Oracle experts! I need some advice because I am very new to the hierarchical queries really new to Oracle. I have the following table:
CREATE TABLE routes
(
from VARCHAR2(15),
to VARCHAR2(15),
cost NUMBER
);
INSERT INTO routes VALUES('San Francisco', 'Denver', 1000);
INSERT INTO routes VALUES('San Francisco', 'Dallas', 10000);
INSERT INTO routes VALUES('Denver', 'Dallas', 500);
INSERT INTO routes VALUES('Denver', 'Chicago', 2000);
INSERT INTO routes VALUES('Dallas', 'Chicago', 600);
INSERT INTO routes VALUES('Dallas', 'New York', 2000);
INSERT INTO routes VALUES('Chicago', 'New York', 3000);
INSERT INTO routes VALUES('Chicago', 'Denver', 2000);
I want to calculate the costs through the hierarchy, to get the following result:
FROM TO COST
--------------- --------------- -----
San Francisco Dallas 10000 //San Francisco -> Dallas
San Francisco Denver
1000 //San Francisco -> DenverSan Francisco Chicago 10600 //San Francisco -> Dallas -> Chicago (10000 + 600)
San Francisco New York 12000 //San Francisco -> Dallas -> New York (10000 + 200)
San Francisco Chicago 3000 //San Francisco -> Denver -> Chicago (1000 + 2000)
San Francisco Dallas 1500 //San Francisco -> Denver -> Dallas (1000 + 500)
etc..
I from imagined using the CONNECT BY PRIOR statement to get the hierarchy and have written a query that runs through him:
SELECT CONNECT_BY_ROOT from, to
FROM routes
CONNECT BY NOCYCLE PRIOR to = from;
But I have absolutely no idea how summarize right costs, I could use some help.
Thank you for your advice.
Hello
Here's a way
SELECT DISTINCT
CONNECT_BY_ROOT from_city AS from_city
to_city
, SYS_CONNECT_BY_PATH (to_city, '->') is ARRESTED - if wanted
, XMLQUERY (SYS_CONNECT_BY_PATH (cost, '+'))
BACK CONTENT
) .getnumberval () SUCH as total_cost
CHANNELS
WHERE CONNECT_BY_ROOT from_city <> to_city
CONNECT BY NOCYCLE from_city = to_city PRIOR
ORDER BY from_city
to_city
stops
;
FROM and TO are the keywords of the Oracle, they really ugly column names. I have changed the from_city and to_city.
-
Get the value of the root of a hierarchical query
I'm writing a hierarchical query for one of my paintings.
I need to know the value of the root by entering a value of sheet of any level.
for example: I enter a value which is at level 4. I need to find the value of the root without displaying the other levels.
Help, please.
Thank you.TRY THIS,
SELECT ename, PRIOR ename MGRNAME FROM emp WHERE ename = START WITH mgr IS NULL CONNECT BY PRIOR empno = mgr
Published by: NSK2KSN on August 9, 2010 18:10
-
Hierarchical query, differentiate between the leafs and nodes?
Hello
I started coding a small program PL/SQL (not finished) with a hierarchical query:
This program must return a hierarchical HTML code like this (he does not at the moment):DECLARE CURSOR cur IS select op_id||' '||op_descript as description, level from operations connect by prior op_id = op_parent_op_id start with op_id = 0; BEGIN HTP.prn('<ul id="arbre2" class="filetree">'); for c in cur loop HTP.prn('<li><span class="folder">'||c.description||'</span></li>'); end loop; HTP.prn('</ul>'); END;
then jQuery will make a tree.<ul id="arbre" class="filetree"> <li><span class="folder">Folder 1</span> <ul> <li><span class="file">Item 1.1</span></li> </ul> </li> <li><span class="folder">Folder 2</span> <ul> <li><span class="folder">Subfolder 2.1</span> <ul> <li><span class="file">File 2.1.1</span></li> <li><span class="file">File 2.1.2</span></li> </ul> </li> <li><span class="file">File 2.2</span></li> </ul> </li> <li class="closed"><span class="folder">Folder 3 (closed at start)</span> <ul> <li><span class="file">File 3.1</span></li> </ul> </li> <li><span class="file">File 4</span></li> </ul>
But I don't know how to differentiate leafs and nodes in the request, to allow me to specify a class = 'file' or class = 'file '.
Does anyone have a solution?
A more general question might be: how to generate a hierarchical HTML code exactly as it is above my table operations (op_id, op_parent_op_id, descript).
Thank you.
Yann.Discover the CONNECT_BY_ISLEAF pseudo-column. Returns 1 if it is a leaf and zero otherwise.
HTH!
-
How to convert the hierarchical query of SQL Server (CTE) to Oracle?
How to convert the hierarchical query of SQL Server (CTE) to Oracle?
WITH cte (col1, col2) AS
(
SELECT col1, col2
FROM dbo. [tb1]
WHERE col1 = 12
UNION ALL
SELECT c.col1, c.col2
FROM dbo. [tb1] AS c INNER JOIN cte AS p ON c.col2 = p.col1
)
DELETE one
FROM dbo. [tb1] AS an INNER JOIN b cte
ON a.col1 = b.col1Hello
Something like this maybe:DELETE FROM dbo.tb1 a WHERE EXISTS ( SELECT 1 FROM dbo.tb1 b WHERE a.co11 = b.col1 AND a.col2 = b.col2 START WITH b.col1 = 12 CONNECT BY b.col2 = PRIOR b.col1)
Although you need to do here is to check that CONNECT it BY SELECT, returns records you wait first, then the DELETION should work too.
Maybe you are looking for
-
W500: Are all vertical USB ports?
Are there any USB port horizontal on the W500?
-
What is "Chapter Browsing"?
I recently got my Fuze and I use it mainly to audiobooks. I actually read the manual (!) and learned how to configure for the chapter of "navigation". BUT... I don't know what that means. Can someone explain what the chapter navigation? Barbara
-
Router M10 Valet (need help)
I have Hughes Net satellite for my computer. I bought the Valet router M10 wireless to connect the computer up there. No instruction. What connects where. Any help would be great. Thank you
-
Laser Jet M1132 - white product sheets on the digitization of documents
Hi, I use a HP LaserJet M1132 on Windows 7This morning, for the first time, I started scanning of documents on paper print them without problem. Once completed, the night I tried to analyze other things but so far, the scan started creating white she
-
BlackBerry Hub edit email in conversation view project
Hello Can you help me?? I could ' t determine how to modify a draft e-mail (automatically saved) appear in conversation view... What's wrong?? In normal mode, I can modify the project by opening it. Thank you very much for your help! boscha