Connect by prior repeating nodes
I have a hierarchical structure (a directed acyclic graph with a root node) and I want to list only the leaf nodes for a root node. For example, say I have the following graph
A -> B, A->C, A->D
B->C,
C->D,
D->E,
D->F
I want to get the results as A ->E, A->F
Queries to create the above graph
create table test_hierarchy (parent varchar2(10), child varchar2(10));
insert into test_hierarchy (parent, child) values ('a','b');
insert into test_hierarchy (parent, child) values ('a','c');
insert into test_hierarchy (parent, child) values ('a','d');
insert into test_hierarchy (parent, child) values ('b','c');
insert into test_hierarchy (parent, child) values ('b','d');
insert into test_hierarchy (parent, child) values ('c','d');
insert into test_hierarchy (parent, child) values ('d','e');
insert into test_hierarchy (parent, child) values ('d','f');
To achieve the desired output i have written the following query
Select connect_by_root(parent), child from test_hierarchy where connect_by_isleaf = 1
start with parent = 'a'
connect by prior child = parent
But I get the result set with A->E and A->F repeated 6 times from all the possible paths.
This is making the whole operation very slow in the production env. Adding distinct is making it even slower.
Is there a way for me to specify to the db to not traversed the already traversed paths.
Any help is highly appreciated
Hi,
[email protected] wrote:
Thanks ... but as far as I know the recursive with clause is supported only in Oracle 11g. And my client is using Oracle 10g. Also, I have not tried this on oracle 11g instance but I am assuming that the output and query you wrote below is for oracle only.
Also, as per Franks suggestion; I did some reading on pipelined functions. Basically, I tried creating a function (not pipelined) that will take a collection and a string. The function would update the collection to have this string if it is not present already in the collection. And would return true if the collection gets modified, false otherwise.
But now am stuck because i cannot call this function in sql query as it has an out parameter. Functions that have OUT parameters, or that return data types not found in SQL (even BOOLEAN), can only be called from PL/SQL. They can not be used in SQL statements, even SQL statements embedded in PL/SQL.
I suggest a function that takes one IN-argument, a string (such as 'A'), that returns a delimited string (such as 'E,F'). You could split that string into parts after it was returned, if necessary.
So you might call the function in a SQL statment like this:
SELECT get_leaves ('A')
FROM my_table;and it would display
GET_LEAVES
E,FInside get_leaves, you can use whatever other functions yu want, including functions with user-defined arguments and return values. (OUT arguments in functions are not very good programming, but they would be allowed, if you really want them.)
It's only inside get_leaves that you need to keep track of what nodes have been visited already; the calling program doesn't need to see that part of the job at all.
Similar Messages
-
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 -
Oracle SQL - Extracting clob value from XML with repeating nodes
Hi All,
I am attempting to run SQL on a table (called test_xml with a column xml_data [data type xmltype]). The column contains xml with repeating nodes (description). The following statement runs successfully when the node contains data of a non clob size:
SELECT
extractvalue (Value (wl), '*/description')
FROM test_xml
, TABLE (xmlsequence (extract (xml_data, '*/record'))) wl
but fails when description node contains a lot of data:
ORA-01706: user function result value was too large
I amended my query:
SELECT
extractvalue(Value (wl), '*/description').getClobVal()
FROM test_xml
, TABLE (xmlsequence (extract (xml_data, '*/record'))) wl
but this fails with:
ORA-22806: not an object or REF
Thanks in AdvanceHi Greg,
11.2.0.2.0 (Although I will need to do this on my work instance also which is 10.2.0.4)That's gonna be a problem...
Direct CLOB projection is supported starting with 11.2.0.2, using XMLTable or XMLCast/XQuery functions (extractvalue, extract, xmlsequence are deprecated now) :
SELECT x.*
FROM test_xml t
, XMLTable(
'/*/record'
passing t.xml_data
columns
description clob path 'description'
) x
;On prior versions, implicit conversions will occur to VARCHAR2 datatype, hence the limitation observed.
AFAIK you have two options on 10.2.0.4 :
1) Using Object-Relational storage, with the xdb:SQLType="CLOB" annotation.
2) Using the following trick :
SELECT dbms_xmlgen.convert(x.description.getClobVal(), 1) as description
FROM test_xml t
, XMLTable(
'/*/record'
passing t.xml_data
columns
description xmltype path 'description/text()'
) x
; -
Hi, I have following problem
Table TMP contains following data
ID PID
1
2 1
3 2
select t.id
from tmp t
where level=(select max(level)
from tmp t2
connect by prior t2.pid=t2.id
start with t2.id=&ID)
connect by prior t.pid=t.id
start with t.id=&ID
Select returns 1 when ID=3
Is there a way to write select so that it returns value for first node
and value of ID used in select?
Something like
select value_of_ID
,t.id
from tmp t
start with t.id=&ID
so I could use group by on first column (group by value_of_ID)
Following select would not satisfy:
select &ID
,t.id
from tmp t
start with t.id=&IDI would suggest something more straightforward:
select :startnode, id
from tmp
where pid is null
start with id = :startnode
connect by prior pid = id;The WHERE clause will limit the result to just the top level - no need for a subquery or aggregate functions. -
Issue with the below query. The query is not getting filtered for the condition hier_typ_c in('BS') with the connect by prior
query. query is fetching all the hier_type_c in the table like 'BS', 'CO', 'EC' etc....
Just wondering how do i restrict the query just to fetch the type_c ='BS' alone? why is it giving all the records??
Select
Level as LEVEL_CODE,
h.HIER_PRNT_NODE_I as PARENT,
h.HIER_CHLD_NODE_I as CHILD,
h.HIER_CHLD_NODE_X || ' (' || h.HIER_CHLD_NODE_I || ')' as ALIAS
From (Select Distinct HIER_CHLD_NODE_I, HIER_PRNT_NODE_I,
HIER_CHLD_NODE_X from .HIER_DIMN
where hier_typ_c in('BS') and CURR_VER_C = 'Y') h
Start with h.HIER_PRNT_NODE_I = 'ROOT'
Connect by prior
h.HIER_CHLD_NODE_I = h.HIER_PRNT_NODE_I
Order by LEVEL_CODE, parent, childHi
It loks like you're doing it right.
By basing the CONNECT BY query on a sub-query that has this WHERE clasue:
where hier_typ_c in('BS') and CURR_VER_C = 'Y') hyou should exclude not only nodes whose hier_typ_c is not 'BS', but also their descendants.
Post some sample data (CREATE TABLE and INSERT statements) and the results you want from that data.
Are you sure the query you posted is what you're actually running?
I would expect the sub-query FROM clause to cause an error because of the '.'.
from .HIER_DIMNEdited by: Frank Kulash on Sep 29, 2009 11:16 AM -
Connect by prior subquery - performance problem
Hello,
I have some data which is organized in a folder tree. The requeriement is to be able to search from any subfolder and down.
/Documents
___Folder A
______Doc A
______Doc B
___Folder B
______Doc C
______Doc D
The folder structure is defined in a table called CORNERS where the records(=folders) has a ID/PARENTID relationsship to describe the folder structure.
Another table, called MASTER, contains the main content. Each item has a CORNERID value which defined in which subfolder the document is located.
MASTER
ID CORNERID TITLE INDEX_URL
100 2 Doc A http://xxx/yy.com
101 2 Doc B http://xxz/yy.com
102 3 Doc C http://xyz/yy.com
103 3 Doc D http://xyz/zz.com
CORNERS
ID PARENTID NAME
1 Documents
2 1 Folder A
3 1 Folder B
MASTER table has ~50000 records
CORNERS has ~900 records.
Analyzed nighly and stats are fresh.
Indexes defined:
CORNERS_ID_PARENT_IDX corners(id,parentid)
CORNERS_PARENT_ID_IDX corners(parentid,id)
MASTER_ID_CORNERID_IDX master(id,cornerid)
MASTER_CORNERID_ID_IDX master(cornerid,id)
Oracle Text index (URL based) on MASTER.INDEX_URL
Foreign key defined:
MASTER.CORNERID references CORNERS.ID
If I do a search without involving the hierarchy, then the search runs pretty fast:
SQL> SELECT COUNT(*) FROM (SELECT a.id, a.cornerid FROM MASTER a WHERE (CONTAINS(title,'$ADS AND {S} AND $PARAMETER',2) > 1 OR CONTAINS(index_url,'$ADS AND {S} AND $PARAMETER',1) > 1) );
COUNT(*)
5125
Elapsed: 00:00:00.14
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=1354 Card=1 Bytes=15
8)
1 0 SORT (AGGREGATE)
2 1 TABLE ACCESS (BY INDEX ROWID) OF 'MASTER' (Cost=1354 Car
d=758 Bytes=119764)
3 2 BITMAP CONVERSION (TO ROWIDS)
4 3 BITMAP OR
5 4 BITMAP CONVERSION (FROM ROWIDS)
6 5 SORT (ORDER BY)
7 6 DOMAIN INDEX OF 'MASTER_TITLE_IDX' (Cost=470)
8 4 BITMAP CONVERSION (FROM ROWIDS)
9 8 SORT (ORDER BY)
10 9 DOMAIN INDEX OF 'MASTER_IDX' (Cost=650)
Statistics
1462 recursive calls
0 db block gets
5507 consistent gets
347 physical reads
0 redo size
380 bytes sent via SQL*Net to client
503 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
2 sorts (memory)
0 sorts (disk)
1 rows processed
SQL>
BUT, if I add a subquery to limit the search to a certain folder tree (which includes ~200 nodes), then the performance is really badly affected. The subquery itself runs fast - around 0.07 seconds, but together with the rest of the query the preformance is really bad:
SQL> SELECT COUNT(*) FROM (SELECT a.id, a.cornerid FROM MASTER a WHERE (CONTAINS(title,'$ADS AND {S} AND $PARAMETER',2) > 1 OR CONTAINS(index_url,'$ADS AND {S} AND $PARAMETER',1) > 1) AND cornerid IN ( SELECT ID FROM corners START WITH id = 2434 CONNECT BY PRIOR id = parentid) );
COUNT(*)
942
Elapsed: 00:00:01.83
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=118 Card=1 Bytes=175
1 0 SORT (AGGREGATE)
2 1 TABLE ACCESS (BY INDEX ROWID) OF 'MASTER' (Cost=19 Card=
1 Bytes=162)
3 2 NESTED LOOPS (Cost=118 Card=8 Bytes=1400)
4 3 VIEW OF 'VW_NSO_1' (Cost=2 Card=6 Bytes=78)
5 4 SORT (UNIQUE)
6 5 CONNECT BY (WITH FILTERING)
7 6 NESTED LOOPS
8 7 INDEX (UNIQUE SCAN) OF 'SYS_C002969' (UNIQUE
) (Cost=1 Card=1 Bytes=4)
9 7 TABLE ACCESS (BY USER ROWID) OF 'CORNERS'
10 6 NESTED LOOPS
11 10 BUFFER (SORT)
12 11 CONNECT BY PUMP
13 10 INDEX (RANGE SCAN) OF 'CORNERS_PARENT_ID_IDX
' (NON-UNIQUE) (Cost=2 Card=6 Bytes=48)
14 3 INDEX (RANGE SCAN) OF 'MASTER_CORNERID_ID_IDX' (NON-
UNIQUE) (Cost=1 Card=38)
Statistics
29267 recursive calls
0 db block gets
55414 consistent gets
140 physical reads
0 redo size
380 bytes sent via SQL*Net to client
503 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
12 sorts (memory)
0 sorts (disk)
1 rows processed
I've tried an alternative syntax, instead of the IN clause like this:
SELECT COUNT(*) FROM (
WITH folders AS (
SELECT ID
FROM CORNERS
START WITH ID=2434
CONNECT BY PRIOR ID= PARENTID
SELECT a.id
FROM MASTER a, folders b
WHERE a.cornerid = b.id
AND CONTAINS(index_url,'$ADS AND {S} AND $PARAMETER',1) > 1);
It does runfaster, but still takes around 1 second.
Any suggestion on how to make this run faster!?
Thanks in advance!
-Matshow long does it takes to complete the query?
-
CONNECT BY PRIOR USING MULTIPLE TABLES RETURNS AWKAWARD RESULTS.
CREATE TABLE "JAM"."FTVORGN2"
( "FTVORGN_COAS_CODE" VARCHAR2(1 CHAR) NOT NULL ENABLE,
"FTVORGN_ORGN_CODE" VARCHAR2(6 CHAR) NOT NULL ENABLE,
"FTVORGN_EFF_DATE" DATE NOT NULL ENABLE,
"FTVORGN_ACTIVITY_DATE" DATE NOT NULL ENABLE,
"FTVORGN_USER_ID" VARCHAR2(30 CHAR) NOT NULL ENABLE,
"FTVORGN_NCHG_DATE" DATE NOT NULL ENABLE,
"FTVORGN_TERM_DATE" DATE,
"FTVORGN_TITLE" VARCHAR2(35 CHAR) NOT NULL ENABLE,
"FTVORGN_STATUS_IND" VARCHAR2(1 CHAR),
"FTVORGN_ORGN_CODE_PRED" VARCHAR2(6 CHAR),
"FTVORGN_DATA_ENTRY_IND" VARCHAR2(1 CHAR) NOT NULL ENABLE,
"FTVORGN_VPDI_CODE" VARCHAR2(6 CHAR)
I have a table like the above.
I also have a table like the below.
CREATE TABLE "JAM"."FORUSOR"
( "FORUSOR_USER_ID_ENTERED" VARCHAR2(30 CHAR) NOT NULL ENABLE,
"FORUSOR_COAS_CODE" VARCHAR2(1 CHAR) NOT NULL ENABLE,
"FORUSOR_ORGN_CODE" VARCHAR2(6 CHAR) NOT NULL ENABLE,
"FORUSOR_ACCESS_IND" VARCHAR2(1 CHAR) NOT NULL ENABLE,
"FORUSOR_ACTIVITY_DATE" DATE NOT NULL ENABLE,
"FORUSOR_USER_ID" VARCHAR2(30 CHAR) NOT NULL ENABLE,
"FORUSOR_WBUD_ACCESS_IND" VARCHAR2(1 CHAR) NOT NULL ENABLE,
"FORUSOR_SURROGATE_ID" NUMBER(19,0),
"FORUSOR_VERSION" NUMBER(19,0),
"FORUSOR_DATA_ORIGIN" VARCHAR2(30 CHAR),
"FORUSOR_VPDI_CODE" VARCHAR2(6 CHAR)
REM INSERTING into FTVORGN2
SET DEFINE OFF;
Insert into FTVORGN2 (FTVORGN_COAS_CODE,FTVORGN_ORGN_CODE,FTVORGN_EFF_DATE,FTVORGN_ACTIVITY_DATE,FTVORGN_USER_ID,FTVORGN_NCHG_DATE,FTVORGN_TERM_DATE,FTVORGN_TITLE,FTVORGN_STATUS_IND,FTVORGN_ORGN_CODE_PRED,FTVORGN_DATA_ENTRY_IND,FTVORGN_VPDI_CODE) values ('D','01',to_date('01-OCT-88','DD-MON-RR'),to_date('19-MAR-97','DD-MON-RR'),'TRAIN01',to_date('31-DEC-99','DD-MON-RR'),null,'TOTAL UNIVERSITY','A',null,'N',null);
Insert into FTVORGN2 (FTVORGN_COAS_CODE,FTVORGN_ORGN_CODE,FTVORGN_EFF_DATE,FTVORGN_ACTIVITY_DATE,FTVORGN_USER_ID,FTVORGN_NCHG_DATE,FTVORGN_TERM_DATE,FTVORGN_TITLE,FTVORGN_STATUS_IND,FTVORGN_ORGN_CODE_PRED,FTVORGN_DATA_ENTRY_IND,FTVORGN_VPDI_CODE) values ('D','1006',to_date('22-SEP-14','DD-MON-RR'),to_date('22-SEP-14','DD-MON-RR'),'MJ35',to_date('31-DEC-99','DD-MON-RR'),null,'Provost','A','01','N',null);
Insert into FTVORGN2 (FTVORGN_COAS_CODE,FTVORGN_ORGN_CODE,FTVORGN_EFF_DATE,FTVORGN_ACTIVITY_DATE,FTVORGN_USER_ID,FTVORGN_NCHG_DATE,FTVORGN_TERM_DATE,FTVORGN_TITLE,FTVORGN_STATUS_IND,FTVORGN_ORGN_CODE_PRED,FTVORGN_DATA_ENTRY_IND,FTVORGN_VPDI_CODE) values ('D','2027',to_date('01-OCT-97','DD-MON-RR'),to_date('01-OCT-97','DD-MON-RR'),'DARDARMT',to_date('31-DEC-99','DD-MON-RR'),null,'Sch of Biomed Engr, Sci Health Sys','A','1006','N',null);
Insert into FTVORGN2 (FTVORGN_COAS_CODE,FTVORGN_ORGN_CODE,FTVORGN_EFF_DATE,FTVORGN_ACTIVITY_DATE,FTVORGN_USER_ID,FTVORGN_NCHG_DATE,FTVORGN_TERM_DATE,FTVORGN_TITLE,FTVORGN_STATUS_IND,FTVORGN_ORGN_CODE_PRED,FTVORGN_DATA_ENTRY_IND,FTVORGN_VPDI_CODE) values ('D','2028',to_date('28-FEB-01','DD-MON-RR'),to_date('28-FEB-01','DD-MON-RR'),'JMOORE',to_date('31-DEC-99','DD-MON-RR'),null,'Sch of Envr Science, Engr Policy','A','1006','N',null);
Insert into FTVORGN2 (FTVORGN_COAS_CODE,FTVORGN_ORGN_CODE,FTVORGN_EFF_DATE,FTVORGN_ACTIVITY_DATE,FTVORGN_USER_ID,FTVORGN_NCHG_DATE,FTVORGN_TERM_DATE,FTVORGN_TITLE,FTVORGN_STATUS_IND,FTVORGN_ORGN_CODE_PRED,FTVORGN_DATA_ENTRY_IND,FTVORGN_VPDI_CODE) values ('D','5275',to_date('01-OCT-97','DD-MON-RR'),to_date('02-JUL-99','DD-MON-RR'),'JMOORE',to_date('31-DEC-99','DD-MON-RR'),null,'Administration','A','1006','N',null);
Insert into FTVORGN2 (FTVORGN_COAS_CODE,FTVORGN_ORGN_CODE,FTVORGN_EFF_DATE,FTVORGN_ACTIVITY_DATE,FTVORGN_USER_ID,FTVORGN_NCHG_DATE,FTVORGN_TERM_DATE,FTVORGN_TITLE,FTVORGN_STATUS_IND,FTVORGN_ORGN_CODE_PRED,FTVORGN_DATA_ENTRY_IND,FTVORGN_VPDI_CODE) values ('D','5325',to_date('01-OCT-97','DD-MON-RR'),to_date('13-JAN-00','DD-MON-RR'),'JMOORE',to_date('31-DEC-99','DD-MON-RR'),null,'Enrollment & Career Management','A','1006','N',null);
Insert into FTVORGN2 (FTVORGN_COAS_CODE,FTVORGN_ORGN_CODE,FTVORGN_EFF_DATE,FTVORGN_ACTIVITY_DATE,FTVORGN_USER_ID,FTVORGN_NCHG_DATE,FTVORGN_TERM_DATE,FTVORGN_TITLE,FTVORGN_STATUS_IND,FTVORGN_ORGN_CODE_PRED,FTVORGN_DATA_ENTRY_IND,FTVORGN_VPDI_CODE) values ('D','5350',to_date('01-OCT-97','DD-MON-RR'),to_date('02-JUL-99','DD-MON-RR'),'JMOORE',to_date('31-DEC-99','DD-MON-RR'),null,'Office of Education','A','1006','N',null);
Insert into FTVORGN2 (FTVORGN_COAS_CODE,FTVORGN_ORGN_CODE,FTVORGN_EFF_DATE,FTVORGN_ACTIVITY_DATE,FTVORGN_USER_ID,FTVORGN_NCHG_DATE,FTVORGN_TERM_DATE,FTVORGN_TITLE,FTVORGN_STATUS_IND,FTVORGN_ORGN_CODE_PRED,FTVORGN_DATA_ENTRY_IND,FTVORGN_VPDI_CODE) values ('D','5365',to_date('01-OCT-97','DD-MON-RR'),to_date('02-JUL-99','DD-MON-RR'),'JMOORE',to_date('31-DEC-99','DD-MON-RR'),null,'Library','A','1006','N',null);
REM INSERTING into FORUSOR
SET DEFINE OFF;
Insert into FORUSOR (FORUSOR_USER_ID_ENTERED,FORUSOR_COAS_CODE,FORUSOR_ORGN_CODE,FORUSOR_ACCESS_IND,FORUSOR_ACTIVITY_DATE,FORUSOR_USER_ID,FORUSOR_WBUD_ACCESS_IND,FORUSOR_SURROGATE_ID,FORUSOR_VERSION,FORUSOR_DATA_ORIGIN,FORUSOR_VPDI_CODE) values ('jam337','D','1006','B',to_date('01-AUG-12','DD-MON-RR'),'P','N',null,null,null,null);
Insert into FORUSOR (FORUSOR_USER_ID_ENTERED,FORUSOR_COAS_CODE,FORUSOR_ORGN_CODE,FORUSOR_ACCESS_IND,FORUSOR_ACTIVITY_DATE,FORUSOR_USER_ID,FORUSOR_WBUD_ACCESS_IND,FORUSOR_SURROGATE_ID,FORUSOR_VERSION,FORUSOR_DATA_ORIGIN,FORUSOR_VPDI_CODE) values ('jam337','D','1007','B',to_date('01-AUG-12','DD-MON-RR'),'P','N',null,null,null,null);
Insert into FORUSOR (FORUSOR_USER_ID_ENTERED,FORUSOR_COAS_CODE,FORUSOR_ORGN_CODE,FORUSOR_ACCESS_IND,FORUSOR_ACTIVITY_DATE,FORUSOR_USER_ID,FORUSOR_WBUD_ACCESS_IND,FORUSOR_SURROGATE_ID,FORUSOR_VERSION,FORUSOR_DATA_ORIGIN,FORUSOR_VPDI_CODE) values ('jam337','D','1011','B',to_date('01-AUG-12','DD-MON-RR'),'P','N',null,null,null,null);
Insert into FORUSOR (FORUSOR_USER_ID_ENTERED,FORUSOR_COAS_CODE,FORUSOR_ORGN_CODE,FORUSOR_ACCESS_IND,FORUSOR_ACTIVITY_DATE,FORUSOR_USER_ID,FORUSOR_WBUD_ACCESS_IND,FORUSOR_SURROGATE_ID,FORUSOR_VERSION,FORUSOR_DATA_ORIGIN,FORUSOR_VPDI_CODE) values ('jam337','D','1026','Q',to_date('30-JAN-08','DD-MON-RR'),'P','N',null,null,null,null);
Insert into FORUSOR (FORUSOR_USER_ID_ENTERED,FORUSOR_COAS_CODE,FORUSOR_ORGN_CODE,FORUSOR_ACCESS_IND,FORUSOR_ACTIVITY_DATE,FORUSOR_USER_ID,FORUSOR_WBUD_ACCESS_IND,FORUSOR_SURROGATE_ID,FORUSOR_VERSION,FORUSOR_DATA_ORIGIN,FORUSOR_VPDI_CODE) values ('jam337','D','1100','Q',to_date('30-JAN-08','DD-MON-RR'),'P','N',null,null,null,null);
the ftvorgn table is hierachical in nature, where the forusor table is not . The FORUSOR table holds the name and a hiearchy node from the FTVORGN TABLE within it.
I have spent several hours trying to get a query to work. I just don't see what my error is? if you follow the trail thus far you will notice that the FORUSOR table contains a value of '1006' in the FORUSOR_ORGN_CODE column. However everytime I run this query i get duplicate rows in return.
SELECT FTVORGN_ORGN_CODE,
FTVORGN_DATA_ENTRY_IND,
FTVORGN_TITLE,
FTVORGN_STATUS_IND,
FTVORGN_ORGN_CODE_NSF,
FTVORGN_HIERARCHY_TABLE_IND,
LEVEL, FTVORGN_ORGN_CODE_PRED
FROM FTVORGN2, FORUSOR
WHERE FORUSOR.FORUSOR_USER_ID_ENTERED = 'jam337'
CONNECT BY PRIOR FTVORGN_ORGN_CODE = FTVORGN_ORGN_CODE_PRED
AND PRIOR FTVORGN_COAS_CODE = FTVORGN_COAS_CODE
AND FTVORGN_EFF_DATE <= TO_DATE(sysdate)
AND FTVORGN_NCHG_DATE > TO_DATE(sysdate)
START WITH FTVORGN_ORGN_CODE = FORUSOR_ORGN_CODE
AND FTVORGN_COAS_CODE = 'D'
AND FTVORGN_EFF_DATE <= TO_DATE(sysdate)
AND FTVORGN_NCHG_DATE > TO_DATE(sysdate);
I't trying to basically get all the predecessor notes for everynote on FORUSOR but i keep ending up with duplicate rows.
I should end up with something similar to this.
sorry for the format of the results I'm not very good at formatting.
"FTVORGN_ORGN_CODE"
"FTVORGN_DATA_ENTRY_IND"
"FTVORGN_STATUS_IND"
"LEVEL"
"FTVORGN_ORGN_CODE_PRED"
"1006"
"N"
"A"
"1"
"01"
"2027"
"N"
"A"
"2"
"1006"
"2028"
"N"
"A"
"2"
"1006"
"5275"
"N"
"A"
"2"
"1006"
"5325"
"N"
"A"
"2"
"1006"
"5350"
"N"
"A"
"2"
"1006"
"5365"
"N"
"A"
"2"
"1006"
on column FORUSOR_ORGN_CODE in the FORUSOR table there is a node value that corresponds to a value on FTVORGN. However , that value could have a FTVORGN_ORGN_CODE_PRED which basically says I'm reporting to this value.
if I basically run this query for a node you can see the hierachy.
SELECT FTVORGN_ORGN_CODE,
FTVORGN_DATA_ENTRY_IND,
FTVORGN_STATUS_IND,
LEVEL, FTVORGN_ORGN_CODE_PRED
FROM FTVORGN2
CONNECT BY PRIOR FTVORGN_ORGN_CODE = FTVORGN_ORGN_CODE_PRED
AND PRIOR FTVORGN_COAS_CODE = FTVORGN_COAS_CODE
AND FTVORGN_EFF_DATE <= TO_DATE(sysdate)
AND FTVORGN_NCHG_DATE > TO_DATE(sysdate)
START WITH FTVORGN_ORGN_CODE = '1006'
AND FTVORGN_COAS_CODE = 'D'
AND FTVORGN_EFF_DATE <= TO_DATE(sysdate)
AND FTVORGN_NCHG_DATE > TO_DATE(sysdate);
I now need to pass the orgn code from forusor to this query to get all the ftvorgn_orgn_code that this user technically has reporting to him on FORUSOR.
so for example user jma337 has org
1006 in forusor
when i run the query i should get
jma337 2027
jma337 2028
jma337 5275 etc.
Reason in because these are all predecessors of 1006 which in this case would essentially be our level 1.
I need to do this all the FORUSOR_ORGN_CODE this user has recorded to him on FORUSOR.Switching to four digit years (to make the query work)
with
ftvorgn2 as
(select 'D' ftvorgn_coas_code,
'01' ftvorgn_orgn_code,
to_date('01-OCT-1988','DD-MON-YYYY') ftvorgn_eff_date,
to_date('19-MAR-1997','DD-MON-YYYY') ftvorgn_activity_date,
'TRAIN01' ftvorgn_user_id,
to_date('31-DEC-2099','DD-MON-YYYY') ftvorgn_nchg_date,
null ftvorgn_term_date,
'TOTAL UNIVERSITY' ftvorgn_title,
'A' ftvorgn_status_ind,
null ftvorgn_orgn_code_pred,
'N' ftvorgn_data_entry_ind,
null ftvorgn_vpdi_code
from dual
union all
select 'D','1006',to_date('22-SEP-2014','DD-MON-YYYY'),to_date('22-SEP-2014','DD-MON-YYYY'),'MJ35',to_date('31-DEC-2099','DD-MON-YYYY'),null,'Provost','A','01','N',null from dual union all
select 'D','2027',to_date('01-OCT-1997','DD-MON-YYYY'),to_date('01-OCT-1997','DD-MON-YYYY'),'DARDARMT',to_date('31-DEC-2099','DD-MON-YYYY'),null,'Sch of Biomed Engr, Sci Health Sys','A','1006','N',null from dual union all
select 'D','2028',to_date('28-FEB-2001','DD-MON-YYYY'),to_date('28-FEB-2001','DD-MON-YYYY'),'JMOORE',to_date('31-DEC-2099','DD-MON-YYYY'),null,'Sch of Envr Science, Engr Policy','A','1006','N',null from dual union all
select 'D','5275',to_date('01-OCT-1997','DD-MON-YYYY'),to_date('02-JUL-1999','DD-MON-YYYY'),'JMOORE',to_date('31-DEC-2099','DD-MON-YYYY'),null,'Administration','A','1006','N',null from dual union all
select 'D','5325',to_date('01-OCT-1997','DD-MON-YYYY'),to_date('13-JAN-2000','DD-MON-YYYY'),'JMOORE',to_date('31-DEC-2099','DD-MON-YYYY'),null,'Enrollment & Career Management','A','1006','N',null from dual union all
select 'D','5350',to_date('01-OCT-1997','DD-MON-YYYY'),to_date('02-JUL-1999','DD-MON-YYYY'),'JMOORE',to_date('31-DEC-2099','DD-MON-YYYY'),null,'Office of Education','A','1006','N',null from dual union all
select 'D','5365',to_date('01-OCT-1997','DD-MON-YYYY'),to_date('02-JUL-1999','DD-MON-YYYY'),'JMOORE',to_date('31-DEC-2099','DD-MON-YYYY'),null,'Library','A','1006','N',null from dual
forusor as
(select 'jam337' forusor_user_id_entered,
'D' forusor_coas_code,
'1006' forusor_orgn_code,
'B' forusor_access_ind,
to_date('01-AUG-12','DD-MON-RR') forusor_activity_date,
'P' forusor_user_id,
'N' forusor_wbud_access_ind,
null forusor_surrogate_id,
null forusor_version,
null forusor_data_origin,
null forusor_vpdi_code
from dual
union all
select 'jam337','D','1007','B',to_date('01-AUG-2012','DD-MON-YYYY'),'P','N',null,null,null,null from dual union all
select 'jam337','D','1011','B',to_date('01-AUG-2012','DD-MON-YYYY'),'P','N',null,null,null,null from dual union all
select 'jam337','D','1026','Q',to_date('30-JAN-2008','DD-MON-YYYY'),'P','N',null,null,null,null from dual union all
select 'jam337','D','1100','Q',to_date('30-JAN-2008','DD-MON-YYYY'),'P','N',null,null,null,null from dual
select ftvorgn_orgn_code,
ftvorgn_data_entry_ind,
ftvorgn_title,
ftvorgn_status_ind,
-- ftvorgn_orgn_code_nsf,
-- ftvorgn_hierarchy_table_ind,
level,
ftvorgn_orgn_code_pred
from ftvorgn2
start with ftvorgn_orgn_code in (select forusor_orgn_code
from forusor
where forusor_user_id_entered = 'jam337'
-- and forusor_coas_code = 'D'
connect by prior ftvorgn_orgn_code = ftvorgn_orgn_code_pred
and ftvorgn_eff_date <= sysdate
and ftvorgn_nchg_date > sysdate
FTVORGN_ORGN_CODE
FTVORGN_DATA_ENTRY_IND
FTVORGN_TITLE
FTVORGN_STATUS_IND
LEVEL
FTVORGN_ORGN_CODE_PRED
1006
N
Provost
A
1
01
2027
N
Sch of Biomed Engr, Sci Health Sys
A
2
1006
2028
N
Sch of Envr Science, Engr Policy
A
2
1006
5275
N
Administration
A
2
1006
5325
N
Enrollment & Career Management
A
2
1006
5350
N
Office of Education
A
2
1006
5365
N
Library
A
2
1006
Regards
Etbin -
Clarification regarding understanding of ORACLE CONNECT BY PRIOR Clause
Dear All,
I am trying to understand ORACLE CONNECT BY CLAUSE and I wrote a query to verify my understanding.
I wrote the below two queries.
select level, lpad('*',2*(level -1),'*') ||ename t_ename, sys_connect_by_path(ename,'/') enames, empno, mgr from emp
--where empno =empno
start with empno=7654
--start with mgr is null
connect by prior empno = mgr;
LEVEL T_ENAME ENAMES EMPNO MGR
1 MARTIN /MARTIN 7654 7698
Explanation: Here Start with EMPNO=7654 Means, the root node is 7654. Connect BY PRIOR EMPNO=MGR Means--> For the empno=7654 are there any persons under him. IE. Mgr with 7654.
select level, lpad('*',2*(level -1),'*') ||ename t_ename, sys_connect_by_path(ename,'/') enames, empno, mgr from emp
--where empno =empno
start with empno=7698
--start with mgr is null
connect by prior empno = mgr
LEVEL T_ENAME ENAMES EMPNO MGR
1 BLAKE /BLAKE 7698 7839
2 **ALLEN /BLAKE/ALLEN 7499 7698
2 **WARD /BLAKE/WARD 7521 7698
2 **MARTIN /BLAKE/MARTIN 7654 7698
2 **TURNER /BLAKE/TURNER 7844 7698
2 **JAMES /BLAKE/JAMES 7900 7698
Explanation: Here Start with EMPNO=7698 Means, the root node is 7698. Connect BY PRIOR EMPNO=MGR Means--> For the empno=7698 are there any persons under him. IE. Mgr with 7698.
IS MY UNDERSTANDING OF CONNECT BY PRIOR IS CORRECT?
Please correct me if I am wrong.
Thanks,
MK.Yes, you're right
Regards,
Sayan M. -
hi friends i have table like tree which have have two different type of leaf
my tables
deparment did,name
person perid,name
treetable id, pid,did,perid--tree table one row have only did or pid if did =2 , peridhave to be nullso i generate hierarchial structure between person and deparments
id pid did perid
1 null 1 null
2 1 null 1
3 2 null 2
4 2 null 3
5 3 2 null
6 5 null 4
7 5 null 5for example i select first deparment's person querry result must be (id=)2,3,4 not 6,7 because 6,7 below 5
can we write with connect by prior it?Hi,
JAVAMAN2 wrote:
hi friends i have table like tree which have have two different type of leaf
my tables
deparment did,name
person perid,name
treetable id, pid,did,perid--tree table one row have only did or pid if did =2 , peridhave to be nullso i generate hierarchial structure between person and deparments
id pid did perid
1 null 1 null
2 1 null 1
3 2 null 2
4 2 null 3
5 3 2 null
6 5 null 4
7 5 null 5for example i select first deparment's person querry result must be (id=)2,3,4 not 6,7 because 6,7 below 5I'm not sure why you want to exclude 5, 6 and 7. Is it that you don't want any nodes (except the root) with NULL perid?
can we write with connect by prior it?Yes.
If my guess about excluding 5 and it's descendants is correct:
SELECT * -- or whatever you want
FROM treetable
START WITH pid IS NULL
CONNECT BY pid = PRIOR id
AND depid IS NULL
;If you need the names from the other tables, then outer-join both other tables, and use NVL to get the one name that is not NULL. -
Complex connect by prior query
I need SQL(for hierarchical tree) for a function which accepts node as input parameter and returns ref cursor.
Following is a sample tree:
1
--2.1
----3.1
------4.1
--------5.1
----------6.1
----------6.2
----3.2
------4.2
--------5.2
--2.2
----3.2
------4.2
--------5.2
----3.3
----3.4
------4.1
--------5.1
----------6.1
----------6.2
1 is at the root level and 2.1 & 2.2 are immediate children and so on.
The output tree should be all related parents and children of the passed node.
e.g:
If the input is 4.1, the output tree will be:
1
--2.1
----3.1
------4.1
--------5.1
----------6.1
----------6.2
--2.2
----3.4
------4.1
--------5.1
----------6.1
----------6.2
If the input is 4.2, the output tree will be:
1
--2.1
----3.2
------4.2
--------5.2
--2.2
----3.2
------4.2
--------5.2
The complex part, I guess, is to remove unwanted(not related) branches from the tree.
Following is the representation of the table RELATIONSHIP
ID PARENT CHILD
1-------1-------2.1
2-------1-------2.2
3-------2.1-----3.1
4-------2.1-----3.2
5-------2.2-----3.2
6-------2.2-----3.3
7-------2.2-----3.4
8-------3.1-----4.1
9-------3.2-----4.2
10------3.4-----4.1
11------4.1-----5.1
12------4.2-----5.2
13------5.1-----6.1
14------5.1-----6.2
Pls. help me out to form this CONNECT BY PRIOR query.
Thanks in advance.make sure you include 2 things in your queries.
# the row number
# the level
for traversing up the tree set
level = 0 - level and
row_number = 0 - row_number.
you than can then do an order by row_number if you union the two queries
for example:
============
select tbl.child,
tbl.parent,
level lvl,
rownum rn
from some_table tbl
start with tbl.parent = 4.1
connect by prior tbl.child = tbl.parent
union
select tbl.child,
tbl.parent,
0 - level lvl,
0 - rownum rn
from some_table tbl
start with tbl.child = 4.1
connect by prior tbl.parent = tbl.child
order by rn asc -
Connect By Prior - Performance and Missing Parent
I have to say right off the top that I'm not a SQL expert by any means so please be gentle. :)
I have a query that runs through a Bill Of Materials that works except for two things:
1. It is horribly slow - 60-90 seconds to return under 300 rows.
2. It doesn't show the parent node.
I'm looking into indexes now, so I'm not sure if there's an issue there or not. As far as #2 goes, there are two tables that are involved: BOM_STRUCTURES_B and BOM_COMPONENTS_B. Every item below the parent node has a BOM_COMPONENTS_B record. If a BOM_COMPONENT is a parent to other components, there is a BOM_STRUCTURES_B record for that. (In other words, everything that is a parent has a BOM_STRUCTURES_B record, and all the children have a BOM_COMPONENTS_B record that point to the parent - BOM_STRUCTURES_B). The only exception to this is the parent node, which only has a BOM_STRUCTURES_B record (it is NOT a child, so there is no BOM_COMPONENTS_B record). I've added a "UNION" to the bottom of the script below, but it changes my sort order completely.
Here's my script:
select bbm.assembly_item_id,
bic.component_item_id Component ,
msi.segment1 Name,
msi.description Description,
bic.component_quantity Quantity,
lpad( ' ', level*2 ) || level MyLevel
from bom_structures_b bbm
,bom_components_b bic
, mtl_system_items msi
where bbm.bill_Sequence_id = bic.bill_sequence_id
and msi.inventory_item_id = bic.component_item_id
and msi.organization_id = bbm.organization_id
start with bbm.assembly_item_id = 271962
and bbm.organization_id = 85
connect by prior bic.component_item_id = bbm.assembly_item_id;
I've hard-coded "start with bbm.assembly_item_id = 271962", as it is the root node of a tree (BOM).
Here's my structure, with extra fields clipped out:
DBMS_METADATA.GET_DDL('TABLE','BOM_STRUCTURES_B','BOM')
CREATE TABLE "BOM"."BOM_STRUCTURES_B"
( "ASSEMBLY_ITEM_ID" NUMBER,
"ORGANIZATION_ID" NUMBER NOT NULL ENABLE,
"COMMON_BILL_SEQUENCE_ID" NUMBER NOT NULL ENABLE,
) PCTFREE 20 PCTUSED 80 INITRANS 10 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 131072 NEXT 131072 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 4 FREELIST GROUPS 4 BUFFER_POOL DEFAULT)
TABLESPACE "APPS_TS_TX_DATA"
DBMS_METADATA.GET_DDL('TABLE','BOM_COMPONENTS_B','BOM')
CREATE TABLE "BOM"."BOM_COMPONENTS_B"
("COMPONENT_ITEM_ID" NUMBER,
"BILL_SEQUENCE_ID" NUMBER NOT NULL ENABLE,
"PARENT_BILL_SEQ_ID" NUMBER,
) PCTFREE 35 PCTUSED 50 INITRANS 10 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 131072 NEXT 131072 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 4 FREELIST GROUPS 4 BUFFER_POOL DEFAULT)
TABLESPACE "APPS_TS_TX_DATA"
DBMS_METADATA.GET_DDL('TABLE','MTL_SYSTEM_ITEMS_B','INV')
CREATE TABLE "INV"."MTL_SYSTEM_ITEMS_B"
( "INVENTORY_ITEM_ID" NUMBER NOT NULL ENABLE,
"ORGANIZATION_ID" NUMBER NOT NULL ENABLE,
"DESCRIPTION" VARCHAR2(240),
) PCTFREE 10 PCTUSED 70 INITRANS 10 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 131072 NEXT 131072 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 4 FREELIST GROUPS 4 BUFFER_POOL DEFAULT)
TABLESPACE "APPS_TS_TX_DATA"
The relationship between the tables is:
A BOM_STRUCTURES_B has many BOM_COMPONENTS_B where BOM_COMPONENTS_B.PARENT_BILL_SEQ_ID = BOM_STRUCTURES_B.BILL_SEQUENCE_ID. Both BOM_STRUCTURES_B.ASSEMBLY_ITEM_ID and BOM_COMPONENTS_B.COMPONENT_ITEM_ID are related to a single MTL_SYSTEM_ITEMS_B.INVENTORY_ITEM_ID.
Thanks to anyone who can help with this horrible problem I've been fighting with for much too long! :)
Thank you!
SteveDue to an error in the otn forums (*), this got posted to the wrong forum, sorry for that, I'll repost in the correct one.
*) For the forum admins: I was browsing the SQL/PLSQL forum, clicked on 'new message', got the login prompt, logged in, and apparently this got me to the application server forum for some reason. -
Strange Errors from Import Server on Schema-based maps for repeating nodes
I have already posted one thread about this, but perhaps some more in-depth information is required:
We have uploaded our schema into the console, and have mapped the following multi-node file to it:
CUSTOMER <repeating node>
MDM_CUST (new Customer number returned)
CUSTOMER(mapped back from XI for RECORD MATCHING)
CREATE_DATE (text field)
CONTACT<repeating node>
CONTACT_NO (RECORD MATCHING : NON-QUALIFIER)
CONTACT_SAP_NO (QUALIFIER)
[NOTE: There are other qualifers for CONTACT, but only this one
qualifier is being returned and is the only field that would and should be updated.
The other fields should be left alone. However, it appears to overwrite EVERYthing
with NULL other than these two fields. The CONTACT_NO is the sole qualifer on
the table. It is a calculated field that equals the Auto-ID that is also produced on
the table - but obviously not mapped.]
PARTNER<repeating node>
CUSTOMER_NO (RECORD MATCHING : NON-QUALIFER)
PARTNER_FUNC (NON-QUALIFIER - Code given to lookup from PARTNER_FUNCTION table which has Code and Desc)
PARTNER_NO (NON-QUALIFIER)
DEFAULT_PARTNER_FLAG (QUALIFIER)
<CLOSE CUSTOMER NODE>
In the Customer main table, all records are matched on CUSTOMER. The CONTACT_NO non-qualifer is matched to the CONTACT qualified lookup table directly since it is the only available non-qualifier. For the PARTNER table, the three qualifiers are mapped and then a compound field is created to map to the PARTNER table entry.
We have the Configurations set at:
Default Multi-Valued Update: Replace
Default Qualified Update: Replace
Default Matching Qualifiers: None
Our record matching is: Create / Update(ALL MAPPED FIELDS) / Update(ALL MAPPED FIELDS)
When the map is reused, the repeating nodes are all recognized, be it one node or many. However, when the map is reused, it does not automatically map the values for many of the Partner fields (nor the compound field) though the fields are correctly mapped.
Also, when we attempt to use Import Server to automate the mapping, we use the mdis.ini set to
Automap Unmapped Value=True
Unmapped Value Handling=Add
Always Use Unmapped Value Handling=False
We get a very strange error saying that :
[code]Encountered a pre-SP4 map. The map needs to be upgraded to SP4.
Solution: Please, Launch the Import Manager GUI using the same source file and simply save the map.[code]
Could it be that our MDM 5.5 SP4 is not compatible with schemas generated with XI v.7 SP9?
All of our MDM is 5.5 SP4. It makes no sense that we would get THIS kind of error. Also, I am questioning our combination of these and other configurations since it doesn't always seem to do what it shouldHi Donald,
this sounds good so far. Just a short hint: you've set "Default Qualified Update: Replace". This explains why Import server "appears to overwrite EVERYthing
with NULL other than these two fields.". The reason is the replace. A replace means that the old values are completely deleted and only the new incoming ones are stored. I'd suggest to use any of the Update possibilities.
Note: the definitions you set in in the record matching step are mainly for the new records in the main table! Records in qualified look ups are handled differently! If you open your map in Import Manager, switch to tab "Map Fields/Values". Select a field that is your non qualifier. Right-Click on it and choose "Set qualified update" from the context menu. Now you can define which qualifiers can be used for the qualified look up matching. And you can define as well what should happen with new and/or existing records.
Regarding the value mapping: do you use keymapping? Or do you use simple values only? Do you save the map everytime you add a new value mapping?
Regarding the issue "Encountered a pre-SP4 map. The map needs to be upgraded to SP4. Solution: Please, Launch the Import Manager GUI using the same source file and simply save the map". This sounds very likely like a bug in MDIS and should be reported to SAP!
Hope that helps
Michael -
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.
Maybe you are looking for
-
Comparison of application server
Hello, Can I get document comparisons of java application servers? I need performance comparison and pros and cons of following Application Servers 1) Oracle Application Server 2)BEA Weblogic (Oracle WebLogic) 3)Tomcat 4)Jboss 5)Resin Regards, Sujnan
-
Hi Experts, I am new to marketing reports. Could you advise me is marketing report fall under the CRM analytics ? Any Business content to show me the keyfigures? I need some sample marketing KPIs and general or common kinds of reports marketing users
-
Hi every one, We have two Business Group and there are people into two Business, Can I log in with a user in two BG in application Self Service?, When I asign a person for the user, the information for SSHR is correct, but the approvals for iProcurem
-
How to update my payment method since I move country from SG to AU? i try to update my new credit card but message prompt, "Your payment method is not accepted" maybe because it was set up in SG. what will I do?
-
In MIGO two seperate entry for the same Material in Accounting Document
Dear Expert, Hi Business is doing stock transfer transaction. Here with reference to stock transfer PO when business is doing Goods Issue. here there is two stock transfer PO. in one PO there is two line items for the same material. in the second PO