Connect By Prior Help
Hi all,
In Connect By Prior, How can i avoid, if some parent dont have child means, it should not come in the results.......
Regards,
Fame
A "parent" without a child will not show up as a parent, but it may show up as a child.
When that happens, CONNECT_BY_ISLEAF will be 1.
So, filter on CONNECT_BY_ISLEAF = 0.select empno, ename, job, mgr from scott.emp
where connect_by_isleaf = 0
start with mgr is null
connect by mgr = prior empno;
EMPNO ENAME JOB MGR
7839 KING PRESIDENT
7566 JONES MANAGER 7839
7788 SCOTT ANALYST 7566
7902 FORD ANALYST 7566
7698 BLAKE MANAGER 7839
7782 CLARK MANAGER 7839Edited by: Stew Ashton on Jan 17, 2013 1:34 PM
Similar Messages
-
Help me in understand connect By Prior
Hi ,
please help me in understand connect By Prior
I did a sample example , but unale to follow , please explain
How did it understand that KING shuld be displaed first .
On wht basis the results are shown here ?
SELECT empno,
ename,
job,
mgr,
hiredate,
level
FROM emp
START WITH mgr IS NULL
CONNECT BY PRIOR empno = mgr
7839 KING PRESIDENT 17-Nov-81 1
7566 JONES MANAGER 7839 2-Apr-81 2
7788 SCOTT ANALYST 7566 19-Apr-87 3
7876 ADAMS CLERK 7788 23-May-87 4
7902 FORD ANALYST 7566 3-Dec-81 3
7369 SMITH CLERK 7902 17-Dec-80 4
7698 BLAKE MANAGER 7839 1-May-81 2
7499 ALLEN SALESMAN 7698 20-Feb-81 3
7521 WARD SALESMAN 7698 22-Feb-81 3
7654 MARTIN SALESMAN 7698 28-Sep-81 3
7844 TURNER SALESMAN 7698 8-Sep-81 3
7900 JAMES CLERK 7698 3-Dec-81 3
7782 CLARK MANAGER 7839 9-Jun-81 2
7934 MILLER CLERK 7782 23-Jan-82 3Hi,
user10503747 wrote:
Hi ,
please help me in understand connect By Prior
I did a sample example , but unale to follow , please explain
How did it understand that KING shuld be displaed first .
On wht basis the results are shown here ?In a CONNECT BY query (without an ORDER BY clause), if x is an ancestor of y (that is, if x is the parent of y, or x is the parent of the parent of y, and so on), then y will be displayed after x, but before any other row that does not have x as its ancestor. (That is also the order in which ROWNUM will be assigned. This applies only to the query in which CONNECT BY is done. If you use the results set as a sub-query, the super query may cause the results to be re-arranged. An ORDER BY clause always takes precedence.)
In a CONNECT BY query, every row in the result set either:
(a) satisfies the START WITH condition, or
(b) is a descendant of some row in (a); that is, some row that satifies the START WITH condition is its ancestor.
Since ancestors always come before their descendants, the first row in the result set must be a row that satisfied the START WITH condition. In your example, the row with ename='KING' was the only row that satisfied the condition "START WITH mgr IS NULL", and all the other rows are its descendants, so the row with ename='KING' must come first, as APC said. -
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. -
Hi all,
Im having two tables like as following,
menu_roles : Table Name
MENU_ID ROLE REGION SEGMENT PERSON_ID ENABLED_FLAG
13 - - - 31766 Y
27 Account Manager - - - Y
29 Account Manager - - -
1 Account Manager - - - Y
2 Account Manager SOUTH - - Y
2 COO EAST Corporate 31547 Y
12 - - ' 31766 Y
Menu_master : Table Name
MENU_ID MENU_NAME MENU_PARENT_ID RANK MENU_LEVEL ENABLED_FLAG DELETE_FLAG
1 T_Folder1 1 0 Y N
2 T_Folder2 2 0 Y N
3 T_SubItem1.1 1 2 1 Y N
4 T_SubItem1.2 1 1 1 Y N
5 T_SubItem1.1.1 3 2 Y N
6 T_SubItem1.1.2 3 1 2 Y N
7 T_SubItem1.1.1.1 5 1 3 Y Y
7 T_SubItem2.1 2 1 Y N
i need the result like the following
MENU_ID MENU_NAME MENU_PARENT_ID RANK MENU_LEVEL
1 T_Folder1 1 0
4 T_SubItem1.2 1 1 1
8 T_SubItem1.2.1 4 2 2
3 T_SubItem1.1 1 2 1
6 T_SubItem1.1.2 3 1 2
5 T_SubItem1.1.1 3 2
Im using the query like as
pergrpid := 791.
WITH T AS
(select mas.menu_id,
mas.menu_name,
mas.url,
mas.apex_page_no,
mas.menu_parent_id,
mas.rank,
mas.menu_level
from sfa_menu_master mas
where mas.enabled_flag = 'Y'
and nvl(mas.delete_flag, 'N') = 'N'
connect by prior mas.menu_id = mas.menu_parent_id
start with menu_id in
(select menu_id
from sfa_menu_roles rol
where (rol.role in
(select max(role)
from sfa_per_groups grp
where grp.per_group_id = pergrpid) or
rol.region in
(select region
from sfa_per_groups grp
where grp.per_group_id = pergrpid) or rol.region = null or
rol.person_id = null))
order siblings by rank)
select * from t;
but i want to check role , region ,segment , personid,
if i give role,region, segment mean i want to check three conditions
if i give only role and region mean want to check that 2 conditions only
if i give role alone mean i have to check 1 conditions alone
if i give personid mean i have to check personid
awaiting for the reply.
Thanks in Advance,
839083 .A "parent" without a child will not show up as a parent, but it may show up as a child.
When that happens, CONNECT_BY_ISLEAF will be 1.
So, filter on CONNECT_BY_ISLEAF = 0.select empno, ename, job, mgr from scott.emp
where connect_by_isleaf = 0
start with mgr is null
connect by mgr = prior empno;
EMPNO ENAME JOB MGR
7839 KING PRESIDENT
7566 JONES MANAGER 7839
7788 SCOTT ANALYST 7566
7902 FORD ANALYST 7566
7698 BLAKE MANAGER 7839
7782 CLARK MANAGER 7839Edited by: Stew Ashton on Jan 17, 2013 1:34 PM -
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.. -
Help regarding CONNECT BY PRIOR AND LEVEL
Hi
I need to retreive all the employees that are at the least level under a particular employee. Assume basic emp table.
I have been using CONNECT BY PRIOR
select mgr,empno,ename, level from emp
start with empno=7839
connect by prior empno=mgrOUTPUT:
MGR EMPNO ENAME LEVEL
7839 KING 1
7839 7566 JONES 2
7566 7788 SCOTT 3
7788 7876 ADAMS 4
7566 7902 FORD 3
7902 7369 SMITH 4
7839 7698 BLAKE 2
7698 7499 ALLEN 3
7698 7521 WARD 3
7698 7654 MARTIN 3
7698 7844 TURNER 3
7698 7900 JAMES 3
7839 7782 CLARK 2
7782 7934 MILLER 3The Output i require should be
MGR EMPNO ENAME LEVEL
7788 7876 ADAMS 4
7902 7369 SMITH 4
7698 7499 ALLEN 3
7698 7521 WARD 3
7698 7654 MARTIN 3
7698 7844 TURNER 3
7698 7900 JAMES 3
7782 7934 MILLER 3Also if i change the empno then i need to get only the employees under that employee only.
Thanks in Advance,
G.Vamsi Krishnaoracle_for_dude wrote:
can you explain what is this query doing
SQL> select mgr,empno,ename, level
2 from emp
3 where level > 2
4 start with empno = 7566
5 connect by prior empno = mgr;
MGR EMPNO ENAME LEVEL
7788 7876 ADAMS 3
7902 7369 SMITH 37566 in scott.emp table is employee_id of JONES, It's displaying the selected columns for all the employees in the sub-tree starting with Jones, not including Jones (who is at LEVEL=1) and the people who report directly to Jones (LEVEL=2).
>
>
Thanks in advance -
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 -
Need help: CONNECT by PRIOR query in ora10g
Hi All,
Recently we have migrated the DB from 9i to 10g.
we used to have many rule based queries in 9i
and all are changed based on 10g.
All the queries that have CONNECT BY PRIOR
conditions seems to behave in wrong manner.
( same query in 9i,fetches less number of records where as 10g there were many
rows displayed eventhough it was not selected in where clause.- as per business logic )
After changing the below parameter setting , the problem was solved.
can you tell wts the below setting would do? by doing so , ur again making 10g
to behave like 9i so that full benefit of 10g is not used ?
Alter session set “_optimizer_connect_by_cost_based”=falseHi, try this:
WITH t AS (
SELECT 100 AS node_id, cast(NULL AS NUMBER) AS parent_node_id FROM dual UNION ALL
SELECT 101 AS node_id, 100 AS parent_node_id FROM dual UNION ALL
SELECT 102 AS node_id, 100 AS parent_node_id FROM dual UNION ALL
SELECT 1021 AS node_id, 102 AS parent_node_id FROM dual UNION ALL
SELECT 1022 AS node_id, 102 AS parent_node_id FROM dual UNION ALL
SELECT 10221 AS node_id, 1022 AS parent_node_id FROM dual UNION ALL
SELECT 10222 AS node_id, 1022 AS parent_node_id FROM dual
SELECT DISTINCT CONNECT_BY_ROOT parent_node_id AS ID, node_id AS id1
FROM t
CONNECT BY t.node_id = PRIOR parent_node_id
START WITH parent_node_id = 1022
ORDER BY 1, 2; -
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. -
Connect by Prior in Oracle 8i question
Need help with building tree-structured query in Oracle 8i (Forms 6i front end if it matters).
Sample structure and data:
CREATE TABLE table_list
my_code NUMBER,
my_level NUMBER,
my_description VARCHAR2(60)
CREATE TABLE table_content
my_code NUMBER,
term_code NUMBER,
term_category VARCHAR2(5)
INSERT into table_list values (101, 1, 'building');
INSERT into table_list values (102, 2, 'flat');
INSERT into table_list values (103, 3, 'living room');
INSERT into table_list values (104, 3, 'bedroom');
INSERT into table_list values (105, 3, 'bathroom');
commit;
INSERT into table_content values (101, 102, 'Sub');
INSERT into table_content values (102, 103, 'Sub');
INSERT into table_content values (102, 104, 'Sub');
INSERT into table_content values (102, 105, 'Sub');
commit;
Need to display data in the following order:
101 'building' --level one
102 'flat' --level two
105 'bathroom' --level three
104 'bedroom' --level three
103 'living room' --level three
*(note alphabetical order in level three)*
Looks like Oracle 8i does not support table joins for CONNECT BY PRIOR. Please advise!And you are correct, it does not display level 1. Do
you think this is this a database structure problem
or it could be corrected via query modification?No and yes. It's not a "structure" problem, its a data problem (as I explained above). You always fix data problems by making queries overly complex (hint, hint, I don't suggest doing it this way).
select tl.my_level, tl.my_description , LPAD(' ',3*(my_level-1)) || substr(tl.my_description, 1, 30) description
from table_list tl,
(select term_Code from table_content
start with my_code in (select my_code from table_list where my_level=1)
connect by prior term_code = my_code
) x
where x.term_code = tl.my_code
union all
select my_level, my_description
from table_list
where my_level = 1
order by 1, 2
better to add a row to table_content with my_code=null, term_code=101.
otherwise you'll be pulling stupid nonsense like above with every query from here on out. -
How to use simple SQL instead of Connect By Prior
Currently, I am using "connect by prior" query in the application, but for reason I don�t want to use this connect by query so can any one please tell how does I get the same result by using SQL, I tried this by using procedure but unable to get the same result, specially LEVEL of the tree.
So please tell, how would I get the correct data.
Thanks in advance,
AMIT.Hi,
Whenever you have a question, it helps to post:
(1) The version of Oracle (and any other relevant software) you're using
(2) A little sample data (just enough to show what the problem is) from all the relevant tables
(3) The results you want from that data
(4) Your best attempt so far (formatted) I don't believe the unformated code you posted is what you're really running, since it has a syntax error ("... WHERE START WITH ..."). Please post code that really works with the sample data you posrted.
(5) The full error message (if any), including line number
Executable SQL statements (like "CREATE TABLE AS ..." or "INSERT ..." statements) are best for (2).
If you can present your problem using commonly available tables (for example, scott.emp, which contains a hierarchy), then you can omit (2).
Formatted tabular output is okay for (3). Type these 6 characters
{code}
(small letters only, inside curly brackets) before and after the tabular text, to preserve spacing.
As Alex said, why don't you want to use CONNECT BY?
Are you getting the correct results now, but just looking for a different way of getting them?
Depending on your exact requirements, you could write a PLSQL function that mimics LEVEL. Don't expect it to be fast.
Nested Sets is a completely different way of modeling trees.
Some things are much easier with Nested Sets than they are using the Adjacency Model (the parent-child model that uses CONNECT BY).
But some things are much harder with Nested Sets, and LEVEL is one of them. -
How to get the root node for a child using connect by prior
Hi,
I searched at many places to do this but not able to get the exact o/p sp posting my question here.
I have a table with parent_id and child_id columns. The levels of this parent_child can be many but my aim is to find the ultimate parent ( dont know the right term)
like if I have a child_node= xyz and its parent= pqr and its parents=lmn which might be ultimate parent which doesnt have any further parent.
So if i start with child_code= "xyz" then i should get the parent as "lmn" and not the immediate parent "pqr".
Please help.
Thanks,
AashishTo find the Parent on emp table:
select empno,ename,level
from emp
where level=3
start with empno=7934
connect by empno= prior mgrTo find the Child on emp table:
select empno,ename,level
from emp
where level=1
start with empno=7934
connect by prior empno= mgrBut you need to know the level or position to specify the level of parent or child. But there are some other possible options available if we can know the requirement properly.
Edited by: Vasista on Jan 11, 2011 2:27 AM
Edited by: Vasista on Jan 11, 2011 2:30 AM -
Query tuning for query using connect by prior
I have written following query to fetch the data. The query is written in this format because there are multiple rows, which make one recrd and we need to bring that record into one row.
For one CAT(commented here), this query takes around 4 minutes and fetches 6900 records but when it runs for 3 CAT, it takes 17 mins.
I want to tune this as this has to run for 350 CAT values.
It is doing FTS on the main table. I tried to use different hints like PARALLEL, APPEND (in insert) but nothing worked.
The cost of the query is 51.
Any help/suggestions will be appreciated.
SELECT DISTINCT MIN(SEQ) SEQ,
PT, APP, IT, LR, QT,CD, M_A_FLAG,
STRAGG(REM) REM, -- aggregates the data from different columns to one which is parent
CAT
FROM (WITH R AS (SELECT CAT, SEQ, PT, M_A_FLAG, IT, LR,QT,CD, REM, APP
FROM table1
WHERE REC = '36' AND M_A_FLAG = '1'
--AND CAT = '11113')
SELECT CAT, SEQ,
CONNECT_BY_ROOT PT AS PT,
CONNECT_BY_ROOT APP AS APPL,
M_A_FLAG,
CONNECT_BY_ROOT IT AS IT,
CONNECT_BY_ROOT LR AS LR,
CONNECT_BY_ROOT QT AS QT,
CONNECT_BY_ROOT CD AS CD,
REM
FROM R A
START WITH PT IS NOT NULL
CONNECT BY PRIOR SEQ + 1 = SEQ
AND PRIOR CAT = CAT
AND PT IS NULL)
GROUP BY PT, APP, IT,LR, QT, CD, M_A_FLAG, CAT
ORDER BY SEQ;
Thanks.
Edited by: user2544469 on Feb 11, 2011 1:12 AMThe following threads detail the approach and information required.
Please gather relevant info and post back.
How to post a SQL tuning request - HOW TO: Post a SQL statement tuning request - template posting
When your query takes too long - When your query takes too long ... -
Oracle Virtual Machine Connect by Prior Problem
Hi Friends ,
In my company we have virtualization. this is new in our organization.
oracle image have been taken and put in to virtual machine.(vmware).
after that, procedure which has "connect by prior" start to create wrong output.
please help us on what to do to correct problem given above.
(my oracle version 10g)JAVAMAN2 wrote:
simple sample:
SELECT ROWNUM+2000-1 AS YIL FROM DUAL CONNECT BY ROWNUM <= 2010-2005 ORDER BY ROWNUM+2000-1 DESC;output on virtual machine
2009,2008,2007,2006,2005
output on real world
2010,2009,2008,2007,2006,2005If that is really the output from that query, then both your virtual machine and your "real world" are broken. On all 4 versions of Oracle I have available, I get:
SQL> SELECT ROWNUM + 2000 - 1 AS YIL
2 FROM DUAL
3 CONNECT BY ROWNUM <= 2010 - 2005
4 ORDER BY ROWNUM + 2000 - 1 DESC;
YIL
2004
2003
2002
2001
2000Which is what I would expect given that 2010 - 2005 = 5.
Perhaps if you showed an actual cut and paste from a sqlplus session on each machine someone might be able to help, however, I'm inclined to agree with sb92075 that it is a data issue in your real query.
John -
Connect by prior by speific order problem.
Oracle: 10.2.0.4
I have a table containing events backup.
i want to list the hierarchy within that backup oreder by the time_stamp for all levels.
create table BCK_EVENTS
bck_backup_id NUMBER(9) default 0 not null,
event_id NUMBER(15) default 0 not null,
event_name NVARCHAR2(100) default ' ' not null
time_stamp NUMBER(9) default 0 not null,
parent_event NUMBER(15) default 0 not null,
add constraint BCK_EVENTS_PK primary key (BCK_EVENT_ID, EVENT_ID); // event_id is not unique can be under one or more backup id'sthis is not a tree with one root, there is more than one event in the root level (level 1).
example:
Event Name Time level <- time is numeric but for easier reading.
*Event A 10:00 1
*Event C 10:30 2
*Event B 11:17 3
*Event H 12:10 1
*Event J 12:10 2
*Event M 12:21 2
*Event Z 15:33 1
*Event R 16:56 2
*Event M 16:57 3
*Event G 20:20 2What i tried was :
select lpad( '*', level*2 ) || event_id,event_name,time_stamp,parent_event,level from bck_events
where bck_event_id=100031
start with parent_event is null
connect by prior event_id = parent_eventand there are two problems with it.
1. it's not ordered even when i added an Index (parent_event,time_stamp) and try to hint it.
2. it returns loads of multiple rows.
hope it's clear enough. I thank for any help.
Edited by: 973065 on Nov 25, 2012 8:04 AM
Edited by: 973065 on Nov 25, 2012 9:23 AM
Edited by: 973065 on Nov 25, 2012 9:29 AM
Edited by: 973065 on Nov 25, 2012 9:31 AMHi,
973065 wrote:
Oracle: 10.2
I have a table containing events backup.
i want to list the hierarchy within that backup oreder by the time_stamp for all levels.
create table BCK_EVENTS
bck_backup_id NUMBER(9) default 0 not null,
event_id NUMBER(15) default 0 not null,
event_name NVARCHAR2(100) default ' ' not null
time_stamp NUMBER(9) default 0 not null,
parent_event NUMBER(15) default 0 not null,
Thanks for posting the version number and the CREATE TABLE statement. Don't forget to post INSERT statements for your sample data.
add constraint BCK_EVENTS_PK primary key (BCK_EVENT_ID, EVENT_ID); // event_id is not unique can be under one or more backup id's
this is not a tree with one root, there is more than one event in the root level (level 1).Is it a forest, that is, a set of trees?
example:
Event Name Time level <- time is numeric but for easier reading.
*Event A 10:00 1
*Event C 10:30 2
*Event B 11:17 3
*Event H 12:10 1
*Event J 12:10 2
*Event M 12:21 2
*Event Z 15:33 1
*Event R 16:56 2
*Event M 16:57 3
*Event G 20:20 2
That seems to be a forest, that is, every row has 0 or 1 parent, and no row is its own ancestor.
What i tried was :
select lpad( '*', level*2 ) || event_id,event_name,time_stamp,parent_event,level from bck_events
where bck_event_id=100031
start with parent_event is null
connect by prior event_id = parent_event
and there are two problems with it.
1. it's not ordered even when i added an Index (parent_event,time_stamp) and try to hint it.Depending on your data, you may just need to add
ORDER SIBLINGS BY time_stampat the end, after the CONNECT BY clause.
If you need rows sorted by time_stamp under their roots, but otherwise without regard to the hierarchy, then use CONNECT_BY_ROOT.
2. it returns loads of multiple rows.Again, it depends on your data. I'll bet you need something more in the CONNECT BY clause, but I can't tell what without some sample data and an exxplanation of how you gett the results you posted from that data. The fact that bck_event_id is part of the primary key makes me suspect that maybe bck_event_id needs to be somewhere in the CONNECT BY clause, but that's just a wild guess.
hope it's clear enough. I thank for any help.As mentioned before, see the forum FAQ {message:id=9360002}
Maybe you are looking for
-
INSERT 'PAGE BREAK' AT CHANGE IN EACH TOTAL
Hi... Does anyone know a way of inserting a page break at each change in a total on a report, so when the report is printed it prints each section (i.e. each time it finds a total) on a separate page. I also want to print the report title on each pag
-
HT5622 Can I delete my Apple ID
Cam I delete my Apple ID
-
What is the latest OS this iMac can run?
What is the latest version of system software this iMac can run? My data from About This Mac includes: Model Name: iMac Model Identifier: iMac7,1 Processor Name: Intel Core 2 Duo Processor Speed: 2 GHz Number Of Processors: 1 Total Number Of Cores: 2
-
Supress "might not have been initialized" compiler error
Is this possible?
-
Hi Experts, When User1 tried SNRO for a particular object in ECC.It is showing as "You are not authorised to use this function".But We checked on SU53,It shows as "Last Authorization was successful". Even User2 with same authorization as User1,able t