CONNECT by PRIOR Clause
Hi friends,
I am using Connect by prior in path, subsequently
SELECT SOCIETY, EMPLOYEE_NO, DM_EMPLOYEE_NO, DM_COMPANY OF)
SELECT DM_COMPANY 100, DM_EMPLOYEE_NO 10, EMPLOYEE_NO 3, 1 COMPANY FROM DUAL UNION ALL
SELECT DM_COMPANY 100, DM_EMPLOYEE_NO 3, EMPLOYEE_NO 4, 1 COMPANY FROM DUAL UNION ALL
SELECT DM_COMPANY 100, DM_EMPLOYEE_NO 4, EMPLOYEE_NO 5, 1 COMPANY FROM DUAL UNION ALL
SELECT DM_COMPANY 100, DM_EMPLOYEE_NO 5, EMPLOYEE_NO 6, 1 COMPANY FROM DUAL UNION ALL
SELECT 2 COMPANY, EMPLOYEE_NO 11, DM_EMPLOYEE_NO 10, DM_COMPANY 100 DOUBLE UNION ALL
SELECT COMPANY 2, EMPLOYEE_NO 12, DM_EMPLOYEE_NO 11, 200 DM_COMPANY FROM DUAL UNION ALL
SELECT COMPANY 2, EMPLOYEE_NO 13, DM_EMPLOYEE_NO 12, 200 DM_COMPANY FROM DUAL UNION ALL
SELECT COMPANY 2, EMPLOYEE_NO 14, DM_EMPLOYEE_NO 13, 200 DM_COMPANY FROM DUAL UNION ALL
SELECT DOUBLE UNION ALL COMPANY 3, EMPLOYEE_NO 3, DM_EMPLOYEE_NO 10, DM_COMPANY 100
SELECT THE COMPANY 4, EMPLOYEE_NO 3, DM_EMPLOYEE_NO 10 DM_COMPANY 100 DOUBLE
)
START WITH DM_EMPLOYEE_NO = 10 AND DM_COMPANY = 100
CONNECT BY (PRIOR EMPLOYEE_NO = DM_EMPLOYEE_NO AND COMPANY BEFORE =: P_COMPANY)
ORDER OF SOCIETY, EMPLOYEE_NO
---------------------------------------------------------------------------------------------------------------------------
I get the following output
LEVEL OF THE SOCIETY EMPLOYEE_NO DM_EMPLOYEE_NO DM_COMPANY
--------- ----------- -------------- ---------- ---------
1 3 10 100 1
1 4 3 100 2
1 4 3 100 2
1 4 3 100 2
1 5 4 100 3
1 5 4 100 3
1 5 4 100 3
1 6 5 100 4
1 6 5 100 4
1 6 5 100 4
2 11 10 100 1
2 12 11 200 2
2 13 12 200 3
2 14 13 200 4
3 3 10 100 1
4 3 10 100 1
16 selected lines.
My Question is, why am I Getting The double rows?
Hello
So the rows that meet the condition to START WITH are:
SELECT DM_COMPANY 100, DM_EMPLOYEE_NO 10, EMPLOYEE_NO 3, 1 COMPANY FROM DUAL UNION ALL
...
SELECT 2 COMPANY, EMPLOYEE_NO 11, DM_EMPLOYEE_NO 10, DM_COMPANY 100 DOUBLE UNION ALL
...
SELECT DOUBLE UNION ALL COMPANY 3, EMPLOYEE_NO 3, DM_EMPLOYEE_NO 10, DM_COMPANY 100
SELECT THE COMPANY 4, EMPLOYEE_NO 3, DM_EMPLOYEE_NO 10 DM_COMPANY 100 DOUBLE
These 4 rows will be at LEVEL 1.
LEVEL 2 will include a line any who meet the conditions CONNECT BY, where PREVIOUSLY referred to LEVEL 1. If the same row in the table of the lines above matches, then it will appear N times LEVEL 2.
For example, this line of the sample data
SELECT DM_COMPANY 100, DM_EMPLOYEE_NO 3, EMPLOYEE_NO 4, 1 COMPANY FROM DUAL UNION ALL
corresponds to 3 lines at LEVEL 1, so this step appears 3 times to LEVEL 2.
This explains why the current results.
If you want to get different results, post the results you want and explain how you get the results of the data. ; in other words, describe what it means to make a line is considered to be one child of another line in the table.
Tags: Database
Similar Questions
-
Hi all of that I'm using a connection by prior clause
Hi all
In my connect by prior clause, I have a request. I want to pass the values to the query of my outer query. Is this possible in Oracle.Can all help me in this. Also paste my request for your reference
SELECT rct2.trx_number
rctl2.customer_trx_line_id
fifs.flex_value_set_id
gcc.code_combination_id
ffvc.child_flex_value
ffvc.parent_flex_value
gcc.segment4
ffvc.description
ffvc.lvl
OF ra_customer_trx_all rct2
ra_customer_trx_lines_all rctl2
ra_cust_trx_line_gl_dist_all rctlgd
gl_code_combinations gcc
(SELECT ffvv.flex_value_set_id
ffvv.flex_value child_flex_value
, LEVEL lvl, ffvv.parent_flex_value
ffvv.description
OF fnd_flex_value_children_v ffvv
WHERE 1 = 1
START WITH ffvv.flex_value = (SELECT
GCC.segment4
OF ra_customer_trx_all rct1,.
ra_customer_trx_lines_all rctl1,
ra_cust_trx_line_gl_dist_all rctlgd,
gl_code_combinations gcc
WHERE rct1.customer_trx_id = rctl1.customer_trx_id
AND rctl1.customer_trx_line_id = rctlgd.customer_trx_line_id
AND rct1.customer_trx_id = rctlgd.customer_trx_id
AND rctlgd.account_class = 'REV '.
AND rctlgd.code_combination_id = gcc.code_combination_id
AND rct1.trx_number =: trx_number - want to move here from the rct2.trx_number that comes from the outer query
AND rctla.customer_trx_line_id =: trx_line_id)-to move rctl2.customer_trx_line_id here which comes from the outer query
Ffvv.parent_flex_value CONNECT BY PRIOR = ffvv.flex_value) ffvc
s fnd_flex_value_sets
fnd_id_flex_segments FIFS
gl_ledgers l
WHERE rct2.customer_trx_id = rctl2.customer_trx_id
AND rctl2.customer_trx_line_id = rctlgd.customer_trx_line_id
AND rct2.customer_trx_id = rctlgd.customer_trx_id
AND rctlgd.code_combination_id = gcc.code_combination_id
AND fifs.flex_value_set_id = ffvc.flex_value_set_id
AND fifs.application_id = 101
AND fifs.flex_value_set_id = s.flex_value_set_id
AND fifs.application_column_name = 'SEGMENT4. '
AND fifs.id_flex_code = ' GL #
AND l.chart_of_accounts_id = fifs.id_flex_num
AND l.ledger_id = rct2.set_of_books_id
AND rctlgd.account_class = 'REV '.
AND rctl2.customer_trx_line_id =: trx_line_id - spend Trx id
AND rct2.trx_number =: trx_number - number of trx Pass
Hi all
I have used a workaround to achieve this.
I created a function and added the query above to function and the values passed as parameters in two places
Thank you
-
Clarification regarding the understanding of the ORACLE CONNECT BY PRIOR Clause
Dear all,
I'm trying to understand the ORACLE CONNECT BY CLAUSE, and I wrote a query to check my understanding.
I wrote the below two queries.
Select the level, lpad ('* ', 2 * (level - 1),'* '). t_ename Ename, empno, sys_connect_by_path(ename,'/') enames, Bishop of PEM
-where empno = empno
Start by empno = 7654
-start with mgr is null
Connect by prior empno = mgr;
LEVEL T_ENAME ENAMES EMPNO, MGR
---------- -------------------- -------------------------------------------------- ---------- ----------
1 MARTIN 7654 7698 /MARTIN
Explanation: Start by EMPNO = 7654 here means, the root node is 7654. Connect BY PRIOR EMPNO = average MGR-> for the empno = 7654 are all the people under him. IE. Mgr with 7654.
Select the level, lpad ('* ', 2 * (level - 1),'* '). t_ename Ename, empno, sys_connect_by_path(ename,'/') enames, Bishop of PEM
-where empno = empno
Start by empno = 7698
-start with mgr is null
Connect by prior empno = mgr
LEVEL T_ENAME ENAMES EMPNO, MGR
--------- -------------------- -------------------------------------------------- ---------- ----------
1 BLAKE 7698 7839 /BLAKE
2 * ALLEN/BLAKE/ALLEN 7499 7698
2 * 7521 7698 WARD/BLAKE/WARD
2 * MARTIN/BLAKE/MARTIN 7654 7698
2 * TURNER, BLAKE/7844 7698 TURNER
2 * JAMES BLAKE/JAMES 7900 7698
Explanation: Start by EMPNO = 7698 here means, the root node is 7698. Connect BY PRIOR EMPNO = average MGR-> for the empno = 7698 are all the people under him. IE. Mgr with 7698.
I UNDERSTAND TO CONNECT BY PRIOR IS CORRECT?
Please correct me if I'm wrong.
Thank you
MK.Yes, you're right
Kind regards
Sayan M. -
Rebuild the parent/child (connect by prior and where clause)
Hi guys, you have a quick question for you - code as follows:
with t1 as)
Select 1 id, 'Name1' some_name, null, 'Y' parent_id of double some_flag
Union select 2 id, 'Name2', 1 parent_id, 'Y' some_flag of the double
Union select 3 id, "Name3", 2 parent_id, "n" of the double some_flag
Union select id 4, 'Name4.1', 3 parent_id, 'Y' some_flag of the double
Union select id 5, 'Name4.2', 3 parent_id, 'Y' some_flag of the double
Union select id 6, 'Name4.3', 3 parent_id, 'Y' some_flag of the double
Union select id 7, "Name5", 6 parent_id, 'Y' some_flag of the double
)
SELECT id, the_name, parent_id, null new_parent_id
de)
SELECT id, lpad (' ', (level 1) * 3) | some_name the_name, parent_id
from t1
where some_flag = 'Y '.
Start with id = 1
connect by parent_id = prior id
brothers and sisters of order by some_name
)
Output:
ID, THE_NAME, PARENT_ID, NEW_PARENT_ID
1, Name1,
2, name2, 1,.
4, Name4.1,3,
5, Name4.2,3.
6, Name4.3, 3,.
7, name5, 6,.
As you can see, id = 3 is not displayed (because of where some_flag = 'Y'), but parent_id for id in (4,5,6) shows 3.
-Only by using SQL - display 'current parent in valid results' as new_parent_id?
In this example that would be showing new_parent_id = 2 for the id in (4,5,6)
Currently on Oracle 11 g 1 material.
Similar theme (manipulate path), but seems to work with the brothers and SŒURS of ORDER BY.
SELECT id, the_name, parent_id, some_flag, new_parent_id
FROM (SELECT id, lpad (' ', (LEVEL - 1) * 3): some_name the_name,)
some_flag, parent_id,
REGEXP_SUBSTR)
REGEXP_SUBSTR)
SYS_CONNECT_BY_PATH)
DECODE (some_flag, 'Y', id), ' / ').
'[0-9]+[/]+[0-9]+$'),
(0-9] +') new_parent_id
FROM t1
WHERE some_flag = 'Y '.
START WITH id = 1
CONNECT BY PRIOR ID = parent_id
ORDER OF brothers and SŒURS some_name);
-
Connect by PRIOR vrs Connect by LEVEL
Hello
I'm trying to understand the difference between CONNECT BY PRIOR and CONNECT BY LEVEL
I know that CONNECT BY PRIOR is basically specifying a hierarchy and which column / field is the parent (using the PRIOR)
for example
SELECT employe_id, employee_name, level
Employees
CONNECT BY PRIOR employee_id = manager_id;
Q1. What is CONNECT BY LEVEL (for example as shown below) practice? What type of relationship normally described? (below, it is just producucing a series of integers)SELECT the level
OF the double
CONNECT BY LEVEL < = 10;
T2. Use LEVEL with CONNECT BY in this way, a subversive use of the CONNECT IN knowledge is really intended to be used this way?
any advice appreciated,
Jim
Hi, Jim,.
Jimbo wrote:
Hello
I'm trying to understand the difference between CONNECT BY PRIOR and CONNECT BY LEVEL
I know that CONNECT BY PRIOR is basically specifying a hierarchy and which column / field is the parent (using the PRIOR)
for example
SELECT employe_id, employee_name, level
Employees
CONNECT BY PRIOR employee_id = manager_id;
Sometimes, CONNECT BY using hierarchical data, such as a tree. In these cases, it may be useful to use the terns as "parent" and "child." The query above is one of these cases.
In more general terms, CONNECT BY returns the union (UNION ALL, specifically) of lines found by the START WITH clause (LEVEL = 1) and lines found by the clause CONNECT BY (LEVEL > 1). If there is no START WITH clause, every row in the table will appear on LEVEL = 1.
If there is no line on LEVEL = N, then Oracle research lines that meet the CONNECT BY clause, to search for the lines that are connected to each line level = N, for put the LEVEL N + 1. If the CONNECT BY clause is set to TRUE for a given row, while the rank will appear on level N + 1 =. Often (but not always) the CONNECT BY clause uses the PRIOR operator. After PRIOR consent refers to something = N level.
Q1. What is CONNECT BY LEVEL (for example as shown below) practice? What type of relationship normally described? (below, it is just producucing a series of integers)
SELECT the level
OF the double
CONNECT BY LEVEL<>
Since there is no START WITH clause, each line of the double table will be LEVEL = 1.
The CONNECT BY clause means that CONNECT BY always means: If there is a line at = N, then it will be connected to any line that fills the CONNECT BY conditions.
So that the lines will be added to the level = 2? All the lines where condition 2<= 10="" is="" true,="" so="" every="" row="" in="" the="" dual="" table="" will="" be="" on="" level="">=>
What lines will be added to the level = 3? All the lines where condition 3<= 10="" is="" true,="" so="" every="" row="" in="" the="" dual="" table="" will="" be="" on="" level="">=>
...
What lines will be added to the level = 10? All the lines where the condition of 10<= 10="" is="" true,="" so="" every="" row="" in="" the="" dual="" table="" will="" be="" on="" level="10." 10="" is="" true,="" so="" every="" row="" in="" the="" dual="" table="" will="" be="" on="" level="">=>
What lines will be added to the level = 11? All the lines where condition 11<= 10="" is="" true,="" so="" no="" row="" in="" the="" dual="" table="" will="" be="" on="" level="">=>
Since no rows are added to the LEVEL = 11, CONNECT BY query stop right there.
Q2. Use LEVEL with CONNECT BY in this way, a subversive use of the CONNECT IN knowledge is really intended to be used this way?
You can think of it as being subversive. It was certainly not how planners and the deveopers at Oracle for CONNECT BY work in versions 2 to 8; you got an error if you tried in these versions. It is also an exception to the rule that no line can be his own ancestor.
-
START WITH and CONNECT BY PRIOR
Hello
Database: Oracle 11g
1.
SELECT ename, empno, mgr
WCP
START WITH empno = 7839
CONNECT BY PRIOR MGR = EMPNO
Result set:
EMPNO, ENAME MGR
-------- ---------- ----------
KING 7839
2.
SELECT empno.ename, Bishop
WCP
START WITH mgr = 7839
CONNECT BY PRIOR MGR = EMPNO
Result set:
EMPNO, ENAME MGR
-------- ---------- ----------
7566 JONES 7839
7698 BLAKE 7839
7782 CLARK 7839
KING 7839
KING 7839
KING 7839
My questions are:
Q1. What is actually happening in the result defines two queries. I'm not able to grasp the difference when I use START WITH empno = 7839 and START WITH mgr =. 7839
Q2. What is the difference between
CONNECTION BY MGR PRIOR = EMPNO and
CONNECT BY PRIOR EMPNO = MGR?
can someone please help me here?
Thank you
Hello
A CONNECT BY query looks like an operation UNION ALL of the data of different levels, numbered 1, 2, 3 and more.
Level 1 is filled with the START WITH clause.
If there are data on level N, then N + 1 level is filled, using the CONNECT BY clause, which generally refers to something on the N level via the PRIOR operator. Another way to put it is that level N + 1 is filled by a self-join with lines that have already chosen the level N.
If there is no data on the level of N, the query stops.
Let's see how this applies to your queries.
Level being such an important concept in CONNECT BY queries, you might want to see in all your CONNECT BY queries all test and debug the.
1 query, including the level are:
SELECT ename, empno, mgr
LEVEL
FROM scott.emp
START WITH empno = 7839
Empno = mgr PRIOR CONNECTION
;
You will notice that I have re-arranged the CONNECT BY clause. I find it a little more clear medium. Of course, it never changes the results just if you say "x = y" or "y = x.
The results, including the level, are:
LEVEL OF ARCHBISHOP EMPNO, ENAME
---------- ---------- ---------- ----------
7839 KING 1
What happened to produce these results?
First level 1 has been met using the START WITH clause. Level 1 is identical to the results of
SELECT ename, empno, mgr
AS LEVEL 1
FROM scott.emp
WHERE empno = 7839 - same as your START WITH clause
;
It happens to be only 1 row in the table scott.emp who met the empno = 7839 condition, and we show a few columns of this line.
That's all that need the level 1. Something has been on level 1, so we're trying now to complete level 2, using the CONNECT BY condition.
Any line that is included in the level 2 meets the empno = mgr PREREQUISITE condition, where the PREVIOUS operator refers to a line of level 1. In this case, there is only 1 row at level 1, this line gets to have a NULL mgr. Given that PRIOR mgr is NULL in this case, the condition to connect BY
EmpNo = mgr BEFORE equals
EmpNo = NULL and who obviously won't be true for any line, so nothing is added to level 2, and ends the query.
Now let's look at application 2. I'll add another column of debugging, called path, which I'll describe later:
SELECT ename, empno, mgr
LEVEL
, SYS_CONNECT_BY_PATH (empno, "/") AS path
FROM scott.emp
START WITH mgr = 7839
CONNECT BY PRIOR Mgr = empno
LEVEL CONTROL
path
;
Output:
EMPNO, ENAME MGR LEVEL PATH
---------- ---------- ---------- ---------- ---------------
7566 7839 1 7566 JONES
7698 7839 1 7698 BLAKE
7782 7839 1 7782 CLARK
7839 KING 2/7566/7839
7839 KING 2/7698/7839
7839 KING 2/7782/7839
Again, we'll study how people got 1 level. It happens to be 3 scott.emp lines that meet this condition START WITH, so there are 3 lines in the game at level 1.
Given that the data on the level 1, the test of the query to complete level 2, referring to some PRIOR line on level 1. Any line that meets the condition to connect BY, with a line any level 1 in the PREVIOUS line, will appear at level 2.
Let's look at the line at level 1 where ename = 'JONES '. Are there lines in sccott.emp that met the empno = mgr PREREQUISITE condition, where mgr PREREQUISITE is the column of Archbishop of the line with "JONES"? Yes, there are one, the line with ename = 'KING', so that the rank is included at level 2.
Let's look at the line at level 1 where ename = 'BLAKE '. Are there lines in sccott.emp that met the empno = mgr PREREQUISITE condition, where mgr PREREQUISITE is the column of Archbishop of the line with "BLAKE"? Yes, there are one, the line with ename = 'KING', so that the rank is included at level 2.
Let's look at the line at level 1 where ename = 'CLARK '. Are there lines in sccott.emp that met the empno = mgr PREREQUISITE condition, where mgr PREREQUISITE is the column of Archbishop of the line with 'CLARK '? Yes, there are one, the line with ename = 'KING', so that the rank is included at level 2.
There are thus 3 rows at level 2. They happen to all be on the same line of the table emp; It is correct. Remember, CONNECT BY is like a UNION ALL (not just a UNION). It is a UNION of
lines that are at level 1, because him meets the condition to BEGIN WITH, and
lines that are at level 2 because puts it CONNECT BY condition regarding the 'JONES', and
lines that are at level 2, because they meet the condition to connect BY regarding the "BLAKE", and
lines that are at level 2, because they meet the condition to connect BY regarding the "CLARK".
SYS_CONNECT_BY_PATH can enlighten us on that. SYS_CONNECT_BY_PATH (empno, ' / ') shows the empno of each level that caused this line appears in the result set. It's a delimited list /, where the nth element (i.e. the empno after bar oblique nth) is the empno who found the N level.
Since there were data at level 2, the quert now trying to complete level 3.
Is there all the rows in the table that satisfy the CONNECT BY condition (mgr PRIOR = empno) with respect to any line level 2? No, Bishop is be NULL on all lines of level 2, so no line can satisfy this condition CONNECT BY, no lines are added at level 3, and ends the query.
I hope that answers the question:
Q1. What is actually happening in the result defines two queries. I'm not able to grasp the difference when I use START WITH empno = 7839 and START WITH mgr =. 7839
I'll try to not be so detailed answering
Q2. What is the difference between
CONNECTION BY MGR PRIOR = EMPNO and
CONNECT BY PRIOR EMPNO = MGR?
These 2 CONNECT BY conditions are different where you put the PRIOR operator. The operator PRIOR to switching as it change the direction, upward or downward, which move you through the tree you get from level to level.
Bishop PRÉALABLE = empno means the employee on level N + 1 will be the same as the Manager of level N. This means that higher level numbers will be the most senior people in the hierarchy. This is called a query from the bottom up. (Both of the queries that you have posted this same CONNECT BY exact state; both are requests from bottom to top).
Mgr = empno PREREQUISITE or equivalent
PRIOR empno = mgr means exactly the opposite. When you move from level N to level N + 1 in the query, you will move to an older person, to a junior position in the hierarchy. This is called a query from top to bottom. The employee level N will be the Manager of wover is a level N + 1.
-
Join the two trees connect by prior Start With and return only common records?
Oracle 10g Release 2 (10.2)
I have two tables which have structured data. The results, when running queries individually are correct, but I need to join tree a tree two to get only the common records between them.
-Trees a
SELECT ip_entity_name, entity_code, hier_level, entity_parent
Of ip_hierarchy
WHERE hier_level > = 3
CONNECT BY PRIOR Entity_code = entity_parent
START WITH entity_code = "MEWWD";
-The two tree
SELECT ip_entity_name, entity_code, hier_level, entity_parent
Of ipt_hierarchy
WHERE hier_level > = 3
CONNECT BY PRIOR Entity_code = entity_parent
START WITH entity_code = "IPNAM";
If I understand correctly, the joints can not work with CONNECT BY / START WITH queries?
A WITH clause is an option?
If possible, I don't want to put a selection in a database to display object and join against other queries.
Thank you.Hello
jtp51 wrote:
Oracle 10g Release 2 (10.2)
...
If I understand correctly, the joints can not work with CONNECT BY / START WITH queries?Before Oracle 9 it was true. Since you're using Oracle 10, you can if you wish; but I'm guessing that you don't want in this case.
A WITH clause is an option?
If possible, I don't want to put a selection in a database to display object and join against other queries.
Yes, a WITH clause that is an option. Viewed online, as Zhxiang has shown, are another option. Either way gives you a regular display effect without creating a database object.
You did not show a sample and the results, so no one can tell if a join is really what you want. Other possibilities include INTERSECT or an IN subquery.
-
question of the practical exam 1Z0-047 - CONNECT BY PRIOR
Hello
Please see the question by clicking on the link:
http://S10.postimg.org/58ph01n5l/1z0_047_connectby.PNG
I échinés on it (well, for a few minutes or more) because I don't think that the answers are correct.
Option 3 is almost correct, except that in my mind the CONNECT BY PRIOR is backwards.
Option 4 is almost correct, except it takes LEVEL < = 5 rather than LEVEL < 5.
In the end, I chose option 3 because it is more certain that the LEVEL should be < = 5 I had CONNECT it BEFORE the wrong way round.
Now, it so is that I chose the "correct" answer but I am mistaken on option 3? Option 3 is really correct?
My reasoning is this: we want the ancestors of Peter. This means that Peter's parents. CONNECTION BY ParentID = PRIOR PersonID will begin at the root (Peter Clark), and the next record found (using this SQL) will have ParentID = PersonId of pre-registration, namely, of Peter Clark PersonID. In other words, Peter Clark will be parent of the next record. But we are looking for ancestors of Peter Clark; Ancestors of Peter Clark are the parents of Peter Clark. Peter Clark cannot be the parent of the ancestor (or ancestors).
I'm here or not?
Thank you
JasonI agree with you. The connection is evil around all 4 options.
I set up a test with grandparents etc and connect ParentId = PersonId prior only got the start record.
Connect prior ParentId = PersonId gives the correct descent.create table gr (parentid integer, personID integer, name varchar2(30)); insert into gr values (null,1,'GGG'); insert into gr values (1,2,'GGF'); insert into gr values (null,3,'GGM'); insert into gr values (2,4,'GF'); insert into gr values (4,5,'F'); insert into gr values (5,6,'Peter'); select * from gr order by parentid, personid; PARENTID PERSONID NAME ---------- ---------- ------------------------------ 1 2 GGF 2 4 GF 3 4 GF 4 5 F 5 6 Peter 1 GGG 3 GGM select level, gr.* from GR where level <= 5 start with name = 'Peter' connect by ParentId = Prior PersonId Order Siblings by name;
Output has only Peter:
LEVEL PARENTID PERSONID NAME ---------- ---------- ---------- ------------------------------ 1 5 6 Peter 1 row selected.
The reverse
select level, gr.* from GR where level <= 5 start with name = 'Peter' connect by Prior ParentId = PersonId Order Siblings by name; LEVEL PARENTID PERSONID NAME ---------- ---------- ---------- ------------------------------ 1 5 6 Peter 2 4 5 F 3 2 4 GF 4 1 2 GGF 5 1 GGG 3 3 4 GF 4 3 GGM 7 rows selected.
-
Connect by prior and leaves...
Hi gurus...
I have real difficulty understanding connect by prior and leaves...
Is there a good example that you can suggest?Hello
Tina wrote:
Hi gurus...
I have real difficulty understanding connect by prior and leaves...What is "connect per sheet? Post an example.
Did you mean SHEETISCONNECT_BY_? It is a pseudo-column, documented in the manual of the SQL language:
http://download.Oracle.com/docs/CD/B28359_01/server.111/b28286/pseudocolumns001.htm#sthref796Is there a good example that you can suggest?
Make your choice:
http://www.adp-GmbH.ch/ora/SQL/connect_by.html
http://www.oradev.com/connect_by.jsp
http://www.oraclepassport.com/connect%20BY%20PRIOR.html -
I know that the query to run the child in the product, but I want to get the products given the child node parent...
can someone request for...
for example
Select
LPAD (' ', (level - 1) * 2) | product_name as product_name,
product_id,
parent_id,
level
products
Connect prior product_id = parent_id
Start by product_id = 11123;
the output is:
11123
-24545
-67676
but my requirement is if I give
24545
output must be
11123
24545
entry = 67676
ouput
11123
-24545
-67676select lpad(' ', (level - 1) * 2) || product_name as product_name, product_id , parent_id , level from products connect by prior parent_id = product_id start with product_id = 11123;
SY.
-
connect by prior (hierarchical query)
Hi all
Some1 asked me a question that goes like this:
Source
emp_no dep_no
10 110
20 110
30 110
40 110
10 120
10 130
130 20
write a query to get the following output from the source above
emp_no dept_no
110 10203040
10 120
130 1020
Now I stumbled upon solutions with the terms "connect by front" but I'm nt able to understand how oracle produces this result, could someone point me to a good article or a text that can explain this concept very well (I searched on google and have seen many articles, but I could not able to understand since these articles were not all explain)
Concerning
RahulHi, Rahul,
Welcome to the forum!
What you want to do is called chain aggregation . This page shows the different ways to do it:
http://www.Oracle-base.com/articles/10G/StringAggregationTechniques.phpSYS_CONNECT_BY_PATH (which requires a CONNECT BY query) is just a medium. (From Oracle 11.2, LISTAGG is clearly the best way to string aggregation. Before that, SYS_CONNECT_BY_PATH seems to be the most popular, especially if the results must be in order.)
The following pages are introductions to CONNECT BY queries:
http://www.adp-GmbH.ch/ora/SQL/connect_by.html
http://www.oradev.com/connect_by.jsp -
Hi allI have it here is the scenerio
The entry like:
power outlet new_outlet one b b c c d d one AA BB BB CC CC AA l m m n x There There x expected results will be:
one d b d c d AA CC BB CC l n m n x There There x Please help me on this. I tried with you connect by prior clause but not able to get the exact output.
Thank you very much
I lost the create table statement, but if you create a table "T" with the data that you have posted:
with case_2 as ( select outlet, new_outlet, connect_by_root(new_outlet) root_outlet from t start with new_outlet not in (select outlet from t) connect by new_outlet = prior outlet ), case_3 as ( select outlet, new_outlet root_outlet from t where outlet < new_outlet and (outlet, new_outlet) in ( select new_outlet, outlet from t ) ), case_1_start as ( select outlet, new_outlet from t minus select outlet, new_outlet from case_2 minus select * from case_3 minus select root_outlet, outlet from case_3 ), case_1 as ( select outlet, min(connect_by_root(new_outlet)) root_outlet from case_1_start a start with outlet > new_outlet connect by nocycle new_outlet = prior outlet group by outlet, new_outlet ) select outlet, root_outlet from case_1 union all select outlet, root_outlet from case_2 union all select outlet, root_outlet from case_3 order by 2,1;
OUTLET ROOT_OUTLET one one b one c one d one AA AA BB AA CC AA l n m n x There Post edited by: StewAshton - PS. I removed the MAX()... GROUP BY, and then put it back with MIN() because it is necessary if you have cycles in cycles.
-
Hello
I have problems with a bellows to query:
Select CUSTOMER_ID, DECODE (NVL (CURR_LEVEL_FUACC, ROOT_LEVEL_FUACC), 'A', 10', 20', 30', 40)
in (select CUSTOMER_ID, POOLED_FU_ACCOUNT CURR_LEVEL_FUACC, CONNECT_BY_ROOT (POOLED_FU_ACCOUNT)
(Select B.CUSTOMER_ID, CUSTOMER_ID_HIGH, POOLED_FU_ACCOUNT ROOT_LEVEL_FUACC
CUSTOMER_ALL b, RLH_PROCESS_CUST CPP
where (B.CUSTOMER_ID = CPP. CUSTOMER_ID and CPP. BILLCYCLE_GROUP = 3))
First, CUSTOMER_ID_HIGH is nothing connect prior CUSTOMER_ID = CUSTOMER_ID_HIGH)
What is the reason for this error?
at or near line number: 0
SQL return code:-600
SQL error message:
[ORA-00600: internal error code, arguments: [sorgetqb_1], [2] [] [] [], [], []]
P.S.:
Oracle Database 10 g Enterprise Edition release 10.2.0.3.0 - 64bi
PL/SQL version 10.2.0.3.0 - Production
CORE Production 10.2.0.3.0
AMT for HP - UX: release 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production
Thank you
FernandoSolutions are provide in the following note:
Note: 415648.1 -ORA-600 [Sorgetqb_1] with connection by prior Clause *.
-
CONNECTION BY level/prior/root
Hi all
I'm trying to CONNECT BY level/prior/root. But I can barely decipher what it actually does. I think it is a very useful method provided by oracle and can have several uses. can someone help me on this please.
I googled on this topic but everywhere I could find only employee and manager problem that will be solved by query below.
But if we just query as below, we can get the employee and his manage without CONNECT BY.SELECT employee_id, last_name, manager_id FROM employees CONNECT BY PRIOR employee_id = manager_id;
Other queries is obtained to connect by is below, but he could not also include:SELECT employee_id, last_name, manager_id FROM employees;
Please help me.select SYSDATE-41 + level - 1 the_date from dual connect by level <= SYSDATE - SYSDATE-41 + 1
Published by: J2EE_Life on December 6, 2011 08:12J2EE_Life wrote:
Hi allI'm trying to CONNECT BY level/prior/root. But I can barely decipher what it actually does. I think it is a very useful method provided by oracle and can have several uses. can someone help me on this please.
I googled on this topic but everywhere I could find only employee and manager problem that will be solved by query below.
SELECT employe_id, last_name, manager_id
Employees
CONNECT BY PRIOR employee_id = manager_id;But if we just query as below, we can get the employee and his manage without CONNECT BY.
SELECT employe_id, last_name, manager_id
Employees;In fact, those who are not the same.
If you use the standard table of hr.employees, the CONNECT BY query above returns 315 lines, but the secoind query return lines only 107.CONNECTION is useful to show the recursive relationships. It can show you, for example, not only that reports directly to a data manager, but the whole hierarchy of persons under a given head. For example, the following shows the hierarchy starting with an employee named "Rooster":
SELECT employee_id, last_name, manager_id , LEVEL AS lvl , SYS_CONNECT_BY_PATH (last_name, '/') AS path FROM hr.employees START WITH last_name = 'De Haan' CONNECT BY PRIOR employee_id = manager_id; SELECT SYSDATE - SYSDATE-41 + 1 FROM DUAL;
Output:
EMPLOYEE_ID LAST_NAME MANAGER_ID LVL PATH ----------- ---------- ---------- --- -------------------------- 102 De Haan 100 1 /De Haan 103 Hunold 102 2 /De Haan/Hunold 104 Ernst 103 3 /De Haan/Hunold/Ernst 105 Austin 103 3 /De Haan/Hunold/Austin 106 Pataballa 103 3 /De Haan/Hunold/Pataballa 107 Lorentz 103 3 /De Haan/Hunold/Lorentz
Very briefly, here's how it works.
The result set of a query CONNECT BY is the UNION ALL of several queries, all sharing a common SELECT clause.
Any line that meets the conditions of the START WITH clause is supposed to be at LEVEL 1. So you can think of the first branch of the UNION of ALL being (in this case):SELECT employee_id, last_name, manager_id , LEVEL AS lvl , SYS_CONNECT_BY_PATH (last_name, '/') AS path FROM hr.employees WHERE last_name = 'De Haan' UNION ALL ...
Other rows in the result set will be LEVEL = N if the conditions of the CONNECT BY clause, where the PRIOR operator refers to any line level = N - 1.
For example, the line with name = "Hunold" satisfies the condition to connect BY when the FIRST line is the line where last_name = "Rooster", Hunold is in the game with the LEVEL of results = 2.
Another example: the line with name = 'Lorentz' meets the condition to connect BY when the FIRST line is the line where last_name = "Hunold", Hunold is in the result set with LEVEL = 3.
This is a classic example of a tree structure, where the rows in the table are connected to other lines in a parent-child relationship. In a tree, each row has 0 or 1 of the parents, but a line can have any humber children. Haan has 0 parent and 1 child (that is, Hunold). Hunold has 1 parent (De Haan) and 4 children. CONNECT BY queries are useful to deal with the trees like this, especially when the parent-child relationship may extend to a number any levels.Other queries is obtained to connect by is below, but he could not also include:
Select the level - 1 the_date + SYSDATE-41
of the double
connect by level<= sysdate="" -="" sysdate-41="" +="">=>I don't understand whether you.
SYSDATE-SYSDATE is just 0
0 - 41 + 1 is just-40, so you might as well say:select SYSDATE-41 + level - 1 the_date from dual connect by level <= -40;
In addition, LEVEL is always a positive integer, then the CONNECT BY State will never have the value TRUE, and if the CONNECT BY clause can never be TRUE, there is no reason to have a CONNECT BY clause.
A more reasonable request is:SELECT SYSDATE + LEVEL - 1 AS the_date FROM dual CONNECT BY LEVEL <= 4 -- or any positive number ;
which produces this output, the given number of days (4 in this example) from today:
THE_DATE -------------------- 06-Dec-2011 11:08:24 07-Dec-2011 11:08:24 08-Dec-2011 11:08:24 09-Dec-2011 11:08:24
This is a very common way to generate a Counter of Table (a table, or, as in this case, a result set, that matters). For example, if you need for the aggregates per day in a table and you want to show 0 for days that never exist in the table, then you outer join in a table of counter like the one shown above.
This is a common use of CONNECT BY, but this is an unusual exception to the rules that is prohibited govern CONNECT BY queries. I suggest you uderstand how the query on hr.employees, above, the work before you start trying to figure out how it works.Published by: Frank Kulash, December 6, 2011 11:15
The pages below are introductions to CONNECT BY:
http://asktom.Oracle.com/pls/asktom/f?p=100:11:0:P11_QUESTION_ID:489772591421
http://Philip.Greenspun.com/SQL/trees -
logical prior connection problem
Hi all
I have the scenario where I fill in amount of parent based on her children:
create table xx_pack_content)
child varchar2 (255),
parent varchar2 (255),
child_quantity NUMBER
);
insert into xx_pack_content values ('OBJ2', 'OBJ1', 50)
insert into xx_pack_content values ("OBJ3", "OBJ2", 2)
insert into xx_pack_content values ('OBJ4', 'OBJ2', 6)
SELECT the level,
x.Child,
x.Parent,
x.child_quantity
OF xx_pack_content x
START WITH x.parent =: p_parent_val
CONNECT BY PRIOR x.child = x.parent
Brothers and SŒURS ORDER BY level, x.child;
Code:
FUNCTION parent_qty (p_parent_val) RETURN VARCHAR2
CURSOR lcu_cur
IS to SELECT the level,
x.Child,
x.Parent,
x.child_quantity
OF xx_pack_content x
START WITH x.parent = p_parent_val
CONNECT BY PRIOR x.child = x.parent
Brothers and SŒURS ORDER BY level, x.child;
TYPE level_tbltype IS TABLE OF lcu_cur % ROWTYPE;
level_tbl level_tbltype;
lv_parent_content VARCHAR2 (30): = NULL;
lv_level_low VARCHAR2 (30): = NULL;
lv_level_high VARCHAR2 (30): = NULL;
BEGIN
OPEN lcu_cur (p_parent_val);
Get the lcu_cur COLLECT in BULK IN level_tbl;
CLOSE Lcu_cur;
DBMS_OUTPUT. Put_line ("Count:" | ") level_tbl. (COUNT);
lv_level_low: = level_tbl (level_tbl. PREMIER level);
lv_parent_content: = level_tbl (level_tbl..) Quantity FIRST) | » x';
BECAUSE me IN 2.level_tbl. LAST
LOOP
lv_level_high: = level_tbl (i) level;
IF (lv_level_low <>lv_level_high) THEN
lv_parent_content: = lv_parent_content | level_tbl (i) .child_quantity;
lv_level_low: = level_tbl (i) level;
ON THE OTHER
lv_parent_content: = lv_parent_content | ' ('| level_tbl (i) .child_quantity |) » +';
lv_level_low: = level_tbl (vpe_idx2) level.
END IF;
return (lv_parent_content);
END LOOP;
DBMS_OUTPUT. Put_line ('lv_parent_content: ' | lv_parent_content);
END;
expected results:
---------------
I need to build a function that returns the total amount of parent based on the parameter
When I pass "OBJ1" he shud be returned as 50 x (2 + 6) x'
When I pass 'OBJ2' he shud be returned as '(2+6) x'
If there is more than one child for the parent's should be like "(2 + 6 +..)" x'
I wrote a function above, but need help to get a correct output.
Don't know what you want to do when / if have brothers and sisters separated children but anyway...
Oracle Database 11 g Enterprise Edition Release 11.2.0.3.0 - 64 bit Production
With partitioning, OLAP, Data Mining and Real Application Testing optionsSQL > SELECT LISTAGG)
2. DECODE (COUNT (*), 1,)
MAX 3 (TO_CHAR (child_quantity)),
4 '(' || LISTAGG (child_quantity, '+')
5 THE GROUP)
6 ORDER BY child) | ')') || 'x', ' ')
7 IN GROUP)
8 LEVEL CONTROL) Qty.
9 FROM xx_pack_content
10 START WITH PARENT = "OBJ1".
11 children to connect BY PRIOR = PARENT
12 GROUP BY LEVEL;QTY.
--------------------------------------------------------------------------------
50 x (2 + 6) xSQL > SELECT LISTAGG)
2. DECODE (COUNT (*), 1,)
MAX 3 (TO_CHAR (child_quantity)),
4 '(' || LISTAGG (child_quantity, '+')
5 THE GROUP)
6 ORDER BY child) | ')') || 'x', ' ')
7 IN GROUP)
8 LEVEL CONTROL) Qty.
9 FROM xx_pack_content
10 START WITH PARENT = 'OBJ2 '.
11 children to connect BY PRIOR = PARENT
12 GROUP BY LEVEL;QTY.
--------------------------------------------------------------------------------
(2 + 6) xSQL >
Maybe you are looking for
-
Thunderbird does not
-
Win7 need display driver for Satellite L20-214
Hi all I have a toshiba satellite L20-214, 2 GB of ram.Just upgraded to Windows 7 ultimate service pack 1.I have a yellow exclamation mark on the video controller. I went to toshibas site to search for the drivers but my model is not mentioned, so I
-
Hello I am currently working on a code for some scales that reads on the weight and saves it in a file .lvm. The program works as it should, except to save it to the file .lvm. The program saves only the last number before I click on stop, ex number
-
How to find the path to an executable of construction labview
After generating an executable file with Labview and the installer, users install on some PCs. During the installation process, they have the option to install to a directory of choice here. How can I know this way of installation with a non-labview,
-
I forgot my password set on the mail server incoming mail Window, how can I retrieve or reset my password? Esther