Connect By prior with condition
i have a table employee with following fields
EMPLOYEEID, MANAGERID, ISPOOL
i have following query
select employeeid MID , level LVL from employee connect by prior employeeid=managerid and working=-1 start with employeeid =1
i need to change it so that i will return me the tree for all employees who have ispool='Y'
mangers of ispool='Y' may may have ispool='N'
Thanks in Advance
closed ...
requirement changed..
Similar Messages
-
Connect by prior with the hint index not improving the performance
Hi
I have a table ORDERS (transaction table) with a parent order and multiple child orders with a field called orig_tid which track it back to main order.Child order can be further amended it will have mapping to immediate parent and immediate parent will have mapping to its parent.
I want find out al the order associated with main order using connect by prior.
I have created an index on required fileds but still those are not reducing the bytes. It is foing full index scan which are involving big bytes.
[code]
create index idx_test on orders decode(length(nvl(orig_tid,0)),9,substr(tran_no,6),orig_tid)
SELECT /*+ (mmd IDX_TEST) */ *
FROM ORDERS mmd
connect by prior tid=decode(length(nvl(orig_tid,0)),9,substr(tran_no,6),orig_tid)
start_with or_number='13454566654553'
[/CODE]
Explain Plan
ID | OPERATION | NAME | ROWS | BYTES | COST
0 | SELECT STATEMENT | | 9768 | 5093K | 6944
1|CONNECT BY WITH FILTERING| | | |
2|TABLE ACCESS BY INDEX ROWID|ORDERS | 1 |534| 319K
3| INDEX FULL SCAN| IDX_TEST| 976K| | 5652
4|NESTED LOOPS| | | |
5|CONNECT By PUMP | | | |
6| TABLE ACCESS BY INDEX ROWID| ORDERS| 9768 | 5093K| 6944
7| INDEX FULL SCAN | IDX_TEST | 3907| | 5689
Message was edited by: NikhilJunejapost EXPLAIN PLAN for SQL
-
This 9i SQL Reference indicates that it is possible to combine CONNECT BY PRIOR with a join predicate.
In 8i I get the following error:
ORA-01437 cannot have join with CONNECT BY
Cause: A join operation was specified with a CONNECT BY clause. If a CONNECT BY clause is used in a SELECT statement for a tree-structured query, only one table may be referenced in the query.
Action: Remove either the CONNECT BY clause or the join operation from the SQL statement.
I can't find documentation to test the obvious hypothesis - comments?My alternative solutions.
***Sample***
ID PrevID
1 null
2 1
3 1
4 3
***output which we want***
ID PrevID isLeaf
1 null 0
2 1 1
3 1 0
4 3 1
***DDL***
create table CloneIsLeaf as
select 1 as ID,null as PrevID from dual
union select 2,1 from dual
union select 3,1 from dual
union select 4,3 from dual;
--method1
select ID,PrevID,
case when exists(select 1 from CloneIsLeaf b
where a.ID = b.PrevID) then 0 else 1 end as isLeaf
from CloneIsLeaf a
Start With ID = 1
connect by prior ID = PrevID;
--method2
select ID,PrevID,
case when Level < Lead(Level) over(order by RowNum)
then 0 else 1 end as isLeaf
from CloneIsLeaf
Start With ID = 1
connect by prior ID = PrevID;
--method3
select ID,PrevID,
case when LV < Lead(LV) over(order by RowNum)
then 0 else 1 end as isLeaf
from (select ID,PrevID,Level as LV
from CloneIsLeaf
Start With ID = 1
connect by prior ID = PrevID
order siblings by ID);
--method4
select ID,PrevID,
case when LV < Lead(LV) over(order by Row_Num)
then 0 else 1 end as isLeaf
from (select ID,PrevID,LV,RowNum as Row_Num
from (select ID,PrevID,Level as LV
from CloneIsLeaf
Start With ID = 1
connect by prior ID = PrevID
order siblings by ID));on method2 and method3 and method3,
I used that "Hierarchical Queries" is depth-first search (http://en.wikipedia.org/wiki/Depth-first_search)
http://download-west.oracle.com/docs/cd/B19306_01/server.102/b14200/queries003.htm#i2053935
my site :-)
http://www.geocities.jp/oraclesqlpuzzle/10-149.html -
Understanding multiple conditions in connect by prior clause Oracle
Hi ,
Can some one please explain me how to comprehend/understand multiple conditions in connect by prior conditions with some example data.
I am creating a table like this
CREATE TABLE FAMiLY_TREE
GRAND_FATHERID number,
FATHER_ID number,
SON_ID number,
person_name varchar(20)
INSERT INTO FAMILY_TREE (GRAND_FATHERID ,FATHER_ID , SON_ID , PERSON_NAME ) VALUES
(NULL, NULL , 5 , 'Mr X ' );
INSERT INTO FAMILY_TREE (GRAND_FATHERID ,FATHER_ID , SON_ID , PERSON_NAME ) VALUES
(null, 5 , 6 , 'Dave' );
INSERT INTO FAMILY_TREE (GRAND_FATHERID ,FATHER_ID , SON_ID , PERSON_NAME ) VALUES
(5, 6 , 7 , 'Vinny' );
INSERT INTO FAMILY_TREE (GRAND_FATHERID ,FATHER_ID , SON_ID , PERSON_NAME ) VALUES
(5, 6 , 16 , 'Omy' );
INSERT INTO FAMILY_TREE (GRAND_FATHERID ,FATHER_ID , SON_ID , PERSON_NAME ) VALUES
(5, 6 , 17 , 'Vijjy' );
INSERT INTO FAMILY_TREE (GRAND_FATHERID ,FATHER_ID , SON_ID , PERSON_NAME ) VALUES
(6, 7 , 8 , 'Vicky' );
INSERT INTO FAMILY_TREE (GRAND_FATHERID ,FATHER_ID , SON_ID , PERSON_NAME ) VALUES
(6, 7 , 9 , 'Varis' );
INSERT INTO FAMILY_TREE (GRAND_FATHERID ,FATHER_ID , SON_ID , PERSON_NAME ) VALUES
(7, 8 , 10 , 'Vshnu' );
INSERT INTO FAMILY_TREE (GRAND_FATHERID ,FATHER_ID , SON_ID , PERSON_NAME ) VALUES
(7, 8 , 11 , 'dyna' );
INSERT INTO FAMILY_TREE (GRAND_FATHERID ,FATHER_ID , SON_ID , PERSON_NAME ) VALUES
(8, 10 , 14 , 'Marry' );
INSERT INTO FAMILY_TREE (GRAND_FATHERID ,FATHER_ID , SON_ID , PERSON_NAME ) VALUES
(8, 10 , 15 , 'Mac' );
INSERT INTO FAMILY_TREE (GRAND_FATHERID ,FATHER_ID , SON_ID , PERSON_NAME ) VALUES
(7, 9 , 12 , 'Garry' );
INSERT INTO FAMILY_TREE (GRAND_FATHERID ,FATHER_ID , SON_ID , PERSON_NAME ) VALUES
(7, 9 , 13 , 'Ganny' );
SELECT
LPAD(' ', LEVEL*3) || PERSON_NAME FROM FAMILY_TREE
START WITH SON_ID= 6
CONNECT BY PRIOR SON_ID = FATHER_ID
AND PRIOR FATHER_ID = GRAND_FATHERID ;
SELECT
LPAD(' ', LEVEL*3) || PERSON_NAME FROM FAMILY_TREE
START WITH SON_ID= 6
CONNECT BY PRIOR SON_ID = FATHER_ID ;
Both These query return the same o/p
Dave
Vinny
Vicky
Vshnu
Marry
Mac
dyna
Varis
Garry
Ganny
Omy
Vijjy
Can some one please explain me comprehension of both these query or give me a example where i can understand multiple connect by prior conditions
ThanksMaybe (something to play with)
with
family_tree as
(select 'Green' family,null ancestor,1 person,'Mr X' person_name,1900 born from dual union all
select 'Green',1,2,'Dave',1920 from dual union all
select 'Green',2,3,'Vinny',1940 from dual union all
select 'Green',2,4,'Omy',1945 from dual union all
select 'Green',2,5,'Vijjy',1950 from dual union all
select 'Green',3,6,'Vicky',1960 from dual union all
select 'Green',3,7,'Varis',1965 from dual union all
select 'Green',6,8,'Vshnu',1980 from dual union all
select 'Green',6,9,'Dyna',1985 from dual union all
select 'Green',8,10,'Mary',2000 from dual union all
select 'Green',8,11,'Mac',2005 from dual union all
select 'Green',7,12,'Garry',1985 from dual union all
select 'Green',7,13,'Ganny',1990 from dual union all
select 'Brown',null,14,'Joe',1950 from dual union all
select 'Brown',14,15,'Jim',1970 from dual union all
select 'Brown',14,16,'Joy',1975 from dual union all
select 'Brown',14,17,'Jay',1980 from dual union all
select 'Brown',16,18,'Jack',1995 from dual union all
select 'Brown',18,19,'Jake',2010 from dual union all
select 'Brown',18,20,'Jess',2012 from dual
select family,
root_name||' ('||to_char(root_born)||')' "(FA/MO)THER",
children
from (select family,
root_born,
root_name,
ltrim(sys_connect_by_path(person_name||' ('||to_char(born)||')',', '),', ') children
from (select family,
connect_by_root(person_name) root_name,
connect_by_root(born) root_born,
person_name,
born,
row_number() over (partition by family,connect_by_root(person_name) order by born) rn
from family_tree
where level = 2
connect by prior person = ancestor
where connect_by_isleaf = 1
start with rn = 1
connect by prior root_name = root_name
and prior family = family
and prior rn + 1 = rn
order by family desc,root_born
Regards
Etbin -
In a CONNECT BY query, we specify a PRIOR and then we can have more conditions afterwards. Is there any effect on the result by changing the place of these conditions ?
ex 1
SELECT * FROM
CCC_TAB
CONNECT BY COL1 = COL2
AND a(b)
AND (c=d OR (e=f AND g=h))
AND i-j
Changing to
ex 2
SELECT * FROM
CCC_TAB
CONNECT BY COL1 = COL2
AND (c=d OR (e=f AND g=h))
AND a(b)
AND i-j
Because my problem is in ex1, Oracle 9 gives an 0RA error 01436 : recursive in a loop error, But not in ORACLE 8(same code) , but when you re-arrange it as in ex2, works fine in Oracle 8 & 9
1. Will there be any change in the result set, by chaging from ex1 to ex2 ?
2. Would like to know more information why this happens.
Thanks very muchExample of Connect_by_is_leaf
select
DEPTNO,
max(sys_connect_by_path(ENAME,'>')) Names_list
from
select
DEPTNO,
ENAME,
row_number() over (partition by DEPTNO order by DEPTNO) rn
from (
select * from emp ) tab_
where CONNECT_BY_ISLEAF=1
start with rn =1
connect by prior DEPTNO = DEPTNO
and
prior rn+1 = rn
group by DEPTNO
DEPTNO NAMES_LIST
30 >WARD>TURNER>ALLEN>JAMES>BLAKE>MARTIN
20 >JONES>FORD>ADAMS>SMITH>SCOTT
10 >CLARK>KING>MILLER -
Help needed with CONNECT BY PRIOR
I need to display salesrep-manager hierarchy. I'm using the following SQL and am sure I'm doing something wrong somewhere, but just can't pinpoint. Any help is greatly appreciated.
<pre>
SELECT sf.source_name salesrep
,mgr.full_name manager
,level
FROM as_salesforce_v sf
,per_all_assignments_f pass
,per_all_people_f mgr
WHERE sf.role_name = 'Sales Representative'
AND SYSDATE BETWEEN nvl(sf.start_date_active, SYSDATE) AND
nvl(sf.end_date_active, SYSDATE)
AND SYSDATE BETWEEN nvl(pass.effective_start_date, SYSDATE) AND
nvl(pass.effective_end_date, SYSDATE)
AND SYSDATE BETWEEN nvl(mgr.effective_start_date, SYSDATE) AND
nvl(mgr.effective_end_date, SYSDATE)
CONNECT BY PRIOR mgr.person_id = pass.supervisor_id
START WITH pass.person_id = sf.employee_person_id;
</pre>
TIA
Alka.
Forgot to mention, the SQL takes forever to run.
Message was edited by:
user498444as_salesforce_v view stores the salesrep name (as specified by the condition sf.role_name = 'Sales Representative'). This view also has rows for managers of salesreps as that's how Oracle Sales app allows the users to access the app. This table stores employee id in column employee_person_id.
per_all_assignments_f table stores the assignments for all the employees (including salesreps and that's why condition START WITH pass.person_id = sf.employee_person_id). It also stores the employee id for the manager (column supervisor_id).
per_all_people_f has all the employee records (including manager records) and the column person_id is the employee id (condition pass.supervisor_id = mgr.person_id).
Hope this explanation is helpful. -
SQL with connect by prior running for a long time
Hi,
We are using Oracle 10g. Below is a cursor sql which is having performance issues. The pAccountid is being passed from the output of a different cursor. But this cursor sql is running for a long time. Could you please help me in tuning this sql. I believe the subquery with connect by prior is causing the trouble.
The TRXNS is a huge table which is not partitioned. The query is forced to use the index on the accountid of the TRXNS table.
The accountlink table has 20,000 records and the TRXNStrack table has 10,000 records in total.
This sql executes for 200,000 pAccountids and runs for more than 8 hours.
SELECT /*+ INDEX(T TRXNS_ACCOUNTID_NIDX) */ AL.FROMACCOUNTID oldaccountid ,
A.ACCOUNTNUM oldaccountnum,
T.TRXNSID,
T.TRXNSTYPEID,
T.DESCRIPTION ,
T.postdt,
T.TRXNSAMT
FROM
ACCOUNTLINK AL,
TRXNS T,
ACCOUNT A
WHERE AL.TOACCOUNTID IN
(SELECT TOACCOUNTID FROM ACCOUNTLINK START WITH TOACCOUNTID = pAccountid
CONNECT BY PRIOR FROMACCOUNTID = TOACCOUNTID)
AND AL.FROMACCOUNTID = T.ACCOUNTID
AND A.ACCOUNTID = AL.FROMACCOUNTID
AND NOT EXISTS (select 1 from TRXNStrack trck where trck.TRXNSid = t.TRXNSid AND TRXNSTrackReasonid = 1)
AND T.postdt > A.CLOSEDATE
AND T.postdt >= sysdate-2
AND T.postdt <= sysdate;
Create script for trxn table:
CREATE TABLE SP.TRXNS
TRXNSID NUMBER(15) CONSTRAINT "BIN$rpIQEeyLDfbgRAAUT4DEnQ==$0" NOT NULL,
ACCOUNTID NUMBER(15) CONSTRAINT "BIN$rpIQEeyMDfbgRAAUT4DEnQ==$0" NOT NULL,
STATEMENTID NUMBER(15),
TRXNSTYPEID NUMBER(15),
DESCRIPTION VARCHAR2(80 BYTE),
postdt DATE,
TRXNSAMT NUMBER(12,2),
TRXNSREQID NUMBER(15),
LASTUPDATE DATE,
SOURCEID NUMBER(15),
HIDE VARCHAR2(1 BYTE)
TABLESPACE SO_TRXN_DATA
RESULT_CACHE (MODE DEFAULT)
PCTUSED 40
PCTFREE 10
INITRXNS 2
MAXTRXNS 255
STORAGE (
INITIAL 50M
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
FREELISTS 8
FREELIST GROUPS 1
BUFFER_POOL DEFAULT
FLASH_CACHE DEFAULT
CELL_FLASH_CACHE DEFAULT
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;
CREATE INDEX SP.TRXNS_ACCOUNTID_NIDX ON SP.TRXNS
(ACCOUNTID, postdt)
LOGGING
TABLESPACE SO_TRXN_INDEX
PCTFREE 10
INITRXNS 2
MAXTRXNS 255
STORAGE (
INITIAL 64K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
FREELISTS 1
FREELIST GROUPS 1
BUFFER_POOL DEFAULT
FLASH_CACHE DEFAULT
CELL_FLASH_CACHE DEFAULT
NOPARALLEL;
below is the executing plan for this sql taken from toad :
PLAN_ID
TIMESTAMP
OPERATION
OPTIONS
OBJECT_OWNER
OBJECT_NAME
OBJECT_ALIAS
OBJECT_INSTANCE
OBJECT_TYPE
OPTIMIZER
SEARCH_COLUMNS
ID
PARENT_ID
DEPTH
POSITION
COST
CARDINALITY
BYTES
CPU_COST
IO_COST
TEMP_SPACE
ACCESS_PREDICATES
FILTER_PREDICATES
PROJECTION
TIME
QBLOCK_NAME
1121
9/10/2013 3:30
FILTER
1
0
1
1
NOT EXISTS (SELECT 0 FROM "TRXNSTRACK" "TRCK" WHERE "TRXNSTRACKREASONID"=1 AND "TRCK"."TRXNSID"=:B1)
AL."FROMACCOUNTID"[NUMBER,22], "T"."TRXNSID"[NUMBER,22], "T"."TRXNSTYPEID"[NUMBER,22], "T"."DESCRIPTION"[VARCHAR2,80], "T"."POSTDT"[DATE,7], "T"."TRXNSAMT"[NUMBER,22], "A"."ACCOUNTNUM"[VARCHAR2,19]
SEL$5DA710D3
1121
9/10/2013 3:30
FILTER
2
1
2
1
SYSDATE@!-2<=SYSDATE@!
AL."FROMACCOUNTID"[NUMBER,22], "T"."TRXNSID"[NUMBER,22], "T"."TRXNSTYPEID"[NUMBER,22], "T"."DESCRIPTION"[VARCHAR2,80], "T"."POSTDT"[DATE,7], "T"."TRXNSAMT"[NUMBER,22], "A"."ACCOUNTNUM"[VARCHAR2,19]
1121
9/10/2013 3:30
NESTED LOOPS
3
2
3
1
(#keys=0) "AL"."FROMACCOUNTID"[NUMBER,22], "T"."TRXNSID"[NUMBER,22], "T"."TRXNSTYPEID"[NUMBER,22], "T"."DESCRIPTION"[VARCHAR2,80], "T"."POSTDT"[DATE,7], "T"."TRXNSAMT"[NUMBER,22], "A"."ACCOUNTNUM"[VARCHAR2,19]
1121
9/10/2013 3:30
NESTED LOOPS
4
3
4
1
5
1
119
3989858
4
(#keys=0) "AL"."FROMACCOUNTID"[NUMBER,22], "T"."TRXNSID"[NUMBER,22], "T"."TRXNSTYPEID"[NUMBER,22], "T"."DESCRIPTION"[VARCHAR2,80], "T"."POSTDT"[DATE,7], "T"."TRXNSAMT"[NUMBER,22], "A".ROWID[ROWID,10]
1
1121
9/10/2013 3:30
NESTED LOOPS
5
4
5
1
4
1
90
3989690
3
(#keys=0) "AL"."FROMACCOUNTID"[NUMBER,22], "T"."TRXNSID"[NUMBER,22], "T"."TRXNSTYPEID"[NUMBER,22], "T"."DESCRIPTION"[VARCHAR2,80], "T"."POSTDT"[DATE,7], "T"."TRXNSAMT"[NUMBER,22]
1
1121
9/10/2013 3:30
HASH JOIN
SEMI
6
5
6
1
3
2
54
3989094
2
AL."TOACCOUNTID"="TOACCOUNTID"
(#keys=1) "AL"."FROMACCOUNTID"[NUMBER,22]
1
1121
9/10/2013 3:30
INDEX
FULL SCAN
SP
ACCOUNTLINK_AK1
AL@SEL$1
INDEX (UNIQUE)
ANALYZED
7
6
7
1
1
18
252
107
1
AL."FROMACCOUNTID"[NUMBER,22], "AL"."TOACCOUNTID"[NUMBER,22]
1
SEL$5DA710D3
1121
9/10/2013 3:30
VIEW
SYS
VW_NSO_1
VW_NSO_1@SEL$5DA710D3
11
VIEW
8
6
7
2
2
18
234
107
1
TOACCOUNTID[NUMBER,22]
1
SEL$683B0107
1121
9/10/2013 3:30
CONNECT BY
NO FILTERING WITH START-WITH
9
8
8
1
TOACCOUNTID=PRIOR "FROMACCOUNTID"
TOACCOUNTID=56354162
TOACCOUNTID[NUMBER,22], "FROMACCOUNTID"[NUMBER,22], PRIOR NULL[22], LEVEL[4]
SEL$683B0107
1121
9/10/2013 3:30
INDEX
FULL SCAN
SP
ACCOUNTLINK_AK1
ACCOUNTLINK@SEL$3
INDEX (UNIQUE)
ANALYZED
10
9
9
1
1
18
252
107
1
ACCOUNTLINK.ROWID[ROWID,10], "FROMACCOUNTID"[NUMBER,22], "TOACCOUNTID"[NUMBER,22]
1
SEL$3
1121
9/10/2013 3:30
TABLE ACCESS
BY INDEX ROWID
SP
TRXNS
T@SEL$1
2
TABLE
ANALYZED
11
5
6
2
1
1
63
298
1
T."TRXNSID"[NUMBER,22], "T"."TRXNSTYPEID"[NUMBER,22], "T"."DESCRIPTION"[VARCHAR2,80], "T"."POSTDT"[DATE,7], "T"."TRXNSAMT"[NUMBER,22]
1
SEL$5DA710D3
1121
9/10/2013 3:30
INDEX
RANGE SCAN
SP
TRXNS_ACCOUNTID_NIDX
T@SEL$1
INDEX
ANALYZED
2
12
11
7
1
1
1
224
1
AL."FROMACCOUNTID"="T"."ACCOUNTID" AND "T"."POSTDT">=SYSDATE@!-2 AND "T"."POSTDT"<=SYSDATE@!
T.ROWID[ROWID,10], "T"."POSTDT"[DATE,7]
1
SEL$5DA710D3
1121
9/10/2013 3:30
INDEX
UNIQUE SCAN
SP
ACCOUNT_PK
A@SEL$1
INDEX (UNIQUE)
ANALYZED
1
13
4
5
2
1
1
90
1
A."ACCOUNTID"="AL"."FROMACCOUNTID"
A.ROWID[ROWID,10]
1
SEL$5DA710D3
1121
9/10/2013 3:30
TABLE ACCESS
BY INDEX ROWID
SP
ACCOUNT
A@SEL$1
3
TABLE
ANALYZED
14
3
4
2
1
1
29
168
1
A."CLOSEDATE"<SYSDATE@! AND "T"."POSTDT">"A"."CLOSEDATE"
A."ACCOUNTNUM"[VARCHAR2,19]
1
SEL$5DA710D3
1121
9/10/2013 3:30
INDEX
RANGE SCAN
SP
TRXNSTRACK_TRXNSID_NIDX
TRCK@SEL$6
INDEX
ANALYZED
2
15
1
2
2
1
1
10
73
1
TRCK."TRXNSID"=:B1 AND "TRXNSTRACKREASONID"=1
TRCK."TRXNSID"[NUMBER,22], "TRXNSTRACKREASONID"[NUMBER,22]
1
SEL$6
Please help me in debugging this thanks!Hi,
Thanks for your thought on this subject. Below is the trace info that I got from the DBA
SQL ID: d0x879qx2zgtz Plan Hash: 4036333519
SELECT /*+ INDEX(T TRXNS_ACCOUNTID_NIDX) */ AL.FROMACCOUNTID OLDACCOUNTID ,
A.ACCOUNTNUM OLDACCOUNTNUM, T.TRXNSID, T.TRXNSTYPEID, T.DESCRIPTION ,
T.POSTDT, T.TRXNSAMT
FROM
ACCOUNTLINK AL, TRXNS T, ACCOUNT A WHERE AL.TOACCOUNTID IN (SELECT
TOACCOUNTID FROM ACCOUNTLINK START WITH TOACCOUNTID = :B3 CONNECT BY PRIOR
FROMACCOUNTID = TOACCOUNTID) AND AL.FROMACCOUNTID = T.ACCOUNTID AND
A.ACCOUNTID = AL.FROMACCOUNTID AND NOT EXISTS (SELECT 1 FROM TRXNSTRACK
TRCK WHERE TRCK.TRXNSID = T.TRXNSID AND TRXNSTRACKREASONID = :B4 ) AND
T.POSTDT > A.CLOSEDATE AND T.POSTDT >= :B2 AND T.POSTDT <= :B1
call count cpu elapsed disk query current rows
Parse 0 0.00 0.00 0 0 0 0
Execute 17160 2.10 1.87 0 0 0 0
Fetch 17160 7354.61 7390.86 169408 5569856 883366791 0
total 34320 7356.71 7392.74 169408 5569856 883366791 0
Misses in library cache during parse: 0
Optimizer mode: CHOOSE
Parsing user id: 38 (recursive depth: 1)
SQL ID: gs89hpavb4cts Plan Hash: 3415795327
SELECT A.ACCOUNTID, C.MEMBERID, A.PROGRAMID, A.ACCOUNTNUM
FROM
CUSTOMER C, CUSTOMERACCOUNT CA, ACCOUNT A, PROGRAMPARAMVALUE PPV,
BATCHPROCESSPROGRAM BP WHERE A.PROGRAMID = BP.PROGRAMID AND A.PROGRAMID =
PPV.PROGRAMID AND A.ACCOUNTID = CA.ACCOUNTID AND CA.PERSONID = C.PERSONID
AND PPV.PARAMID = :B2 AND PPV.VALUE = 'Y' AND BP.PROCESSID = :B1 AND BP.RUN
= 'Y' AND A.ACCOUNTTYPEID = 4 AND A.ACCOUNTSTATUSID = 1 AND C.MEMBERID IS
NOT NULL
call count cpu elapsed disk query current rows
Parse 0 0.00 0.00 0 0 0 0
Execute 0 0.00 0.00 0 0 0 0
Fetch 172 13.14 115.34 80826 278650 0 17200
total 172 13.14 115.34 80826 278650 0 17200
Misses in library cache during parse: 0
Parsing user id: 38 (recursive depth: 1)
OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS
call count cpu elapsed disk query current rows
Parse 0 0.00 0.00 0 0 0 0
Execute 0 0.00 0.00 0 0 0 0
Fetch 0 0.00 0.00 0 0 0 0
total 0 0.00 0.00 0 0 0 0
Misses in library cache during parse: 0
OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS
call count cpu elapsed disk query current rows
Parse 0 0.00 0.00 0 0 0 0
Execute 17160 2.10 1.87 0 0 0 0
Fetch 17332 7367.75 7506.21 250234 5848506 883366791 17200
total 34492 7369.85 7508.09 250234 5848506 883366791 17200
Misses in library cache during parse: 0
2 user SQL statements in session.
0 internal SQL statements in session.
2 SQL statements in session.
Trace file: svoprod_ora_12346.trc
Trace file compatibility: 11.1.0.7
Sort options: default
1 session in tracefile.
2 user SQL statements in trace file.
0 internal SQL statements in trace file.
2 SQL statements in trace file.
2 unique SQL statements in trace file.
66499 lines in trace file.
7516 elapsed seconds in trace file. -
Using START WITH and CONNECT BY PRIOR in a report
Hi - I am using Oracle 9i and reports 10g. Does anyone know of a reason why running my sql in TOAD will produce the correct results, but the report is dropping records? I am using start with and connect by prior to build a hierarchy of linked records. The report is dropping the "child records" and only returning records from the route level.
Thanks you for your help.Hi user574499
Could u pls share us ur Query...?
Regards,
Abdetu... -
'CONNECT BY PRIOR..START WITH' clause Usage
Hi All,
Could you please let me know the usage of 'connect by prior...start with' clause.
I only know that it helps for hierarchial retrival,but not aware of details.
Can someone provide the use with example for the same.
On searching on the net,I have seen numerous examples but none of them could explain it properly and everywhere the same SCOTT/TIGER schemas EMP and MGR table's example is given which is not enough explanatory.
Thanks in advance...
Aashish S.suppose u need to get all employees in a company in a hirerchical manner
ie presdent then mgrs
then employeess reporting to them
this can be done using connect by prior and start by
select empname from emp
connect by prior empno=mgrno
start with mgrno is null -
Join two Connect By Prior Start With trees and return only common records?
Oracle 10g Release 2 (10.2)
I have two tables that have tree structured data. The results, when running the queries individually are correct, however I need to join tree one to tree two in order to obtain only the common records between them.
-- Tree one
SELECT ip_entity_name, entity_code, hier_level, entity_parent
FROM ip_hierarchy
WHERE hier_level >= 3
CONNECT BY PRIOR entity_code = entity_parent
START WITH entity_code = 'MEWWD';
-- Tree two
SELECT ip_entity_name, entity_code, hier_level, entity_parent
FROM ipt_hierarchy
WHERE hier_level >= 3
CONNECT BY PRIOR entity_code = entity_parent
START WITH entity_code = 'IPNAM';
As I understand, joins may not work with CONNECT BY/START WITH queries?
Is a WITH clause an option?
If at all possible, I don't want to put one select in a View database object and join against the other query.
Thanks.Hi JTP51,
You can use WITH clause or sub-query by using in-line view, without creating any view object in database.
for example
SELECT A.IP_ENTITY_NAME, A.ENTITY_CODE, ....
FROM (SELECT IP_ENTITY_NAME, ENTITY_CODE, HIER_LEVEL, ENTITY_PARENT
FROM IP_HIERARCHY
WHERE HIER_LEVEL >= 3
CONNECT BY PRIOR ENTITY_CODE = ENTITY_PARENT
START WITH ENTITY_CODE = 'MEWWD') A,
(SELECT IP_ENTITY_NAME, ENTITY_CODE, HIER_LEVEL, ENTITY_PARENT
FROM IPT_HIERARCHY
WHERE HIER_LEVEL >= 3
CONNECT BY PRIOR ENTITY_CODE = ENTITY_PARENT
START WITH ENTITY_CODE = 'IPNAM') B
WHERE A. ENTITY_CODE = B. ENTITY_CODE
AND ....Best regards,
Zhxiang
Edited by: zhxiangxie on Feb 2, 2010 5:35 PM -
Slow connect by prior ... start with subquery in 9i
Has anyone come across a performance problem (compared to 8i) when using hierarchical queries where the START WITH list is generated by a subquery? The culprit seems to be an extra visit to the subquery block as part of the CONNECT BY WITH FILTERING operation.
For example, take a simple tree structure:
CREATE TABLE tree
id NUMBER,
parentid NUMBER
CONSTRAINT tree_pk PRIMARY KEY (id)
...and a subquery - here just a table called sample with a subset of the ids from the tree table:
CREATE TABLE sample
id NUMBER,
CONSTRAINT sample_pk PRIMARY KEY (id)
...with which to drive the start points of the treewalk:
SELECT parentid, id, label
FROM tree
CONNECT BY PRIOR parentid = id
START WITH id IN
SELECT id FROM SAMPLE
With the tables populated and analyzed, I get this from 8i:
Execution Plan
.0......SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=19)
.1....0...CONNECT BY
.2....1.....NESTED LOOPS (Cost=1 Card=1280 Bytes=10240)
.3....2.......INDEX (FAST FULL SCAN) OF 'ID_PK' (UNIQUE) (Cost=1 Card=1280 Bytes=5120)
.4....2.......INDEX (UNIQUE SCAN) OF 'TREE_PK' (UNIQUE)
.5....1.....TABLE ACCESS (BY USER ROWID) OF 'TREE'
.6....1.....TABLE ACCESS (BY INDEX ROWID) OF 'TREE' (Cost=2 Card=1 Bytes=19)
.7....6.......INDEX (UNIQUE SCAN) OF 'TREE_PK' (UNIQUE) (Cost=1 Card=1)
Statistics
.....0..recursive calls
.....4..db block gets
.15687..consistent gets
....59..physical reads
.....0..redo size
223313..bytes sent via SQL*Net to client
.38276..bytes received via SQL*Net from client
...343..SQL*Net roundtrips to/from client
.....3..sorts (memory)
.....0..sorts (disk)
..5120..rows processed
and this is 9i:
Execution Plan
.0......SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=19)
.1....0...CONNECT BY (WITH FILTERING)
.2....1.....NESTED LOOPS
.3....2.......NESTED LOOPS (Cost=2 Card=1280 Bytes=10240)
.4....3.........INDEX (FAST FULL SCAN) OF 'ID_PK' (UNIQUE) (Cost=2 Card=1280 Bytes=5120)
.5....3.........INDEX (UNIQUE SCAN) OF 'TREE_PK' (UNIQUE)
.6....2.......TABLE ACCESS (BY USER ROWID) OF 'TREE'
.7....1.....NESTED LOOPS
.8....7.......BUFFER (SORT)
.9....8.........CONNECT BY PUMP
10....7.......TABLE ACCESS (BY INDEX ROWID) OF 'TREE' (Cost=2 Card=1 Bytes=19)
11...10.........INDEX (UNIQUE SCAN) OF 'TREE_PK' (UNIQUE) (Cost=1 Card=20480)
12....1.....INDEX (UNIQUE SCAN) OF 'SAMPLE_PK' (UNIQUE) (Cost=1 Card=1 Bytes=4)
Statistics
.....1..recursive calls
.....1..db block gets
.20525..consistent gets
....72..physical reads
...120..redo size
224681..bytes sent via SQL*Net to client
.38281..bytes received via SQL*Net from client
...343..SQL*Net roundtrips to/from client
.....9..sorts (memory)
.....0..sorts (disk)
..5120..rows processed
..so, about another 5000 logical reads, corresponding to the extra access of the sample table at the bottom of the query plan. So instead of just visiting the START WITH subquery once, to kick off the treewalk, I seem to be revisiting it for every row returned. Not too bad if that happens to be a unique index scan as here but that's not always the case.
I know I've got new options for re-writing this as a join under 9i, I'm just curious about those extra lookups and why they're necessary.
Cheers - AndrewThere is undocumented parameter in Oracle 9i "_old_connect_by_enabled"
which controls the behavoiur of hierarchy queries in 9i and above:
You can try to return to 8i behaviour using it:
SQL> SELECT parentid, id
2 FROM tree
3 CONNECT BY PRIOR parentid = id
4 START WITH id IN
5 (
6 SELECT id FROM SAMPLE
7 )
8 /
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=1 Card=1 Bytes=26)
1 0 CONNECT BY (WITH FILTERING)
2 1 TABLE ACCESS (BY INDEX ROWID) OF 'TREE' (TABLE)
3 2 NESTED LOOPS (Cost=2 Card=1 Bytes=26)
4 3 TABLE ACCESS (FULL) OF 'SAMPLE' (TABLE) (Cost=2 Card
=1 Bytes=13)
5 3 INDEX (UNIQUE SCAN) OF 'TREE_PK' (INDEX (UNIQUE)) (C
ost=0 Card=1 Bytes=13)
6 1 NESTED LOOPS
7 6 BUFFER (SORT)
8 7 CONNECT BY PUMP
9 6 TABLE ACCESS (BY INDEX ROWID) OF 'TREE' (TABLE) (Cost=
1 Card=1 Bytes=26)
10 9 INDEX (UNIQUE SCAN) OF 'TREE_PK' (INDEX (UNIQUE)) (C
ost=1 Card=1)
11 1 TABLE ACCESS (FULL) OF 'TREE' (TABLE) (Cost=1 Card=1 Byt
es=26)
12 1 INDEX (UNIQUE SCAN) OF 'SAMPLE_PK' (INDEX (UNIQUE)) (Cos
t=1 Card=1 Bytes=13)
SQL> alter session set "_old_connect_by_enabled" = TRUE;
Session altered.
SQL> SELECT parentid, id
2 FROM tree
3 CONNECT BY PRIOR parentid = id
4 START WITH id IN
5 (
6 SELECT id FROM SAMPLE
7 )
8 /
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=1 Card=1 Bytes=26)
1 0 CONNECT BY
2 1 NESTED LOOPS (Cost=2 Card=1 Bytes=26)
3 2 TABLE ACCESS (FULL) OF 'SAMPLE' (TABLE) (Cost=2 Card=1
Bytes=13)
4 2 INDEX (UNIQUE SCAN) OF 'TREE_PK' (INDEX (UNIQUE)) (Cos
t=0 Card=1 Bytes=13)
5 1 TABLE ACCESS (BY USER ROWID) OF 'TREE' (TABLE)
6 1 TABLE ACCESS (BY INDEX ROWID) OF 'TREE' (TABLE) (Cost=1
Card=1 Bytes=26)
7 6 INDEX (UNIQUE SCAN) OF 'TREE_PK' (INDEX (UNIQUE)) (Cos
t=1 Card=1)
Rgds. -
Can anyone please help with a Connect by PRIOR sql?
Hi,
I am not so well versed in Oracle analytic functions and am having trouble with a sql.
Oracle Version: 9.2.0.1.0
I have a tree table storing heirarchies upto 4 levels.
create table FAMILY_TREE
TREE_ID NUMBER not null,
PARENT_ID NUMBER,
NAME VARCHAR2(100)
I know that using a CONNECT BY PRIOR, I can find out who the previous ancestar is.
select
f.tree_id TreeId,
f.parent_id ParentId,
f.Name Name
from family_tree f
connect by
prior f.tree_id = f.parent_id
start with f.parent_id is null
I need to display all the ancestars in a single line (in different columns), which is where I am stumped.
I need the output, in different columns as :
Joe Jack Harry Rick Ned
Bascially, I need to display in a single row, all the ancestars.
I tried to use joins but it was very very slow due to the fact that I had to perform multiple joins.
Can anyone please point me in the right direction?
Data:
insert into FAMILY_TREE (TREE_ID, PARENT_ID, NAME)
values (1, null, 'Joe');
insert into FAMILY_TREE (TREE_ID, PARENT_ID, NAME)
values (2, 1, 'Jack');
insert into FAMILY_TREE (TREE_ID, PARENT_ID, NAME)
values (3, 2, 'Harry');
insert into FAMILY_TREE (TREE_ID, PARENT_ID, NAME)
values (4, 3, 'Rick');
insert into FAMILY_TREE (TREE_ID, PARENT_ID, NAME)
values (5, 4, 'Ned');This may give you a start
SQL> ed
Wrote file afiedt.buf
1 select f.tree_id TreeId,f.parent_id ParentId,
2 trim(',' from sys_connect_by_path(f.Name,',')) name
3 from family_tree f
4 connect by
5 prior f.tree_id = f.parent_id
6* start with f.parent_id is null
SQL> /
TREEID PARENTID NAME
1 Joe
2 1 Joe,Jack
3 2 Joe,Jack,Harry
4 3 Joe,Jack,Harry,Rick
5 4 Joe,Jack,Harry,Rick,Ned
Message was edited by:
jeneesh
Forgot to refresh.. -
Hi,
I have this sample table :
create table testcon
(parentid number,
childid number,
cond varchar2(1));
insert into testcon values(1,2,'A');
insert into testcon values(1,2,'B');
insert into testcon values(2,3,'A');
insert into testcon values(2,3,'B');
insert into testcon values(3,4,'A');
insert into testcon values(3,5,'B');
select * from testcon;
PARENTID CHILDID C
1 2 A
1 2 B
2 3 A
2 3 B
3 4 A
3 5 B
What I want to achieve is to have output like this :
PARENTID CHILDID C
1 2 A
2 3 A
3 4 A
1 2 B
2 3 B
3 5 B
meaning it will connect only if the cond is the same (for example A with A, B with B)
I use query like below :
select parentid,childid,cond
from testcon
start with parentid||cond = '1A'
connect by prior childid||cond = parentid||cond;
but i am having extra rows like this
PARENTID CHILDID C
1 2 A
2 3 A
3 4 A
3 5 B
2 3 B
3 4 A
3 5 B
Any suggestions or maybe a better query ?
Thanks in advanceSQL> select *
2 from testcon
3 start with parentid = 1
4 connect by prior childid = parentid and prior cond = cond
5 order siblings by cond;
PARENTID CHILDID C
1 2 A
2 3 A
3 4 A
1 2 B
2 3 B
3 5 B
6 rows selected. -
Connect by prior problem with order by
Hi,
I and using connect by prior within a query on Oracle 8 and would like to order the results within the parent levels.
All the documentation that I have read shows that in Oracle 9i there is an option to say order siblings by which looks like what I need, but this does not work on Oracle 8.
Can anyone tell me how I can order the children within the parents without changing the tree structure?
I have also tried SYS_CONNECT_BY_PATH and I just get an error saying that it is an invalid column name,Karen, see here for a dicussion on how to order the siblings in a pre-9i environment:
http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:9212348049 -
Help with 'connect by prior' statement
I've got a quoting report that is sporadically ordering incorrectly and I've traced the source down to a 'connect by prior' statement. Can I get an explanation of what the statement is doing so I can figure out how to change it to get the desired results?
select rownum config_rownum, quote_line_id,related_quote_line_id rlid, level
from aso_line_relationships
where relationship_type_code = 'CONFIG'
connect by prior related_quote_line_id = quote_line_idsample output for the line_ids for one quote:
CONFIG_ROWNUM QUOTE_LINE_ID RLID LEVEL
1 7438 7439 2
2 7440 7441 2
3 7430 7431 2
4 7432 7433 2
5 7432 7434 2
6 7432 7435 2
7 7436 7437 2
8 7442 7443 2
9 7442 7444 2
10 7442 7445 2
11 7442 7446 2
12 7442 7447 2
13 7442 7448 2
14 7442 7449 2
15 7450 7451 2
16 7452 7453 2
17 7452 7454 2
18 7452 7455 2
19 7456 7457 2
20 7456 7458 2
21 7456 7459 2
22 7460 7461 2
23 7460 7462 2
24 7463 7464 2
25 7430 7431 1
26 7432 7433 1
27 7432 7434 1
28 7432 7435 1
29 7436 7437 1
30 7438 7439 1
31 7440 7441 1
32 7442 7443 1
33 7442 7444 1
34 7442 7445 1
35 7442 7446 1
36 7442 7447 1
37 7442 7448 1
38 7442 7449 1
39 7450 7451 1
40 7452 7453 1
41 7452 7454 1
42 7452 7455 1
43 7456 7457 1
44 7456 7458 1
45 7456 7459 1
46 7460 7461 1
47 7460 7462 1
48 7463 7464 1 The correct ordering can be seen by running this statement:
select rownum config_rownum, quote_line_id,related_quote_line_id rlid
from aso_line_relationships
where relationship_type_code = 'CONFIG' and quote_line_id between 7430 and 7464
CONFIG_ROWNUM QUOTE_LINE_ID RLID
1 7430 7431
2 7432 7433
3 7432 7434
4 7432 7435
5 7436 7437
6 7438 7439
7 7440 7441
8 7442 7443
9 7442 7444
10 7442 7445
11 7442 7446
12 7442 7447
13 7442 7448
14 7442 7449
15 7450 7451
16 7452 7453
17 7452 7454
18 7452 7455
19 7456 7457
20 7456 7458
21 7456 7459
22 7460 7461
23 7460 7462
24 7463 7464I tried to substitute the simple query above for the 'connect by prior' query in the report but failed because something in the report is expecting input from the 'connect by prior' statement. So eliminating the statement is not a choice."connect by prior " is for for hierarchical queries which is for data has parent-children relationship, and ususlly its' result is used to populate tree-like data result. and order by is NOT recommend since it will destroy the hierarchical order.
you could use "order by sibiling" if you would like to order inside the same level of data
Maybe you are looking for
-
I received this update from Apple, but I have no record of altering anything, in particular telephone numbers, can anyone tell me what has been updated, who updated it, and how was entry made to my account? Hello, The following information for your
-
How do I retain Jbutton text for action event & set img icon on Jbutton
I need to retain the text on my button as I use this to identify the action event in actionPerformed method when the button is pressed. Hoe do I add an image icon to the Jbutton without effecting this? [/b] class CalcPanel extends JPanel implemen
-
2tb internal drive macbook pro?
I have a macbook pro 13 inch from spring 2010 Will this be able to handle a 2tb internal hard drive? it is 3.5" also can i put in 16gb of ram? And is it possible to buy an extended battery like 12 cell or something, so my mac has double the life? Tha
-
[SOLVED] Gimp does not honor KDE Task Manager Settings
I've configured the Task Manager to only display tasks from the current desktop. This works for every application except the Gimp, whose toolbox windows are shown in the task manager on every desktop. Is there some setting I could adjust to fix this?
-
Validation "is a valid date" does not pass but Apex proceeding anyway
Hi, I have a simple date-item which I validate first against "not null", then for "is a valid date", then that the date is not in the future. In debug mode the first validation passes, the second ( is a valid date ) does not pass, but the validation