Connect by prior (hierarchical query)
Hi All,
Some1 asked me a question which goes like this:
Source
emp_no dep_no
110 10
110 20
110 30
110 40
120 10
130 10
130 20
write a query to achieve the following output from the above source
emp_no dept_no
110 10203040
120 10
130 1020
Now I have come across solutions with 'connect by prior' clauses but I am nt able to understand how oracle is producing that result , Could someone point me to a good article or text that can explain this concept very thoroughly( I have searched on google and got to see many articles but I couldnt able to understand it since these articles were not explaining everything)
Regards
Rahul
You can try this:
SQL> ed
Wrote file afiedt.buf
1 SELECT deptno,MAX(TRIM(SUBSTR(SYS_CONNECT_BY_PATH(empid,','),2)))
2 KEEP(DENSE_RANK LAST ORDER BY deptno) FROM
3 (SELECT deptno,empid,ROW_NUMBER() OVER(PARTITION BY deptno ORDER BY deptno) curr,
4 ROW_NUMBER() OVER(PARTITION BY deptno ORDER BY deptno) - 1 prev
5 FROM emp)
6 CONNECT BY PRIOR curr = prev
7 AND deptno = PRIOR deptno
8 START WITH curr = 1
9* group by deptno
SQL> /
DEPTNO MAX(TRIM(SUBSTR(SYS_CONNECT_BY_PATH(EMPID,','),2)))KEEP(DENSE_RANKLASTORDERBYDEPTNO)
10 1,2,3,4
20 5,6,7
SQL> select deptno,empid from emp;
DEPTNO EMPID
10 1
10 2
10 3
10 4
20 5
20 6
20 7
7 rows selected.
SQL>
Similar Messages
-
what is mean by CONNECT TO PRIOR? is it SQL keyword?
i have tried following query....
select level,max(sal) from emp
where level=2
connect to prior sal>sal
group by level
please explain above query........
thanx ....check this also
http://www.oracle.com/technology/oramag/oracle/05-may/o35asktom.html -
Problem Creating a query for a hierarchical tree. [using connect by prior]
Hi all,
I have 2 tables.
box (box_id, box_name)
item(item_id, item_name, box_id)
In a box there are several items.
I want to create a hierachical tree to display items that are present in each box.
LIKE:
|---BOX1
| |----ITEM 1
| |----ITEM 2
|
|---BOX2
| |----ITEM 1
| |----ITEM 2
Currently i am trying this query:
SELECT -1 state, box_name, 'icon' icon, box_id val
from box b, item i;
I don't know what value to put for level, i don't know how to code the 'connect by prior' part.
Could you please advise me?
Michaël.
PS. Then i will eventually use this query in forms builder.Note the name of this forum is "SQL Developer *(Not for general SQL/PLSQL questions)*" - so only for issues with the SQL Developer tool. Please post these questions under the dedicated SQL And PL/SQL forum.
Regards,
K. -
Hierarchical Query with Rollup Sum (CONNECT BY with GROUP BY ROLLUP)
Hi all,
Imagine the following scenario: i have an ACCOUNT table which holds accounts and their hierarchy (currently 5 levels), and a BALANCE table which holds balance records for the accounts. Only CHILD accounts (level 5) have records in the BALANCE table. Simple example:
CREATE TABLE accounts (account_code VARCHAR2(30), parent_account VARCHAR2(30), account_desc VARCHAR2(400));
CREATE TABLE balances (account_code VARCHAR2(30), balance_amount NUMBER(18,2));
INSERT INTO ACCOUNTS VALUES ('TOT',NULL,'Total');
INSERT INTO ACCOUNTS VALUES ('ANA1','TOT','General Expenses');
INSERT INTO ACCOUNTS VALUES ('4801001','ANA1','Small Expenses');
INSERT INTO ACCOUNTS VALUES ('4801002','ANA1','Transportation');
INSERT INTO ACCOUNTS VALUES ('ANA2','TOT','Health Expenses');
INSERT INTO ACCOUNTS VALUES ('4802001','ANA2','Healthcare');
INSERT INTO ACCOUNTS VALUES ('4802002','ANA2','Facilities');
INSERT INTO BALANCES VALUES ('4801001', 2000);
INSERT INTO BALANCES VALUES ('4801002', 1000);
INSERT INTO BALANCES VALUES ('4802001', 3000);
INSERT INTO BALANCES VALUES ('4802002', 4000);What i need in this scenario is to run a hierarchical query, where for each node i compute the sum of all its children (In LEAF nodes which are the child accounts, this sum is the value in BALANCES itself). Final Result would be:
TOT -> 10000
ANA1 -> 3000
4801001 -> 2000
4801001 -> 1000
ANA2 -> 7000
4802001 -> 3000
4802002 -> 4000I have tried various ways, and found out a workaround which works for a fixed amount of levels, basically it builds the hierarchy and computes the SYS_CONNECT_BY_PATH, then splits this as a regular expression and uses GROUP BY ROLLUP to compute the higher levels. Then i assemble it again, now with the computed values. Below is the example query:
select level
, NVL (vfinal.child_account,'TOTAL') ||' - '||
( SELECT account_desc
FROM accounts
WHERE account_code = vfinal.child_acct ) account_name
, to_char(sum_bal, 'fm999g999g999g990') as rolled_up_balance
from
select coalesce( princ.lvl3, princ.lvl2, princ.lvl1 ) child_acct
, DECODE ( princ.lvl2 , NULL
, NULL
, DECODE ( princ.conta_lvl3, NULL
, princ.conta_lvl1,princ.conta_lvl2 ) ) parent_acct
, sum(princ.balance_amount) sum_bal
from (
select hier.lvl1
, hier.lvl2
, hier.lvl3
, hier.parent_account
, hier.account_code child_acc
, bal.balance_amount
from ( select level
, sys_connect_by_path( account_code, '/' ) hierarchy_acct
, REGEXP_SUBSTR(sys_connect_by_path( account_code, '/' ),'[^/]+',1,3) lvl3
, REGEXP_SUBSTR(sys_connect_by_path( account_code, '/' ),'[^/]+',1,2) lvl2
, REGEXP_SUBSTR(sys_connect_by_path( account_code, '/' ),'[^/]+',1,1) lvl1
, account_code
, parent_account
from accounts acc
where level <= 3
start with parent_account is null
connect by nocycle prior account = parent_account
order siblings by parent_account
) hier
, balances bal
where bal.cod_conta = hier.account_code
) princ
where princ.lvl1 is not null
group by rollup ( princ.lvl1
, princ.lvl2
, princ.lvl3 )
order by princ.conta_lvl1
, princ.conta_lvl2
, princ.conta_lvl3
) vfinal
where child_acct is not null
start with parent_acct is null
connect by nocycle prior child_acct = parent_acctAll said and done, what i need is to do the same thing for infinite levels, because this query has 3 fixed levels. Do you know how can i structure a new query where, independently of the number of levels, the parent sums are all rolled up like this?
Thanks a lot in advance! Best Regards!
Thiago
Edited by: Thiago on Sep 6, 2011 11:31 AM
Edited by: Thiago on Sep 6, 2011 1:01 PMHi,
Thiago wrote:
Hi all,
Imagine the following scenario: i have an ACCOUNT table which holds accounts and their hierarchy (currently 5 levels), and a BALANCE table which holds balance records for the accounts. Only CHILD accounts (level 5) have records in the BALANCE table. Simple example:
CREATE TABLE accounts (account_code VARCHAR2(30), parent_account VARCHAR2(30), account_desc VARCHAR2(400));
CREATE TABLE balances (account_code VARCHAR2(30), balance_amount NUMBER(18,2));
INSERT INTO ACCOUNTS ('TOT',NULL,'Total');
INSERT INTO ACCOUNTS ('ANA1','TOT','General Expenses');
INSERT INTO ACCOUNTS ('4801001','ANA1','Small Expenses');
INSERT INTO ACCOUNTS ('4801002','ANA1','Transportation');
INSERT INTO ACCOUNTS ('ANA2','TOT','Health Expenses');
INSERT INTO ACCOUNTS ('4802001','ANA2','Healthcare');
INSERT INTO ACCOUNTS ('4802002','ANA2','Facilities');
INSERT INTO BALANCES ('4801001', 2000);
INSERT INTO BALANCES ('4801001', 1000);
INSERT INTO BALANCES ('4802001', 3000);
INSERT INTO BALANCES ('4802001', 4000);
Thanks for posting the CREATE TABLE and INSERT statements. Remember why you do it: so that the people who want to help you can re-create the problem and test their ideas. If the statments don't work, then they are not so useful. None of the INSERT statements you posted work: they all need a VALUES keyword. Please test those statments before you post them.
Also, make sure that the reuslts you post correspond to the sample data you post. In your sample data, there are no rows in balances for account_codes '4801002' or '4802002'.
I think you want something like this:
WITH connect_by_results AS
SELECT CONNECT_BY_ROOT account_code AS root_account_code
, account_code
FROM accounts
-- NOTE: No START WITH clause
CONNECT BY parent_account = PRIOR account_code
SELECT c.root_account_code || ' -> '
|| TO_CHAR (SUM (b.balance_amount)) AS txt
FROM connect_by_results c
LEFT OUTER JOIN balances b ON c.account_code = b.account_code
GROUP BY c.root_account_code
; -
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 -
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 ... -
Improve hierarchical query speed using 'start with' and 'conect by prior'
Hi
The query within the 'explain' runs about a second and I need to imporve it.
There are indexes set for both the child_id and the parent_id.
The total number of rows for the PRM_COMPONENTS table is 120000.
I'm working on 'Oracle Database 10g Release 10.2.0.4.0 - 64bit Production' in a Linux OS.
I've included the explain plan below.
Any suggestions would be appreciated.
Thanks
EXPLAIN PLAN FOR
SELECT substr(SYS_CONNECT_BY_PATH(usage_title, '|'), 2, instr( SYS_CONNECT_BY_PATH(usage_title, '|'), '|', -1) -2 )
FROM prm_components p
WHERE LEVEL > 1 and usage_id = 10301100
START WITH parent_usage_id is null
CONNECT BY PRIOR usage_id = parent_usage_id;
select * from table(dbms_xplan.display);
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
| 0 | SELECT STATEMENT | | 6 | 174 | 4 (0)|
|* 1 | FILTER | | | | |
|* 2 | CONNECT BY WITH FILTERING | | | | |
|* 3 | TABLE ACCESS FULL | PRM_COMPONENTS | 69526 | 3937K| 2468 (1)|
| 4 | NESTED LOOPS | | | | |
| 5 | CONNECT BY PUMP | | | | |
| 6 | TABLE ACCESS BY INDEX ROWID| PRM_COMPONENTS | 6 | 174 | 4 (0)|
|* 7 | INDEX RANGE SCAN | PRM_PARENT_USAGE_ID_I | 2 | | 1 (0)|
Predicate Information (identified by operation id):
1 - filter(LEVEL>1 AND "USAGE_ID"=10301100)
2 - access("PARENT_USAGE_ID"=PRIOR "USAGE_ID")
3 - filter("PARENT_USAGE_ID" IS NULL)
7 - access("PARENT_USAGE_ID"=PRIOR "USAGE_ID")
Note
- 'PLAN_TABLE' is old versionHi
I've resolved the issue by other means but here is the description of the table anyways.
USAGE_ID NOT NULL NUMBER
PARENT_USAGE_ID NUMBER
PRODUCT_CODE NOT NULL VARCHAR2(12)
PRINT_OR_ONLINE NOT NULL CHAR(1)
SLP_ID VARCHAR2(24)
RELEASE_NAME VARCHAR2(80)
USAGE_TITLE VARCHAR2(255)
ENT_USAGE_TITLE VARCHAR2(255)
TRANS_TITLE VARCHAR2(255)
REVISION_TYPE VARCHAR2(8)
ACTIVE CHAR(1)
MARKED_FOR_DELETION CHAR(1)
CREATED_DT DATE
CREATED_BY VARCHAR2(80)
UPDATED_DT DATE
UPDATED_BY VARCHAR2(80)
PLANNING_COMMENTS VARCHAR2(2000)
OUTPUT_FILENAME VARCHAR2(200)
TRANSFORMER_ID NUMBER(38)
START_PAGE VARCHAR2(8)
START_PAGE_NUM NUMBER
END_PAGE VARCHAR2(8)
END_PAGE_NUM NUMBER
VOLUME VARCHAR2(8)
SORT_ORDER NUMBER
PRIORITY NUMBER
XREF_BLIND_ENTRY CHAR(1)
SPECIAL_CATEGORY VARCHAR2(20)
TO_BE_REVISED CHAR(1)
EDITOR VARCHAR2(80)
DUE_DT DATE
POSTED_DT DATE
LOGICAL_UOI_ID NOT NULL VARCHAR2(40)
PHYSICAL_UOI_ID NOT NULL VARCHAR2(40)
EDIT_APPROV_UOI_ID VARCHAR2(40)
EDIT_APPROV_BY VARCHAR2(80)
EDIT_APPROV_DT DATE
FINAL_APPROV_UOI_ID VARCHAR2(40)
FINAL_APPROV_BY VARCHAR2(80)
FINAL_APPROV_DT DATE
PHOTO_APPROV_UOI_ID VARCHAR2(40)
PHOTO_APPROV_BY VARCHAR2(80)
PHOTO_APPROV_DT DATE
RIGHTS_APPROV_UOI_ID VARCHAR2(40)
RIGHTS_APPROV_BY VARCHAR2(80)
RIGHTS_APPROV_DT DATE
LAYOUT_APPROV_UOI_ID VARCHAR2(40)
LAYOUT_APPROV_BY VARCHAR2(80)
LAYOUT_APPROV_DT DATE
BLUES_APPROV_UOI_ID VARCHAR2(40)
BLUES_APPROV_BY VARCHAR2(80)
BLUES_APPROV_DT DATE
LAST_PUB_ONLINE_DT DATE
LAST_PUB_PRINT_DT DATE
BLIND_ENTRY_ON_DT DATE
BLIND_ENTRY_OFF_DT DATE
DELIVERY_APPROV_UOI_ID VARCHAR2(40)
DELIVERY_APPROV_BY VARCHAR2(80)
DELIVERY_APPROV_DT DATE
APPROVAL_STATUS VARCHAR2(40)
CHANGE_SINCE_LAST_DELIVERY CHAR(1)
USAGE_COMMENTS VARCHAR2(2000)
LEXILE_CODE VARCHAR2(18)
SERIES VARCHAR2(8)
USAGE_TITLE_TMP VARCHAR2(255)
ENT_USAGE_TITLE_TMP VARCHAR2(255)
WORD_COUNT VARCHAR2(10)
READ_LEV VARCHAR2(7)
GRADES VARCHAR2(80)
DELIVERY_TYPE NOT NULL CHAR(1)
METADATA_APPROVAL_STATUS VARCHAR2(40)
METADATA_APPROVAL_BY VARCHAR2(80)
METADATA_APPROVAL_DT DATE
RESOURCE_FLAG CHAR(1)
STZ_FLAG CHAR(1)
RESOURCE_TYPE_CODE VARCHAR2(16)
ASSET_DESCRIPTION VARCHAR2(2000)
ROLE_CODE VARCHAR2(16)
PROGRAMS_DATA VARCHAR2(256)
TIME_TO_COMPLETE VARCHAR2(32)
ENTITLEMENTS_DATA VARCHAR2(256)
ISBN_10 VARCHAR2(32)
ISBN_13 VARCHAR2(32)
MFG_ITEM_NO VARCHAR2(256)
AR CHAR(1)
SRC CHAR(1)
SRC_POINTS NUMBER
AUTHORS VARCHAR2(320)
SEARCH_STRINGS VARCHAR2(2000)
PATH_SLP_ID VARCHAR2(256)
PATH_GTC VARCHAR2(256)
PATH_TITLE VARCHAR2(2560)
GRL VARCHAR2(8)
COMMON_CORE CHAR(1) -
Hierarchical query - CONNECT BY and result in different columns.
Hi everyone,
I have a small question, hoping that someone can help me. As you will see, this
is not very important. It's just me wondering if I can learn something in SQL.
Let's suppose I have an ORGANIZATIONS table:
CREATE TABLE ORGANIZATIONS (
ID_ORGANISATION NUMBER(10) PRIMARY KEY,
NAME VARCHAR2(255) NOT NULL,
ID_PARENT_ORGANIZATION NUMBER(10)
INSERT INTO ORGANIZATIONS(1, 'Parent of all', null);
INSERT INTO ORGANIZATIONS(11, 'Child 11', 1);
INSERT INTO ORGANIZATIONS(111, 'Child 111', 11);
INSERT INTO ORGANIZATIONS(112, 'Child 112', 12);
INSERT INTO ORGANIZATIONS(12, 'Child 12', 1);
INSERT INTO ORGANIZATIONS(121, 'Child 121', 12);Let's also assume that we can have an undefined number of levels.
Now, my question:
With a query like
SELECT LPAD(' ', LEVEL) || NAME as conc
FROM ORGANIZATIONS
START WITH ID_ORGANIZATION = 1
CONNECT BY PRIOR ID_ORGANISATION = ID_PARENT_ORGANIZATION
..I have a result like
[conc]
"Parent of all"
"Child 11"
"Child 111"
"Child 112"
"Child 12"
...All in one columns.
What I'd like is a result like this one:
[LEVEL_1] [LEVEL_2] [LEVEL_3]
"Parent of all"
"Child 11"
"Child 111"
"Child 112"
"Child 12"
"Child 121"I'd like to have this structure for the 5 first levels. Do you think this is possible or not?
Thanks for your advice,user13117585 wrote:
I'd like to have this structure for the 5 first levels. Do you think this is possible or not?Yes, something like...
SQL> ed
Wrote file afiedt.buf
1 SELECT DECODE(LEVEL,1,NAME) as conc1
2 ,DECODE(LEVEL,2,NAME) as conc2
3 ,DECODE(LEVEL,3,NAME) as conc3
4 ,DECODE(LEVEL,4,NAME) as conc4
5 ,DECODE(LEVEL,5,NAME) as conc5
6 ,DECODE(LEVEL,6,NAME) as conc6
7 FROM ORGANIZATIONS
8 START WITH ID_ORGANISATION = 1
9* CONNECT BY PRIOR ID_ORGANISATION = ID_PARENT_ORGANIZATION
SQL> /
CONC1 CONC2 CONC3 CONC4 CONC5 CONC6
Parent of all
Child 11
Child 111
Child 12
Child 112
Child 121
6 rows selected.
SQL>Just add levels as you need them. -
Connect by prior working in sql but not in forms 10g hierarchical tree
Hello Friends,
I have the following connect by prior example which is working in sql command prompt but not in Forms 10g hierarchical tree item type. Can you please let me know why ?
configuration: Forms 10g patchset 10.1.2.0.2 and oracle 11g database on windows 7
SQL> SELECT 1 InitialState,
2 level Depth,
3 labeller NodeLabel,
4 NULL NodeIcon,
5 to_char(reportno) NodeValue
6 FROM reports where formname = 'billinin.fmx' or reportno > 9999
7 start with reportno > 9999
8 CONNECT BY PRIOR reportno = labelno
9 /
INITIALSTATE DEPTH NODELABEL N NODEVALUE
1 1 FIRST 10000
1 2 report1 UD Label 1
1 2 report2 UD Label 2
1 2 report3 UD Label 3
1 1 SECOND 10001
1 1 THIRD 10002
If I write this command in forms hierarchical tree, then it is working, why not the above code ?
SQL> SELECT 1 InitialState,
2 level Depth,
3 labeller NodeLabel,
4 NULL NodeIcon,
5 to_char(reportno) NodeValue
6 FROM reports
7 start with reportno > 9999
8 CONNECT BY PRIOR reportno = labelnoThanks Room,
This command worked ! I will put the sample working code here. It will help you to filter the records in a tree in sql command prompt as well as in forms hierarchical tree 10g.
SELECT 1 InitialState,
level Depth,
labeller NodeLabel,
NULL NodeIcon,
to_char(reportno) NodeValue
FROM reports
start with reportno > 9999
CONNECT BY PRIOR reportno = labelno
AND FORMNAME = :reports.testitem -
CONNECT BY PRIOR and performance of Query Plan
Anyone,
I have an SQL Statement that is performing rather slow and I am trying to figure out if I could optimize it. Here is the SQL:
SELECT/*+ index(MAXIMO.EQNDX99) */
maximo.equipment.eqnum, maximo.equipment.parent, LEVEL
FROM maximo.equipment@maxi_dblink
WHERE parent = :b1 CONNECT BY PRIOR eqnum = parent
ORDER BY eqnum, LEVELAfter some research in this board I followed some advice found to create an index on the table for both the eqnum, parent and the parent, eqnum. EQNDX99 and EQNDX999 respectivley.
Now the Qery Plan for this query shows the following:
SELECT STATEMENT (REMOTE)
SORT (ORDER BY)
FILTER
CONNECT BY
INDEX (FAST FULL SCAN) EQNDX99 (NON-UNIQUE)
TABLE ACESS (BY USER ROWID) EQUIPMENT
INDEX (RANGE SCAN) EQNDX999 (NON-UNIQUE)Now it appears to be using both indexes but it is operating through a DBLINK. Is there anything else I can do to increase performance??? It appears to be using the HINT through the link as well.
Thanks for any help I can get,
David Millerhow long does it takes to complete the query?
-
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 -
Strange Behaviour of Connect By Prior query
Has anybody faced a problem like your query returns alternative record like suppose you have 10 records in a table and your connect by prior query gives you 1st, 3rd, 5th and so on. My query was working fine few days back but suddenly its giving me different result. I don't know whether there was some settings changed by someone or not.
Can't guess anything from here. You need to post the query, sample data and the result.
-
Recursive Hierarchical Query with CONNECT BY/START WITH
I want to traverse a tree which has is not hierarchical in linear sense. You know, like :
A
B
C
D
E
C
F
D
E
C
You see that D, Eand C are repeated and C is under E as well as under B. I tried using CONNECT BY and START WITH but I get tripliate occurances of C under B, and 2 occurances of DEC under A and under F. Clearly this is not correct. I believe SQL3 has special way of handling with such recursive trees. How does O9i handle these constructs? Please help.CREATE OR REPLACE PACKAGE hierarchy
IS
TYPE branchtable_type IS TABLE OF VARCHAR2 (4000)
INDEX BY BINARY_INTEGER;
branchtable branchtable_type;
FUNCTION branch
(v_level IN NUMBER,
v_value IN VARCHAR2,
v_delimeter IN VARCHAR2 DEFAULT CHR (0))
RETURN VARCHAR2;
PRAGMA RESTRICT_REFERENCES (branch, WNDS);
END hierarchy;
CREATE OR REPLACE PACKAGE BODY hierarchy
IS
returnvalue VARCHAR2 (4000);
FUNCTION branch
(v_level IN NUMBER,
v_value IN VARCHAR2,
v_delimeter IN VARCHAR2 DEFAULT CHR (0))
RETURN VARCHAR2
IS
BEGIN
branchtable (v_level) := v_value;
returnvalue := v_value;
FOR i IN REVERSE 1 .. v_level - 1
LOOP
returnvalue := branchtable (i)
|| v_delimeter
|| returnvalue;
END LOOP;
RETURN returnvalue;
END branch;
END hierarchy;
SELECT id, parent_id, print_order, lvl
FROM (SELECT id, parent_id, print_order, LEVEL lvl,
hierarchy.branch (LEVEL, print_order) branch
FROM test_table
START WITH parent_id IS NULL
CONNECT BY PRIOR id = parent_id)
ORDER BY branch
ID PARENT_ID PRINT_ORDER LVL
1 1 1
6 1 1 2
5 1 2 2
2 1 3 2
4 2 1 3
3 2 2 3
6 rows selected. -
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; -
Hierarchical Query - Connect By clause
Hi
I've written a query using the Connect By clause to generate the table data in Tree structure with the appropriate levels.
But Data might not correct such that it may have data which loops around. Like Table1 is parent of Table 2 and Table 2 is parent of Table 3 and Table 3 is again Parent of Table 1. If such incorrect Data exists then I'm getting Oracle Error "ORA-01436: CONNECT BY loop in user data" which is correct.
What I need is, whenever this error message is raised I need to give my own error message instead of this Oracle error in SQL statement. Please note, I'm not using any PLSQL block, I just have one SQL statement. Appreciate any help on this subject. Thanks.Hi,
Outside of PL/SQL, I don't know of any way to substitute your own error messages for the ones Oracle provides.
In Oracle 10 (and up), you can write a query using the CONNECT_BY_ISCYCLE pseudo-column. Using CONNECT BY NOCYCLE, the query will never actually raise the ORA-01436 error, but you can get it to display your message in cases where it would have, had you not been using CONNECT BY NOCYCLE.
Here's how to do it:
Put your real query, plus CONNECT_BY_ISCYCLE, in the WITH-clause.
Get the MAX (CONNECT_BY_ISCYCLE) from that sub-query. Let's call this max_cbi.
Do a UNION ALL of two prongs:
(1) SELECTs everything (except, pehaps, CONNECT_BY_ISCYCLE) from the sub-query, WHERE max_cbi = 0
(2) SELECTs your "error message" from dual, WHERE max_cbi = 1.
Edited by: Frank Kulash on Nov 26, 2008 2:09 PM
Maybe you are looking for
-
Playback appears different in IE and Firefox, WHY?
Hi, I have been working on a web site that I have just recently tested and noticed a wierd error: The playback appears fine in IE however in Firefox the scripted animated elements do ot apper correctly. Please visit the site in IE and Firefox: http:/
-
Cannot install creator-2_1-linux-ml.bin
This is the scenario: Hardware: P IV, 3 GHz, HT enabled, 4 GB RAM DDR 1, DD SATA. Software: Linux Fedora Core 5, I've made "yum update" so I suppouse that everyting it's updated by now. J2SDK: jdk-1_5_0_06-linux-1586.rpm I've setup JAVA_HOME so if i
-
[Forum FAQ] How to format and combine PowerShell outputs
Format the output with "Format-Table" and "Format-list" Sometimes when we query Powershell cmdlet, we would get ellipses in the result, which is not desirable. In this scenario, we can use the cmdlet "Format-Table" and "Format-list" to view the entir
-
Hi Guru: When I use transaction f110,I encountered this issue,it said "the system cannot display possible entries here" when I tried to click the run data column,and there is no identification value while trying to run f110,does there anything need
-
I'm new to RAW. What do I do next?
Hi everyone. I am new to Aperture and RAW files. I always used the jpeg format on my camera and now I'm experimenting with the RAW format. As expected I love the way the RAW files look. After importing the files into Aperture, and making my adjustmen